Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100681 > unrolled thread

What could cause a plot fail in my code?

Started byRobert <rxjwg98@gmail.com>
First post2015-12-21 09:15 -0800
Last post2015-12-22 14:12 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  What could cause a plot fail in my code? Robert <rxjwg98@gmail.com> - 2015-12-21 09:15 -0800
    Re: What could cause a plot fail in my code? Robert <rxjwg98@gmail.com> - 2015-12-21 09:29 -0800
      Re: What could cause a plot fail in my code? Peter Pearson <pkpearson@nowhere.invalid> - 2015-12-21 17:54 +0000
    Re: What could cause a plot fail in my code? Peter Otten <__peter__@web.de> - 2015-12-21 19:10 +0100
    Re: What could cause a plot fail in my code? Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-22 14:12 +0000

#100681 — What could cause a plot fail in my code?

FromRobert <rxjwg98@gmail.com>
Date2015-12-21 09:15 -0800
SubjectWhat could cause a plot fail in my code?
Message-ID<22c809c7-3aaf-4439-b919-8de982d7ffc9@googlegroups.com>
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?
Thanks,

[toc] | [next] | [standalone]


#100683

FromRobert <rxjwg98@gmail.com>
Date2015-12-21 09:29 -0800
Message-ID<af6b23d0-225b-42cc-9f53-114e07091ba0@googlegroups.com>
In reply to#100681
On Monday, December 21, 2015 at 12:15:54 PM UTC-5, 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?
> Thanks,

Excuse me. The code link should be this one:

http://nbviewer.ipython.org/gist/aflaxman/8329ec1b9f861469f896

Do you experience such errors?

Thanks,

[toc] | [prev] | [next] | [standalone]


#100687

FromPeter Pearson <pkpearson@nowhere.invalid>
Date2015-12-21 17:54 +0000
Message-ID<ddqsn1Fqf06U1@mid.individual.net>
In reply to#100683
On Mon, 21 Dec 2015 09:29:24 -0800 (PST), Robert <rxjwg98@gmail.com> wrote:
> On Monday, December 21, 2015 at 12:15:54 PM UTC-5, 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?
>> Thanks,
>
> Excuse me. The code link should be this one:
>
> http://nbviewer.ipython.org/gist/aflaxman/8329ec1b9f861469f896
>
> Do you experience such errors?


From a quick look, it appears that you are trying to plot a histogram
in which all the bins are empty.


-- 
To email me, substitute nowhere->runbox, invalid->com.

[toc] | [prev] | [next] | [standalone]


#100690

FromPeter Otten <__peter__@web.de>
Date2015-12-21 19:10 +0100
Message-ID<mailman.37.1450721463.2237.python-list@python.org>
In reply to#100681
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.

[toc] | [prev] | [next] | [standalone]


#100727

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-12-22 14:12 +0000
Message-ID<n5bloq$v26$1@dont-email.me>
In reply to#100681
On Mon, 21 Dec 2015 09:15:38 -0800, 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:

>    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?
> Thanks,

It looks as if ymin may be defined in a conditional block, and you've 
managed to reach line 6194 without going through that block.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web