scatter plot with constant x

Hey! :o)
This should be simple, but i cant manage: I need to plot many dots with the same x, like

plt.plot([3,3,3,3],[60,80,120,180],’+’,markersize=8,mec=‘k’)

The array for x values is silly, especially since the number of y values may be rather large. Is there a way to enter a constant there?

Cheers to you all!
Ulli

No, but you can do this:

plt.plot([3] * 4, [60, 80, 120, 180], …)

Does that help?
Ben Root

···

On Tue, Jun 5, 2012 at 11:53 AM, Ulrich vor dem Esche <ulrich.esche@…982…> wrote:

Hey! :o)
This should be simple, but i cant manage: I need to plot many dots with the same x, like

plt.plot([3,3,3,3],[60,80,120,180],’+’,markersize=8,mec=‘k’)

The array for x values is silly, especially since the number of y values may be rather large. Is there a way to enter a constant there?

Cheers to you all!
Ulli

...
No, but you can do this:

plt.plot([3] * 4, [60, 80, 120, 180], ...)

This started from a simple enough question, but it got me thinking about what the fastest way to do this is (in case you have HUGE arrays, or many loops over them). This may be old news to some of you, but I thought it was interesting:

In ipython --pylab

In [1]: %timeit l=[3]*10000
10000 loops, best of 3: 53.3 us per loop

In [2]: %timeit l=np.zeros(10000)+3
10000 loops, best of 3: 26.9 us per loop

In [3]: %timeit l=np.ones(10000)*3
10000 loops, best of 3: 32.9 us per loop

In [4]: %timeit l=(np.zeros(1)+3).repeat(10000)
10000 loops, best of 3: 87.4 us per loop

In [5]: %timeit l=np.zeros(10000);l[:]=3
10000 loops, best of 3: 21.6 us per loop

In [6]: %timeit l=np.zeros(10000,dtype=np.uint8);l[:]=3
100000 loops, best of 3: 13.9 us per loop

Using int16, int32, float32 get progressively slower to the default float64 case listed on line [5], changing the datatype in other methods doesn't result in nearly as large a speed up as it does in the last case.

Ben's method is probably the most elegant for small arrays, but does any one else have a faster way to do this? (I'm assuming no use of blitz, inline C, f2py, but if you think you can do it faster in one of those, show me the way).

Sorry, maybe this is more appropriate on the numpy list.

Ethan

Interesting result. Note, however, that matplotlib will eventually turn all data arrays into float64 at rendering time, so any speed advantage to using integers will be lost by the subsequent conversion, I suspect.

Mike

···

On 06/06/2012 12:42 PM, Ethan Gutmann wrote:

...
No, but you can do this:

plt.plot([3] * 4, [60, 80, 120, 180], ...)

Using int16, int32, float32 get progressively slower to the default float64 case listed on line [5], changing the datatype in other methods doesn't result in nearly as large a speed up as it does in the last case.

I don't think it does if you pass uint8 to imshow, but otherwise you might be right.

ethan

···

On Jun 6, 2012, at 10:49 AM, Michael Droettboom wrote:

Interesting result. Note, however, that matplotlib will eventually turn
all data arrays into float64 at rendering time, so any speed advantage
to using integers will be lost by the subsequent conversion, I suspect.

Sure. I was referring to scatter here.

With imshow, of course, everything is ultimately turned into 8-bits-per-plane rgba.

Mike

···

On 06/06/2012 12:54 PM, Ethan Gutmann wrote:

On Jun 6, 2012, at 10:49 AM, Michael Droettboom wrote:

Interesting result. Note, however, that matplotlib will eventually turn
all data arrays into float64 at rendering time, so any speed advantage
to using integers will be lost by the subsequent conversion, I suspect.

I don't think it does if you pass uint8 to imshow, but otherwise you might be right.

...
No, but you can do this:

plt.plot([3] * 4, [60, 80, 120, 180], ...)

This started from a simple enough question, but it got me thinking about what the fastest way to do this is (in case you have HUGE arrays, or many loops over them). This may be old news to some of you, but I thought it was interesting:

In ipython --pylab

In [1]: %timeit l=[3]*10000
10000 loops, best of 3: 53.3 us per loop

In [2]: %timeit l=np.zeros(10000)+3
10000 loops, best of 3: 26.9 us per loop

In [3]: %timeit l=np.ones(10000)*3
10000 loops, best of 3: 32.9 us per loop

In [4]: %timeit l=(np.zeros(1)+3).repeat(10000)
10000 loops, best of 3: 87.4 us per loop

In [5]: %timeit l=np.zeros(10000);l[:]=3
10000 loops, best of 3: 21.6 us per loop

