[Matplotlib-users] [Question] Possible bug in plotting large NumPy array

Jens Nielsen jenshnielsen at gmail.com
Fri Aug 19 06:31:49 EDT 2016


The problem is the way you are slicing your data

by doing y[:1] you are creating a 1 by 400 array. When you try to plot that
you will get 400 individual line plots each with only one point. A line
plot with one point is invisible since there in no other points to draw the
lines between. You will not notice that with scatter since it defaults to
drawing a point for each data point. If you add a marker you can see whats
going on.
I.e. do
ax1.plot(x, y[:1], 'o')
in the first example and you will notice that the points color cycle as a
new plot is created.

If you really want to slice this way you have to ensure that your data is
along the first dimension.  I.e. you can do `ax1.plot(x.transpose(),
y[:1].transpose())` which plots 400 by 1 arrays

Hope that helps
Jens



On Fri, 19 Aug 2016 at 11:10 <juhaszp95 at gmail.com> wrote:

> Dear Matplotlib users,
>
>
>
> I am Péter Juhász and I started experimenting with this demo:
> http://matplotlib.org/examples/pylab_examples/subplots_demo.html.
>
>
>
> What I then tried to achieve is to let *y* be a NumPy array of 2 rows,
> the first holding sin(x**2) values, the second holding something else (I
> set cos(x**2)), and the make the same plots as in the original demo only by
> using the first row of *y*. Unfortunately, I was quite surprised to see
> that pyplot/matplotlib only succeeds with this plotting if *scatter plots*
> or *markers* are used *only*, instead of the usual plotting with lines.
> On the other hand, with some tricks in the NumPy array (some reshapes and
> slicing) I was able to make pyplot/matplotlib work for both scatter plots
> and the usual simple plots. This however possibly takes much more time, so
> for big datasets does not seem feasible and anyway I do not see an obvious
> reason why a dataset should only work with scatter plots but not the usual,
> simple plots.
>
>
>
> Please find attached my code below and I would appreciate any
> help/explanation, or it is indeed a bug, then if it could be raised to the
> developers’ attention.
>
>
>
> My original trial, working only with scatter plots (see for yourself!
> maybe it’s only my installation what is going wrong?):
>
>
>
> import matplotlib.pyplot as plt
>
> import numpy as np
>
>
>
> # Simple data to display in various forms
>
> x = np.linspace(0, 2 * np.pi, 400).reshape(1, 400)
>
> print(x.shape)
>
> y = np.vstack((np.sin(x ** 2), np.cos(x ** 2)))
>
> print(y[:1].shape)
>
>
>
> plt.close(*'all'*)
>
>
>
> # Three *subplots* sharing both x/y axes
>
> f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
>
> ax1.plot(x, y[:1]) # This is not showing!
>
> ax1.set_title(*'Sharing both axes'*)
>
> ax2.scatter(x, y[:1])
>
> ax3.scatter(x, 2 * y[:1] ** 2 - 1, color=*'r'*)
>
> # Fine-tune figure; make *subplots* close to each other and hide x ticks
> for
>
> # all but bottom plot.
>
> f.subplots_adjust(hspace=0)
>
> plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
>
>
>
> # row and column sharing
>
> f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=*'col'*, sharey=
> *'row'*)
>
> ax1.plot(x, y[:1]) # This is not showing!
>
> ax1.set_title(*'Sharing x per column, y per row'*)
>
> ax2.scatter(x, y[:1])
>
> ax3.scatter(x, 2 * y[:1] ** 2 - 1, color=*'r'*)
>
> ax4.plot(x, 2 * y[:1] ** 2 - 1, color=*'r'*) # This is not showing!
>
>
>
> plt.show()
>
>
>
>
>
> The working solution:
>
>
>
> import matplotlib.pyplot as plt
>
> import numpy as np
>
>
>
> # Simple data to display in various forms
>
> x = np.linspace(0, 2 * np.pi, 400).reshape(400, 1)
>
> print(x.shape)
>
> y = np.vstack((np.sin(x ** 2), np.cos(x ** 2))).reshape(400, 2)
>
> print(y[:, 0].shape)
>
>
>
> plt.close(*'all'*)
>
>
>
> # Three *subplots* sharing both x/y axes
>
> f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
>
> ax1.plot(x, y[:, 0])
>
> ax1.set_title(*'Sharing both axes'*)
>
> ax2.scatter(x, y[:, 0])
>
> ax3.scatter(x, 2 * y[:, 0] ** 2 - 1, color=*'r'*)
>
> # Fine-tune figure; make *subplots* close to each other and hide x ticks
> for
>
> # all but bottom plot.
>
> f.subplots_adjust(hspace=0)
>
> plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
>
>
>
> # row and column sharing
>
> f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=*'col'*, sharey=
> *'row'*)
>
> ax1.plot(x, y[:, 0])
>
> ax1.set_title(*'Sharing x per column, y per row'*)
>
> ax2.scatter(x, y[:, 0])
>
> ax3.scatter(x, 2 * y[:, 0] ** 2 - 1, color=*'r'*)
>
> ax4.plot(x, 2 * y[:, 0] ** 2 - 1, color=*'r'*)
>
>
>
> plt.show()
>
>
>
> I did not modify anything else in the code respective to the original demo
> than what I mentioned. I hope you will be able to help.
>
>
>
> Kind regards,
>
> Péter
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160819/0cae93a1/attachment-0001.html>


More information about the Matplotlib-users mailing list