使用 twiny 时,Python Matplotlib 图形标题与轴标签重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12750355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Matplotlib figure title overlaps axes label when using twiny
提问by Magic_Matt_Man
I am trying to plot two separate quantities on the same graph using twiny as follows:
我正在尝试使用 twiny 在同一张图上绘制两个单独的数量,如下所示:
fig = figure()
ax = fig.add_subplot(111)
ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')
ax.set_yscale('log')
ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))
ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))
ax.set_xlabel('Rotational period (hrs)')
ax.set_ylabel('Orbital radius (km), logarithmic')
ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')
ax2 = ax.twiny()
ax2.plot(v,r,'k-')
ax2.set_xlabel('Linear speed (ms-1)')
show()
and the data is presented fine, but I am having the problem that the figure title is overlapping with the axes labels on the secondary x axis so that it's barely legible (I wanted to post a picture example here, but I don't have a high enough rep yet).
并且数据显示良好,但我遇到的问题是图形标题与辅助 x 轴上的轴标签重叠,因此几乎难以辨认(我想在此处发布图片示例,但我没有足够高的代表)。
I'd like to know if there's a straightforward way to just shift the title directly up a few tens of pixels, so that the chart looks prettier.
我想知道是否有一种简单的方法可以将标题直接向上移动几十个像素,以便图表看起来更漂亮。
采纳答案by herrlich10
I'm not sure whether it is a new feature in later versions of matplotlib, but at least for 1.3.1, this is simply:
我不确定它是否是 matplotlib 更高版本中的新功能,但至少对于 1.3.1,这只是:
plt.title(figure_title, y=1.08)
This also works for plt.suptitle(), but not (yet) for plt.xlabel(), etc.
这也适用于plt.suptitle(),但(还)不适用于plt.xlabel()等。
回答by Hooked
Forget using plt.titleand place the text directly with plt.text. An over-exaggerated example is given below:
忘记使用plt.title并直接用 放置文本plt.text。下面给出了一个过度夸大的例子:
import pylab as plt
fig = plt.figure(figsize=(5,10))
figure_title = "Normal title"
ax1 = plt.subplot(1,2,1)
plt.title(figure_title, fontsize = 20)
plt.plot([1,2,3],[1,4,9])
figure_title = "Raised title"
ax2 = plt.subplot(1,2,2)
plt.text(0.5, 1.08, figure_title,
horizontalalignment='center',
fontsize=20,
transform = ax2.transAxes)
plt.plot([1,2,3],[1,4,9])
plt.show()


回答by Efkan
ax.set_title('My Title\n', fontsize="15", color="red")
plt.imshow(myfile, origin="upper")
If you put '\n'right after your title string, the plot is drawn just below the title. That might be a fast solution too.
如果您紧跟'\n'在标题字符串之后,则会在标题下方绘制绘图。这也可能是一个快速的解决方案。
回答by jmunsch
I was having an issue with the x-label overlapping a subplot title; this worked for me:
我遇到了 x 标签与子图标题重叠的问题;这对我有用:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1)
ax[0].scatter(...)
ax[1].scatter(...)
plt.tight_layout()
.
.
.
plt.show()
before
前
after
后
reference:
参考:
回答by Peter Zhu
Just use plt.tight_layout()before plt.show(). It works well.
plt.tight_layout()之前就用plt.show()。它运作良好。
回答by Radvin
You can use pad for this case:
您可以在这种情况下使用 pad:
ax.set_title("whatever", pad=20)

