python - How to disable bbox_inches='tight' when working with matplotlib inline in ipython notebook -
python - How to disable bbox_inches='tight' when working with matplotlib inline in ipython notebook -
when work matplotlib inline backend in ipython notebook, default behavior using bbox_inches='tight' generate embedded png image internally via savefig(). eliminates whitespace around axes , great in cases.
however, 1 might want (temporarily) disable feature, example, when (s)he wants manually maintain 2 figures vertically aligned (assume don't want utilize subplot here):
%matplotlib inline pylab import * plot(rand(100)) subplots_adjust(left=0.2) # has no effect inline, works expected qt figure() plot(rand(100)*10000) # result in larger left margin figure... subplots_adjust(left=0.2)
so how disable behavior? thanks~
editto create issue involved here more explicit (thanks anzel), 2nd figure, due more digits displayed in yticklabels, have larger left margin (and smaller right margin) after automatic layout adjustment triggered bbox_inches='tight' alternative in savefig(), internally called notebook generate embedded png output. truncate additional space intentionally create subplots_adjust(), 2nd figure seem shifted right, , not vertically "aligned" 1st figure.
it easy see mean---just seek code snippet above:)
the reason why i'm not using subplot/subplots here (see comments anzel's answer) in particular case, 2 figures acutally composed of tens of little subplots, plus additional formatting/labeling. merging them 1 larger array of subplots not trivial...
you may utilize pyplot.subplots
align plots in grid order, figures visually aligned in notebook (if that's want?)
something this:
%matplotlib inline import matplotlib.pyplot plt import numpy np d1 = np.random.rand(100) d2 = np.random.rand(100)*10000 fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) plt.subplots_adjust(left=0.2) ax1.plot(d1) ax2.plot(d2)
updates as op's requirements utilize separate plots rather subplots, here hacky solution. working on notebook, more details customization can found here.
import matplotlib.pyplot plt import numpy np %matplotlib inline # override ytick.major.width before plot plt.rcparams['ytick.major.pad'] = 20 plt.plot(np.random.rand(100)) # override set alignment plot plt.rcparams['ytick.major.pad'] = 5 plt.figure() plt.plot(np.random.rand(100)*10000)
# plt.rcdefaults() reset defaults doc says.
not elegant way it's working required.
python matplotlib ipython ipython-notebook
Comments
Post a Comment