What could cause a plot fail in my code?

Peter Otten __peter__ at web.de
Mon Dec 21 13:10:40 EST 2015


Robert wrote:

> Hi,
> 
> I find a useful code snippet on link:
> 
> http://stackoverflow.com/questions/25126444/logistic-regression-in-pymc/34400966#34400966
> 
> but it has error on plot function. The error message is as following:
> ---------
> %run "C:\Users\rj\pyprj\logic_regression0.py"
> [-----------------100%-----------------] 10000 of 10000 complete in 12.6  
> [    secPlotting beta0
> Plotting tau
> Plotting betaSalad
> Plotting sigma
> 
---------------------------------------------------------------------------
> UnboundLocalError                         Traceback (most recent call
> last) C:\Users\pyprj\logic_regression0.py in <module>()
>      34 #m.sample(100000, 50000, 50)
>      35 m.sample(10000, 5000, 50)
> ---> 36 pm.Matplot.plot(m)
>      37 import matplotlib.pyplot as plt
>      38 #plt.plot(m)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
> packages\pymc\Matplot.pyc in wrapper(pymc_obj, *args, **kwargs)
>     339                     if args:
>     340                         name = '%s_%s' % (args[0],
>     variable.__name__)
> --> 341                     f(data, name, *args, **kwargs)
>     342             return
>     343         except AttributeError:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\pymc\Matplot.pyc
> in plot(data, name, format, suffix, path,  common_scale, datarange, new,
> last, rows, num, fontmap, verbose)
>     453             num=num * 2,
>     454             last=last,
> --> 455             fontmap=fontmap)
>     456
>     457         if last:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\pymc\Matplot.pyc
> in wrapper(pymc_obj, *args, **kwargs)
>     375
>     376         # If others fail, assume that raw data is passed
> --> 377         f(pymc_obj, *args, **kwargs)
>     378
>     379     wrapper.__doc__ = f.__doc__
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\pymc\Matplot.pyc
> in histogram(data, name, bins, datarange, format, suffix, path, rows,
> columns, num, last, fontmap, verbose)
>     576
>     577         # Generate histogram
> --> 578         hist(data.tolist(), bins, histtype='stepfilled')
>     579
>     580         xlim(datarange)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\matplotlib\pyplot.py
> in hist(x, bins, range, normed, weights, cumulative, bottom, histtype,
> align, orientation, rwidth, log, color, label, stacked, hold, data,
> **kwargs)
>    2956                       histtype=histtype, align=align,
>    orientation=orientation,
>    2957                       rwidth=rwidth, log=log, color=color,
>    label=label,
> -> 2958                       stacked=stacked, data=data, **kwargs)
>    2959     finally:
>    2960         ax.hold(washold)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\matplotlib\__init__.py
> in inner(ax, *args, **kwargs)
>    1809                     warnings.warn(msg % (label_namer,
>    func.__name__),
>    1810                                   RuntimeWarning, stacklevel=2)
> -> 1811             return func(ax, *args, **kwargs)
>    1812         pre_doc = inner.__doc__
>    1813         if pre_doc is None:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-
packages\matplotlib\axes\_axes.py
> in hist(self, x, bins, range, normed, weights, cumulative, bottom,
> histtype, align, orientation, rwidth, log, color, label, stacked,
> **kwargs)
>    6192                         ymin = np.amin(m[m != 0])
>    6193                         # filter out the 0 height bins
> -> 6194                 ymin = max(ymin*0.9, minimum) if not input_empty
> else minimum
>    6195                 ymin = min(ymin0, ymin)
>    6196                 self.dataLim.intervaly = (ymin, ymax)
> 
> UnboundLocalError: local variable 'ymin' referenced before assignment
> /////////////
> 
> I have no clue at all on debug it. Could you help me?

To me that looks like a bug in matplotlib. ymin is not properly initialised 
before the loop

"""
                for m in n:
                    if np.sum(m) > 0:  # make sure there are counts
                        ymin = np.amin(m[m != 0])
                        # filter out the 0 height bins
                ymin = max(ymin*0.9, minimum) if not input_empty else 
minimum
"""

so if for there aren't any m with np.sum(m) > 0 (i. e. all bins are empty) 
you get the error. Here's a made-up example for illustration:

>>> def last_positive_item(items):
...     for item in items:
...             if item > 0:
...                     last = item
...     return last
... 

As long as you pass at least one positive item everything seems OK:

>>> last_positive_item([1, 2, -3, 3])
3

But if you don't you get the error:

>>> last_positive_item([-1, -2, -3, 0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in last_positive_item
UnboundLocalError: local variable 'last' referenced before assignment

Please file a bug report if there isn't one already.




More information about the Python-list mailing list