python - 为情节的行添加字幕

标签 python matplotlib

对于这样的情节:

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')

如何为一行添加字幕?.它应该看起来像这样:

enter image description here

我用 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()

enter image description here

关于python - 为情节的行添加字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788403/

相关文章:

python - 绘制多边形时,如何在 Matplotlib 中将颜色值分配给顶点而不是面颜色?

python - 具有描述符和属性对象的属性查找优先级

python - 如何在Tensorflow中计算矩阵乘积的对角线?

python - 如何使其更具 Python 风格或 future 更具可读性?

python - 科学 'Minimize the sum of squares of a set of equations'

python - 如何使用python和openCV制作热图

Python Plotly : Percentage Axis Formatter

matplotlib - 以最大宽度保持等比例显示图像

python - Matplotlib : Can't create log plots

python - 如何制作表分区?