In [6]: %timeit l=np.zeros(10000,dtype=np.uint8);l[:]=3
100000 loops, best of 3: 13.9 us per loop

Using int16, int32, float32 get progressively slower to the default float64 case listed on line [5], changing the datatype in other methods doesn't result in nearly as large a speed up as it does in the last case.

Ben's method is probably the most elegant for small arrays, but does any one else have a faster way to do this? (I'm assuming no use of blitz, inline C, f2py, but if you think you can do it faster in one of those, show me the way).

Since we end up needing float64 anyway:

In [3]: %timeit l=np.empty(10000,dtype=np.float64); l.fill(3)
100000 loops, best of 3: 14.1 us per loop

In [4]: %timeit l=np.zeros(10000,dtype=np.float64);l[:]=3
10000 loops, best of 3: 26.6 us per loop

Eric

···

On 06/06/2012 06:42 AM, Ethan Gutmann wrote:

Sorry, maybe this is more appropriate on the numpy list.

Ethan

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

nice, fill and empty seem to be responsible for about half the speed up each, good tools to know about.

···

On Jun 6, 2012, at 11:41 AM, Eric Firing wrote:

Since we end up needing float64 anyway:

In [3]: %timeit l=np.empty(10000,dtype=np.float64); l.fill(3)
100000 loops, best of 3: 14.1 us per loop

From: Eric Firing [mailto:efiring@…202…]
Sent: Wednesday, June 06, 2012 13:41
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] scatter plot with constant x

>> ...
>> No, but you can do this:
>>
>> plt.plot([3] * 4, [60, 80, 120, 180], ...)
>
> This started from a simple enough question, but it got me
thinking about what the fastest way to do this is (in case
you have HUGE arrays, or many loops over them).

[...]

Since we end up needing float64 anyway:

In [3]: %timeit l=np.empty(10000,dtype=np.float64); l.fill(3)
100000 loops, best of 3: 14.1 us per loop

In [4]: %timeit l=np.zeros(10000,dtype=np.float64);l[:]=3
10000 loops, best of 3: 26.6 us per loop

Eric

Numpy's as_strided came to mind; it can make a large array that's really a
view of a one-element array:

    In [1]: as_strided = np.lib.stride_tricks.as_strided

    In [2]: s = as_strided(np.array([3], dtype=np.float64), shape=(10000,),
       ...: strides=(0,))

    In [3]: s[0] = 4

    In [4]: s[9999] # all elements share data
    Out[4]: 4.0

It's somewhat slower to create the base array and the view than to create and
fill a 10000-element array:

    In [5]: %timeit l = np.empty(10000, dtype=np.float64); l.fill(3)
    100000 loops, best of 3: 10.1 us per loop

    In [6]: %timeit s = as_strided(np.array([3], dtype=np.float64),
    shape=(10000,), strides=(0,)) # line broken for email
    10000 loops, best of 3: 21.6 us per loop

However, once created, its contents may be changed much more quickly:

    In [7]: l = np.empty(10000, dtype=np.float64)

    In [8]: %timeit l.fill(3)
    100000 loops, best of 3: 7.71 us per loop

    In [9]: %timeit s[0] = 3
    10000000 loops, best of 3: 116 ns per loop

Numpy's broadcast_arrays uses as_strided under the hood. Code could look
like:

    x, y = np.broadcast_arrays(3, [60, 80, 120, 180])
    plt.plot(x, y, '+')
    x[0] = 21 # new x for all samples
    plt.plot(x, y, 'x')

···

On 06/06/2012 06:42 AM, Ethan Gutmann wrote:

I usually do something like:
y=np.array([60,80,120,180])
x = np.ones_like(y)*3
plt.plot(x, y,’+’,markersize=8,mec=‘k’)
David G. Parker

From:
Benjamin Root <ben.root@…1304…>

To:
Ulrich vor dem Esche
<ulrich.esche@…982…>

Cc:
matplotlib-users@lists.sourceforge.net

Date:
06/06/2012 12:15 PM

Subject:
Re: [Matplotlib-users]
scatter plot with constant x

···

On Tue, Jun 5, 2012 at 11:53 AM, Ulrich vor dem Esche <ulrich.esche@…982…> wrote:

Hey! :o)

This should be simple, but i cant manage: I need to plot many dots with
the same x, like

plt.plot([3,3,3,3],[60,80,120,180],’+’,markersize=8,mec=‘k’)

The array for x values is silly, especially since the number of y values
may be rather large. Is there a way to enter a constant there?

Cheers to you all!

Ulli

No, but you can do this:

plt.plot([3] * 4, [60, 80, 120, 180], …)

Does that help?

Ben Root

`------------------------------------------------------------------------------

Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. [http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________](http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________)

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)

`