如何为每个子图添加标题
- 2025-02-25 09:07:00
- admin 原创
- 19
问题描述:
我有一个包含许多子图的图。
fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k')
fig.canvas.set_window_title('Window Title')
# Returns the Axes instance
ax = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)
如何为子图添加标题?
fig.suptitle
为所有图表添加标题,尽管ax.set_title()
存在,但后者并没有为我的子图添加任何标题。
感谢您的帮助。
编辑:更正了有关 的拼写错误set_title()
。谢谢鲁特格·卡西斯
解决方案 1:
ax.title.set_text('My Plot Title')
似乎也有效。
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()
解决方案 2:
ax.set_title()
应该为单独的子图设置标题:
import matplotlib.pyplot as plt
data = [1, 2, 3, 4, 5]
fig = plt.figure()
fig.suptitle("Title for whole figure", fontsize=16)
ax = plt.subplot(2, 1, 1)
ax.set_title("Title for first plot")
ax.plot(data)
ax = plt.subplot(2, 1, 2)
ax.set_title("Title for second plot")
ax.plot(data)
plt.show()
您能检查一下此代码是否适合您吗?也许稍后会有东西覆盖它们?
解决方案 3:
简写答案假设 import matplotlib.pyplot as plt
:
plt.gca().set_title('title')
例如:
plt.subplot(221)
plt.gca().set_title('title')
plt.subplot(222)
etc...
那么就不需要多余的变量了。
解决方案 4:
如果你想让它更短,你可以写:
import matplotlib.pyplot as plt
for i in range(4):
plt.subplot(2,2,i+1).set_title(f'Subplot n°{i+1}')
plt.show()
这可能会使它不太清楚,但你不需要更多的行或变量
解决方案 5:
我越来越倾向于使用这个解决方案:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2) # 1
for i, ax in enumerate(axs.ravel()): # 2
ax.set_title("Plot #{}".format(i)) # 3
创建任意数量的轴
axs.ravel() 将二维对象转换为以行为主样式的 1 维向量
将标题分配给当前轴对象
解决方案 6:
为了完整性,无需明确参考图轴也可以实现所请求的结果,如下所示:
import matplotlib.pyplot as plt
plt.subplot(221)
plt.title("Title 1")
plt.subplot(222)
plt.title("Title 2")
plt.subplot(223)
plt.title("Title 3")
plt.subplot(224)
plt.title("Title 4")
plt.tight_layout()
如果标签重叠问题,请在最后一个图之后使用。
解决方案 7:
fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1, ncols=4,figsize=(11, 7))
grid = plt.GridSpec(2, 2, wspace=0.2, hspace=0.5)
ax1 = plt.subplot(grid[0, 0])
ax2 = plt.subplot(grid[0, 1:])
ax3 = plt.subplot(grid[1, :1])
ax4 = plt.subplot(grid[1, 1:])
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()
解决方案 8:
如果您有多张图片,并且想要循环显示它们并逐张显示标题,您可以这样做。无需明确定义 ax1、ax2 等。
问题是您可以像代码第 1 行那样定义动态轴 (ax),并且可以在循环内设置其标题。
二维数组的行是轴(ax)的长度(len)
每行有 2 个项目,即它是列表中的列表(要点 2)
一旦选择了适当的轴 (ax) 或子图,就可以使用 set_title 来设置标题。
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(6, 8))
for i in range(len(ax)):
for j in range(len(ax[i])):
## ax[i,j].imshow(test_images_gr[0].reshape(28,28))
ax[i,j].set_title('Title-' + str(i) + str(j))
解决方案 9:
您仅可以通过迭代就可以为每个图表赋予不同的标题和标签。
titles = {221: 'First Plot', 222: 'Second Plot', 223: 'Third Plot', 224: 'Fourth Plot'}
fig = plt.figure()
for x in range(221,225):
ax = fig.add_subplot(x)
ax.title.set_text(titles.get(x))
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.9,
top=0.9,
wspace=0.4,
hspace=0.4)
plt.show()
输出:
解决方案 10:
从 matplotlib 3.4.3 开始,Figure.add_subplot函数支持带有标题的 kwargs,如下所示:
fig.add_subplot(311, title="first")
fig.add_subplot(312, title="second")
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD