对于这样的情节:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True)
ax1.plot(x, y)
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')
如何为一行添加字幕?.它应该看起来像这样:
我用 photoshop 制作了“Title1”和“Title2”,如何将它们添加到 python 中的绘图中?
最佳答案
要为带有子图的图制作标题,matplotlib 有 pyplot.suptitle
.由于每个图形只能有一个 suptitle
,如果你想有两行图形,它不能解决问题。
使用 plt.text()
可以将文本设置为轴,这里也不需要,所以我建议使用 plt.figtext
然后可能需要使用 plt.subplots_adjust(hspace = 0.3 )
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True)
ax1.plot(x, y)
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')
plt.figtext(0.5,0.95, "A tremendously long title that wouldn't fit above a single figure", ha="center", va="top", fontsize=14, color="r")
plt.figtext(0.5,0.5, "Yet another multi-worded title that needs some space", ha="center", va="top", fontsize=14, color="r")
plt.subplots_adjust(hspace = 0.3 )
plt.savefig(__file__+".png")
plt.show()
关于python - 为情节的行添加字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788403/