Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyplot.errorbar: problem with some shapes of the positional arguments #2197

Closed
khyox opened this issue Jul 2, 2013 · 2 comments · Fixed by #3014
Closed

pyplot.errorbar: problem with some shapes of the positional arguments #2197

khyox opened this issue Jul 2, 2013 · 2 comments · Fixed by #3014

Comments

@khyox
Copy link
Contributor

khyox commented Jul 2, 2013

This is not a major issue indeed, but I lost quite time until I found where the problem was. About pyplot.errorbar (plot x versus y with error deltas in yerr and xerr) the doc states this about the first optional argument:

xerr/yerr: [ scalar | N, Nx1, or 2xN array-like ] If a scalar number, len(N) array-like
object, or an Nx1 array-like object, errorbars are drawn at +/-value relative to the data.
(...)

But in some cases this is not working as expected (or at least, after reading the doc, as I understand it should work). I prepared an example to show where the problems arise (tested with matplotlib 1.4.x, numpy 1.7.1; python 3.3.2):

import numpy as np
import matplotlib.pyplot as plt

x1=np.array([1.2e-05, 1.3e-05, 1.6e-05, 1.7e-05, 1.7e-05])
y1=np.array([6.6e-05, 7.2e-05, 8.9e-05, 9.3e-05, 9.3e-05])
err1=np.array([5.0e-06, 5.0e-06, 5.0e-06, 5.0e-06, 5.0e-06])
print('Data set 1 shape (x1,y1,err1): (%s,%s,%s)' % (x1.shape, y1.shape, err1.shape))

x2=np.array([[  1.2e-05],
             [  1.3e-05],
             [  1.6e-05],
             [  1.7e-05],
             [  1.7e-05]])
y2=np.array([[  6.6e-05],
             [  7.2e-05],
             [  8.9e-05],
             [  9.3e-05],
             [  9.3e-05]])
err2=np.array([[  5.0e-06],
                [  5.0e-06],
                [  5.0e-06],
                [  5.0e-06],
                [  5.0e-06]])
print('Data set 2 shape (x2,y2,err2): (%s,%s,%s)' % (x2.shape, y2.shape, err2.shape))

ax = plt.subplot(141)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
plt.errorbar(x1, y1, yerr=err1) # OK!
ax.set_ylim(ymin=1e-5)

ax = plt.subplot(142)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
plt.errorbar(x2, y2) # Also OK
ax.set_ylim(ymin=1e-5)

ax = plt.subplot(143)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
plt.errorbar(x2, y2, yerr=err2) # Fails!
ax.set_ylim(ymin=1e-5)

ax = plt.subplot(144)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
plt.errorbar(x2, y2, yerr=err1) # also fails
ax.set_ylim(ymin=1e-5)

plt.show()

Thanks!

@tacaswell
Copy link
Member

@khyox Is the fact that this is with a log scale relevant?

@khyox
Copy link
Contributor Author

khyox commented Feb 27, 2014

@tacaswell No, I am afraid not. This shorter version also fails:

import numpy as np
import matplotlib.pyplot as plt

x1=np.array([1.2e-05, 1.3e-05, 1.6e-05, 1.7e-05, 1.7e-05])
y1=np.array([6.6e-05, 7.2e-05, 8.9e-05, 9.3e-05, 9.3e-05])
err1=np.array([5.0e-06, 5.0e-06, 5.0e-06, 5.0e-06, 5.0e-06])
print('Data set 1 shape (x1,y1,err1): (%s,%s,%s)' % (x1.shape, y1.shape, err1.shape))

x2=np.array([[  1.2e-05],
             [  1.3e-05],
             [  1.6e-05],
             [  1.7e-05],
             [  1.7e-05]])
y2=np.array([[  6.6e-05],
             [  7.2e-05],
             [  8.9e-05],
             [  9.3e-05],
             [  9.3e-05]])
err2=np.array([[  5.0e-06],
                [  5.0e-06],
                [  5.0e-06],
                [  5.0e-06],
                [  5.0e-06]])
print('Data set 2 shape (x2,y2,err2): (%s,%s,%s)' % (x2.shape, y2.shape, err2.shape))

ax = plt.subplot(141)
plt.errorbar(x1, y1, yerr=err1) # OK!

ax = plt.subplot(142)
plt.errorbar(x2, y2) # Also OK

ax = plt.subplot(143)
plt.errorbar(x2, y2, yerr=err2) # Fails!

ax = plt.subplot(144)
plt.errorbar(x2, y2, yerr=err1) # also fails

plt.show()

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Apr 27, 2014
 - passing in a (N, 1) shaped array as input causes the zipping to
   generate an extra dimension
 - use squeeze to get rid of useless dimensions
 - use atleast_1d to make sure scalars remain 1D ((1, ) vs () shape)
 - closes matplotlib#2197
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Apr 28, 2014
 - passing in a (N, 1) shaped array as input causes the zipping to
   generate an extra dimension
 - closes matplotlib#2197
 - use ravel + reshape to do shape validation + getting rid of
   extra dimension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants