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

errorbar of (x,y) data on semilogx plot with NaN in x throws ValueError if errorbar() command initializes the axes #5541

Closed
skjaeve opened this issue Nov 22, 2015 · 5 comments
Milestone

Comments

@skjaeve
Copy link

skjaeve commented Nov 22, 2015

If I plot an errorbar on a semilogx axis, and there are NaN datapoints in the X data, a ValueError is thrown if the axes is not already initialized. If the axes is already set up, the errorbar is plotted as expected and there is no error.

The ValueError message is:

 ValueError: Data has no positive values, and therefore can not be log-scaled.

which is unhelpful, as it is incorrect. Full error chain further down.

This only affects the semilog X axis, not the semilog Y axis.

Example code:

#!/usr/bin/env python

import numpy
import matplotlib
from matplotlib import pyplot

print "matplotlib version:", matplotlib.__version__
print "numpy version:", numpy.__version__

# Some data
x = numpy.arange(0,10,.1)
y1 = 10**numpy.sin(x)
y2 = 10**numpy.sin(x)
y2[10] = numpy.nan


for i in 1,2:

    f, ((ax1, ax2), (ax3, ax4)) = pyplot.subplots(nrows=2, ncols=2)

    ax1.set_yscale('log')
    ax2.set_yscale('log')

    ax3.set_xscale('log')
    ax4.set_xscale('log')

    ax1.errorbar(x, y1, yerr=x/100)
    ax1.set_title('semilogy, not nan')
    ax2.errorbar(x, y2, yerr=x/100)
    ax2.set_title('semilogy, nan')
    ax3.errorbar(y1, x, xerr=x/100)
    ax3.set_title('semilogx, not nan')
    if i==1:
        ax4.plot(y2, x, 'r') # If this is exectured the script runs without error
    ax4.errorbar(y2, x, xerr=x/100) # if this command initializes the axes a ValueError is thrown
    ax4.set_title('semilogx, nan')

Running this will open two figures, one with four plots, the other with three plots and one empty axes, and then this message in the terminal:

In [29]: run minimum_example.py
matplotlib version: 1.5.0+441.g3764d42
numpy version: 1.10.1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/asmund/src/minimum_example.py in <module>()
     33         if i==1:
     34                 ax4.plot(y2, x, 'r') # If this is exectured the script runs without error
---> 35         ax4.errorbar(y2, x, xerr=x/100) # if this command initializes the axes a ValueError is thrown
     36         ax4.set_title('semilogx, nan')

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1844                     warnings.warn(msg % (label_namer, func.__name__),
   1845                                   RuntimeWarning, stacklevel=2)
-> 1846             return func(ax, *args, **kwargs)
   1847         pre_doc = inner.__doc__
   1848         if pre_doc is None:

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/axes/_axes.pyc in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
   2907                 yo, _ = xywhere(y, right, noxlims & everymask)
   2908                 lo, ro = xywhere(left, right, noxlims & everymask)
-> 2909                 barcols.append(self.hlines(yo, lo, ro, **lines_kw))
   2910                 if capsize > 0:
   2911                     caplines.extend(self.plot(lo, yo, 'k|', **plot_kw))

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1844                     warnings.warn(msg % (label_namer, func.__name__),
   1845                                   RuntimeWarning, stacklevel=2)
-> 1846             return func(ax, *args, **kwargs)
   1847         pre_doc = inner.__doc__
   1848         if pre_doc is None:

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/axes/_axes.pyc in hlines(self, y, xmin, xmax, colors, linestyles, label, **kwargs)
   1006 
   1007             self.update_datalim(corners)
-> 1008             self.autoscale_view()
   1009 
   1010         return coll

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/axes/_base.pyc in autoscale_view(self, tight, scalex, scaley)
   2155                 x1 += delta
   2156             if not _tight:
-> 2157                 x0, x1 = xlocator.view_limits(x0, x1)
   2158             self.set_xbound(x0, x1)
   2159 

/home/asmund/venv/py2-mplgit/lib/python2.7/site-packages/matplotlib-1.5.0+441.g3764d42-py2.7-linux-x86_64.egg/matplotlib/ticker.pyc in view_limits(self, vmin, vmax)
   1608         if minpos <= 0 or not np.isfinite(minpos):
   1609             raise ValueError(
-> 1610                 "Data has no positive values, and therefore can not be "
   1611                 "log-scaled.")
   1612 

ValueError: Data has no positive values, and therefore can not be log-scaled.

In [30]: 
@tacaswell tacaswell added this to the proposed next point release (2.1) milestone Nov 22, 2015
@tacaswell
Copy link
Member

This is the auto-scale logic having issues with logscale, which is unfortunately a known issue. In most other cases this just results in really bad limits, not an exception.

Another simple work around is to set ax.set_*scale after doing the plotting.

Another, somewhat more sneaky, work around is to use ax4.errorbar(y2, x, xerr=x/100, barsabove=True)

@AshishBora
Copy link

Any updates on this? It seems this is still an issue.

@kadrlica
Copy link

I hit this issue on travis using MPLBACKEND=Agg. Following @tacaswell suggestion to set the scale after plotting fixed it.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@danieldwright
Copy link

I just reported #9800 which most likely has the same root cause, but is perhaps easier to reproduce.

@dstansby
Copy link
Member

I can reproduce this on Matplotlib version 1.5.3, but not 2.0 or 2.1; I'd recommend upgrading Matplotlib and this should all be working now - please comment if it isn't!

@QuLogic QuLogic modified the milestones: v2.2, v2.0.0 Nov 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants