![](/img/trans.png)
[英]Defining xlabels with set() and changing bottom arg inside subplots_adjust() matplotlib will modify xlabels order
[英]Matplotlib subplots_adjust hspace so titles and xlabels don't overlap?
例如,在matplotlib中有3行子圖, xlabels
可以與下一行的標題重疊。 一個人必須擺弄pl.subplots_adjust(hspace)
,這很煩人。
是否有hspace
的配方可以防止重疊並適用於任何nrow?
""" matplotlib xlabels overlap titles ? """
import sys
import numpy as np
import pylab as pl
nrow = 3
hspace = .4 # of plot height, titles and xlabels both fall within this ??
exec "\n".join( sys.argv[1:] ) # nrow= ...
y = np.arange(10)
pl.subplots_adjust( hspace=hspace )
for jrow in range( 1, nrow+1 ):
pl.subplot( nrow, 1, jrow )
pl.plot( y**jrow )
pl.title( 5 * ("title %d " % jrow) )
pl.xlabel( 5 * ("xlabel %d " % jrow) )
pl.show()
我的版本:
Qt4Agg
( TkAgg
=> Tkinter回調中的異常) (對於許多額外的點,任何人都可以概述matplotlib的打包器/墊片如何工作,沿着Tcl / Tk書中第17章“打包者”的路線?)
您可以使用plt.subplots_adjust來更改子圖Link之間的間距
subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots
hspace = 0.2 # the amount of height reserved for white space between subplots
Jose發布的鏈接已更新,pylab現在有一個tight_layout()
函數,可自動執行此操作(在matplotlib版本1.1.0中)。
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.tight_layout
http://matplotlib.org/users/tight_layout_guide.html#plotting-guide-tight-layout
我發現這很棘手,但在MatPlotLib常見問題解答中有一些信息。 它相當麻煩,需要了解單個元素(ticklabels)占用的空間......
更新:該頁面指出tight_layout()
函數是最簡單的方法,它試圖自動糾正間距。
否則,它會顯示獲取各種元素(例如標簽)大小的方法,以便您可以更正軸元素的間距/位置。 以下是上述FAQ頁面中的示例,該頁面確定了非常寬的y軸標簽的寬度,並相應地調整軸寬度:
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
def on_draw(event):
bboxes = []
for label in labels:
bbox = label.get_window_extent()
# the figure transform goes from relative coords->pixels and we
# want the inverse of that
bboxi = bbox.inverse_transformed(fig.transFigure)
bboxes.append(bboxi)
# this is the bbox that bounds all the bboxes, again in relative
# figure coords
bbox = mtransforms.Bbox.union(bboxes)
if fig.subplotpars.left < bbox.width:
# we need to move it over
fig.subplots_adjust(left=1.1*bbox.width) # pad a little
fig.canvas.draw()
return False
fig.canvas.mpl_connect('draw_event', on_draw)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.