Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: What could cause a plot fail in my code? Date: Mon, 21 Dec 2015 19:10:40 +0100 Organization: None Lines: 141 Message-ID: References: <22c809c7-3aaf-4439-b919-8de982d7ffc9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de yueSA8Zc9ChZ7lL5eqGrMwpeyhVRMQ4D/3+AZ4iiWmEA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'debug': 0.04; '"""': 0.05; '---------': 0.05; 'error:': 0.05; 'finally:': 0.05; 'none:': 0.05; '*args,': 0.07; 'assignment': 0.07; 'skip:/ 10': 0.07; 'subject:code': 0.07; '340': 0.09; 'already.': 0.09; 'label,': 0.09; 'orientation,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows,': 0.09; 'skip:[ 40': 0.09; 'snippet': 0.09; 'subject:plot': 0.09; 'bug': 0.10; 'assume': 0.11; 'def': 0.13; 'properly': 0.15; '339': 0.16; '343': 0.16; '379': 0.16; '50)': 0.16; 'align,': 0.16; 'args:': 0.16; 'fail,': 0.16; 'last)': 0.16; 'made-up': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'verbose)': 0.16; 'wrote:': 0.16; 'variable': 0.18; '>>>': 0.20; 'skip:" 40': 0.20; '"",': 0.22; "aren't": 0.22; 'color,': 0.22; 'new,': 0.22; 'pass': 0.22; 'seems': 0.23; 'import': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'raw': 0.27; 'skip:# 10': 0.27; 'least': 0.27; 'data,': 0.27; 'format,': 0.27; 'looks': 0.29; '**kwargs)': 0.29; 'path,': 0.29; 'referenced': 0.29; 'code': 0.30; 'error.': 0.31; 'useful': 0.33; 'traceback': 0.33; 'me?': 0.34; 'file': 0.34; 'except': 0.34; 'skip:c 30': 0.35; 'could': 0.35; 'filter': 0.35; 'robert': 0.35; "isn't": 0.35; 'item': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'minimum': 0.38; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'data': 0.39; 'sure': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'complete': 0.63; '10000': 0.66; 'link:': 0.69; 'counts': 0.81; '--->': 0.84; 'matplotlib.': 0.84; '378': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd814f.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100690 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 () > 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 "", line 1, in File "", line 5, in last_positive_item UnboundLocalError: local variable 'last' referenced before assignment Please file a bug report if there isn't one already.