[Matplotlib-users] [Question] Possible bug in plotting large NumPy array
juhaszp95 at gmail.com
juhaszp95 at gmail.com
Fri Aug 19 06:10:02 EDT 2016
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160819/89db77df/attachment-0001.html>
More information about the Matplotlib-users
mailing list