| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
plt.clf()
fig, ax = plt.subplots(1,2, sharey=True)
plt.setp(ax.flat, aspect=1.0, adjustable='box-forced')
#fig.set_size_inches(16,8)
sns.despine(fig=fig)
#scale = 'logit'
scale = None
plot.plot_year_dist(df_mesh[(df_mesh['Year'] >= min_y) & (df_mesh['Year'] <= 2009)],col='min_VolFirstP',col_label="Year",thresh=10,file_prefix='individual_', ax=ax[0],color=sns.xkcd_rgb['black'],label='Individual Volume [%s-2009]' % min_y,marker="o", scale = scale)
plot.plot_year_dist(df[(df['Year'] >= min_y) & (df['Year'] <= 2009)],col='min_VolFirstP',col_label="Year",thresh=10,file_prefix='pair_', ax=ax[0],color=sns.xkcd_rgb['red'],label='Pair Volume [%s-2009]' % min_y, marker="s", scale = scale)
plot.plot_year_dist(df_mesh[(df_mesh['Year'] >= min_y) & (df_mesh['Year'] <= 2009)],col='min_TFirstP',col_label="Years",thresh=3,file_prefix='individual_', ax=ax[1],color=sns.xkcd_rgb['black'],label='Individual Time [%s-2009]' % min_y,marker="o", scale = scale)
plot.plot_year_dist(df[(df['Year'] >= min_y) & (df['Year'] <= 2009)],col='min_TFirstP',col_label="Years",thresh=3,file_prefix='pair_', ax=ax[1],color=sns.xkcd_rgb['red'],label='Pair Time [%s-2009]' % min_y,marker="s", scale = scale)
for i in range(2):
ax[i].set_yscale("logit", p_min=1e-4)
ax[i].legend(bbox_to_anchor=(0., 1.02, 1., .102), borderaxespad=0., frameon=True, fancybox=True)
plt.savefig("temp.pdf",bbox_inches='tight')When running the above code I am getting the following error:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-75-76bd951dfd46> in <module>()
19
20
---> 21 plt.savefig("Novelty_year_dist_logit.pdf",bbox_inches='tight')
22 plt.savefig("Novelty_year_dist_logit.eps",bbox_inches='tight')
/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in savefig(*args, **kwargs)
575 def savefig(*args, **kwargs):
576 fig = gcf()
--> 577 res = fig.savefig(*args, **kwargs)
578 draw() # need this if 'transparent=True' to reset colors
579 return res
/usr/local/lib/python2.7/dist-packages/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
1474 self.set_frameon(frameon)
1475
-> 1476 self.canvas.print_figure(*args, **kwargs)
1477
1478 if frameon:
/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2156 orientation=orientation,
2157 dryrun=True,
-> 2158 **kwargs)
2159 renderer = self.figure._cachedRenderer
2160 bbox_inches = self.figure.get_tightbbox(renderer)
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.pyc in print_pdf(self, filename, **kwargs)
2483 RendererPdf(file, image_dpi),
2484 bbox_inches_restore=_bbox_inches_restore)
-> 2485 self.figure.draw(renderer)
2486 renderer.finalize()
2487 finally:
/usr/local/lib/python2.7/dist-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
57 def draw_wrapper(artist, renderer, *args, **kwargs):
58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
60 after(artist, renderer)
61
/usr/local/lib/python2.7/dist-packages/matplotlib/figure.pyc in draw(self, renderer)
1083 dsu.sort(key=itemgetter(0))
1084 for zorder, a, func, args in dsu:
-> 1085 func(*args)
1086
1087 renderer.close_group('figure')
/usr/local/lib/python2.7/dist-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
57 def draw_wrapper(artist, renderer, *args, **kwargs):
58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
60 after(artist, renderer)
61
/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
2004 self.apply_aspect(pos)
2005 else:
-> 2006 self.apply_aspect()
2007
2008 artists = []
/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.pyc in apply_aspect(self, position)
1191 fig_aspect = figH / figW
1192 if self._adjustable in ['box', 'box-forced']:
-> 1193 if aspect_scale_mode == "log":
1194 box_aspect = A * self.get_data_ratio_log()
1195 else:
UnboundLocalError: local variable 'aspect_scale_mode' referenced before assignment
However, running the example explained in http://stackoverflow.com/a/13663328/1650159 doesn't give this error.
Additional Info:
def plot_year_dist(df,col='',col_label="",thresh=0,file_prefix='',outFolder=DEFAULT_OUTPUT,\
ax=None,color=sns.xkcd_rgb['black'],label='',scale=None,**kwargs):
saveFig = False
if ax is None:
plt.clf()
ax = plt.gca()
saveFig = True
filename = file_prefix+col+"_year_dist.png"
df_g = df[[col,'Year']].groupby('Year').count()
df_g["filtered"] = df[[col,"Year"]][df[col] <= thresh].groupby("Year").count()
df_g["prop"] = df_g["filtered"]/df_g[col]
#print df_g.index
#print df_g
x = df_g.index.values
y = df_g["prop"].values
if scale == 'logit':
y = np.log10(y/(1.0-y))
ax.plot(x,y, linestyle='-',markersize=3.0,color=color,label=label,**kwargs)
print "Finished generating histogram"
ax.set_xlabel(col_label)
ax.set_ylabel("Proportion (<= %s)" % thresh)
if scale == 'logit':
ax.set_ylabel("$log_{10}(p/1-p)$ [$p = P(X<= %s)$]" % thresh)
if saveFig:
plt.savefig(outFolder+"/"+filename)
print "Plot saved in %s." % (filename)| Back | FazBrowse Home | New Git URL |
Activity
WeatherGod commented on Aug 11, 2015
Looks like this piece of code is the problem part:
if self.name != 'polar': xscale, yscale = self.get_xscale(), self.get_yscale() if xscale == "linear" and yscale == "linear": aspect_scale_mode = "linear" elif xscale == "log" and yscale == "log": aspect_scale_mode = "log" elif ((xscale == "linear" and yscale == "log") or (xscale == "log" and yscale == "linear")): if aspect != "auto": warnings.warn( 'aspect is not supported for Axes with xscale=%s, ' 'yscale=%s' % (xscale, yscale)) aspect = "auto" else: # some custom projections have their own scales. pass else: aspect_scale_mode = "linear"It has two branches where aspect_scale_mode is left undefined. Probably
have it set to None or something like that. I'll put together a PR.
On Tue, Aug 11, 2015 at 6:31 AM, Shubhanshu Mishra <notifications@github.com
WeatherGod commented on Aug 11, 2015
I just tried making a test based off of your example, but I don't know 1)
the scale of your data and 2) the p_min argument doesn't seem to be valid.
I am not sure how that works for you. Could you try making a more
simplified version of your script that demonstrates the problem and can
easily be made into a test.
On Tue, Aug 11, 2015 at 8:56 AM, Benjamin Root ben.root@ou.edu wrote:
napsternxg commented on Aug 11, 2015
Hi @WeatherGod thanks for looking into this.
The code is supposed to ensure that each subplot is square in shape.
Here is another set of code which I am running before running the above lines of code:
p_min argument is for controlling the parameters of the logit scale.
WeatherGod commented on Aug 11, 2015
Ah, that would explain why the logit scale wasn't working (matplotlib has
its own logit scale available).
This example, though isn't a SSCCE: http://sscce.org/
It isn't short, nor is it self-contained (I don't have the data, nor do I
want the data). The example doesn't need the extra bells and whistles, and
it can have fake data generated using np.logspace(). The x-data doesn't
need to be dates, either, for the purposes of the demonstration.
On Tue, Aug 11, 2015 at 12:43 PM, Shubhanshu Mishra <
notifications@github.com> wrote:
napsternxg commented on Aug 11, 2015
I am using version 1.4.3 in which the logit scale is not present. Hence I had to write this class.
WeatherGod commented on Aug 11, 2015
Ah, I was testing with the master branch, which has it, but is a little bit
different. In any case, I still need an SSCCE, even if it doesn't work for
you. I have no clue what fake data to use in my test. This is what I have
so far:
master...WeatherGod:fix_4900,
but the result looks terrible.
On Tue, Aug 11, 2015 at 1:32 PM, Shubhanshu Mishra <notifications@github.com
napsternxg commented on Aug 11, 2015
This is an SSCE using matplotlib version 1.4.3
Running the above code gives the following error:
UnboundLocalError Traceback (most recent call last) <ipython-input-19-56aae623735e> in <module>() ----> 1 plt.savefig("Logit_check.png") /usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in savefig(*args, **kwargs) 575 def savefig(*args, **kwargs): 576 fig = gcf() --> 577 res = fig.savefig(*args, **kwargs) 578 draw() # need this if 'transparent=True' to reset colors 579 return res /usr/local/lib/python2.7/dist-packages/matplotlib/figure.pyc in savefig(self, *args, **kwargs) 1474 self.set_frameon(frameon) 1475 -> 1476 self.canvas.print_figure(*args, **kwargs) 1477 1478 if frameon: /usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs) 2209 orientation=orientation, 2210 bbox_inches_restore=_bbox_inches_restore, -> 2211 **kwargs) 2212 finally: 2213 if bbox_inches and restore_bbox: /usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs) 519 520 def print_png(self, filename_or_obj, *args, **kwargs): --> 521 FigureCanvasAgg.draw(self) 522 renderer = self.get_renderer() 523 original_dpi = renderer.dpi /usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.pyc in draw(self) 467 468 try: --> 469 self.figure.draw(self.renderer) 470 finally: 471 RendererAgg.lock.release() /usr/local/lib/python2.7/dist-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs) 57 def draw_wrapper(artist, renderer, *args, **kwargs): 58 before(artist, renderer) ---> 59 draw(artist, renderer, *args, **kwargs) 60 after(artist, renderer) 61 /usr/local/lib/python2.7/dist-packages/matplotlib/figure.pyc in draw(self, renderer) 1083 dsu.sort(key=itemgetter(0)) 1084 for zorder, a, func, args in dsu: -> 1085 func(*args) 1086 1087 renderer.close_group('figure') /usr/local/lib/python2.7/dist-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs) 57 def draw_wrapper(artist, renderer, *args, **kwargs): 58 before(artist, renderer) ---> 59 draw(artist, renderer, *args, **kwargs) 60 after(artist, renderer) 61 /usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe) 2004 self.apply_aspect(pos) 2005 else: -> 2006 self.apply_aspect() 2007 2008 artists = [] /usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.pyc in apply_aspect(self, position) 1191 fig_aspect = figH / figW 1192 if self._adjustable in ['box', 'box-forced']: -> 1193 if aspect_scale_mode == "log": 1194 box_aspect = A * self.get_data_ratio_log() 1195 else: UnboundLocalError: local variable 'aspect_scale_mode' referenced before assignmentduncanmmacleod commented on Oct 4, 2017
This still seems to be an issue using matplotlib 2.0.2:
Here the traceback is:
Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 136, in _draw self.figure.draw(renderer) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1144, in draw renderer, self, dsu, self.suppressComposite) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images a.draw(renderer) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2364, in draw self.apply_aspect() File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 1437, in apply_aspect if aspect_scale_mode == "log": UnboundLocalError: local variable 'aspect_scale_mode' referenced before assignmentduncanmmacleod commented on Oct 4, 2017
It seems to me the problem is that the scales are referred by name, rather than by type, e.g.
rather than
I'm not familiar with why the scales are referred to absolutely, rather than relatively, can someone comment?
tacaswell commented on Oct 4, 2017
@phobson probably understands this best right now.
tacaswell commented on Oct 4, 2017
The git blame on that function is all pretty old (2007 - 2010) so the original motivation may be lost to the sands of time.
phobson commented on Oct 9, 2017
@tacaswell getting back from vacation, will take a look at this tomorrow,
jbndlr commented on Aug 23, 2018
Any news on this? Running into the same error when creating custom scales and trying to save the resulting figure.
anntzer commented on Aug 23, 2018
A simple repro is
plot([.1, .9], [.01, .99]); xscale("logit"); yscale("logit"); gca().set_aspect(1)Looks like a correct fix should "simply" be along the lines of
diff --git i/lib/matplotlib/axes/_base.py w/lib/matplotlib/axes/_base.py index 6d7a01cbd..7ecb57c44 100644 --- i/lib/matplotlib/axes/_base.py +++ w/lib/matplotlib/axes/_base.py @@ -1458,23 +1458,15 @@ class _AxesBase(martist.Artist): aspect = self.get_aspect() - if self.name != 'polar': + if self.name == 'polar': + aspect_scale_mode = 'linear' + else: xscale, yscale = self.get_xscale(), self.get_yscale() - if xscale == "linear" and yscale == "linear": - aspect_scale_mode = "linear" - elif xscale == "log" and yscale == "log": - aspect_scale_mode = "log" - elif ((xscale == "linear" and yscale == "log") or - (xscale == "log" and yscale == "linear")): - if aspect != "auto": - warnings.warn( - 'aspect is not supported for Axes with xscale=%s, ' - 'yscale=%s' % (xscale, yscale), stacklevel=2) - aspect = "auto" - else: # some custom projections have their own scales. - pass - else: - aspect_scale_mode = "linear" + if xscale != yscale and aspect != 'auto': + cbook._warn_external('aspect is not supported for Axes with ' + 'different scales (xscale=%s, yscale=%s).' + % (xscale, yscale)) + aspect = 'auto' if aspect == 'auto': self._set_position(position, which='active') @@ -1489,12 +1481,18 @@ class _AxesBase(martist.Artist): fig_aspect = figH / figW if self._adjustable in ['box', 'box-forced']: if self in self._twinned_axes: - raise RuntimeError("Adjustable 'box' is not allowed in a" - " twinned Axes. Use 'datalim' instead.") - if aspect_scale_mode == "log": - box_aspect = A * self.get_data_ratio_log() - else: - box_aspect = A * self.get_data_ratio() + raise RuntimeError("Adjustable 'box' is not allowed in a " + "twinned Axes; use 'datalim' instead") + trf_xmin, trf_xmax = (self.xaxis.get_transform() + .transform(self.get_xbound())) + trf_ymin, trf_ymax = (self.yaxis.get_transform() + .transform(self.get_ybound())) + + xsize = max(abs(trf_xmax - trf_xmin), 1e-30) + ysize = max(abs(trf_ymax - trf_ymin), 1e-30) + + box_aspect = A * ysize / xsize + pb = position.frozen() pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect) self._set_position(pb1.anchored(self.get_anchor(), pb), 'active')@@ -1489,12 +1481,18 @@ class _AxesBase(martist.Artist): fig_aspect = figH / figW if self._adjustable in ['box', 'box-forced']: if self in self._twinned_axes: - raise RuntimeError("Adjustable 'box' is not allowed in a" - " twinned Axes. Use 'datalim' instead.") - if aspect_scale_mode == "log": - box_aspect = A * self.get_data_ratio_log() - else: - box_aspect = A * self.get_data_ratio() + raise RuntimeError("Adjustable 'box' is not allowed in a " + "twinned Axes; use 'datalim' instead") + trf_xmin, trf_xmax = (self.xaxis.get_transform() + .transform(self.get_xbound())) + trf_ymin, trf_ymax = (self.yaxis.get_transform() + .transform(self.get_ybound())) + + xsize = max(abs(trf_xmax - trf_xmin), 1e-30) + ysize = max(abs(trf_ymax - trf_ymin), 1e-30) + + box_aspect = A * ysize / xsize + pb = position.frozen() pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect) self._set_position(pb1.anchored(self.get_anchor(), pb), 'active')and deprecate get_data_ratio/get_data_ratio_log (the fix just applies the actual scale transform to the data limits and computes the ratio from that). Except that this will break custom projections that override get_data_ratio (which is explicitly defined as a overridable method), so we need to figure out how to handle that (perhaps change the behavior of the default get_data_ratio instead to make it do the proper calculation).
Edit (note to self): @efiring suggested we could put this behavior into get_data_ratio so that overriding get_data_ratio still works.
zc62 commented on May 3, 2019
@phobson
Ran into this problem when trying to set equal axis for a symlog-symlog plot. The error occurs at
Any chance for this to get some attention?
It looks like only linear and log scales are supported. Using other scales such as symlog and logit will trigger the bug. (Actually this is an unimplemented feature.)
EDIT: nvm, there is an issue for the feature now.