matplotlib sub的行标题

2024-05-23 17:45:24 发布

您现在位置:Python中文网/ 问答频道 /正文

在matplotlib中,除了为整个图形设置标题和为每个单独的绘图设置标题之外,还可以为每行子绘图设置单独的标题吗?这将对应于下图中的橙色文本。 enter image description here

如果没有,你会如何解决这个问题?在左侧创建一个单独的空子块列,并用橙色文本填充它们?

我知道可以使用text()annotate()手动定位每个标题,但这通常需要很多调整,而且我有许多子块。有更平滑的解决方案吗?


Tags: text定位文本图形绘图标题matplotlib手动
1条回答
网友
1楼 · 发布于 2024-05-23 17:45:24

一个想法是创建三个“大的子块”,给每个子块一个标题,让它们不可见。在此基础上,你可以创建更小的子块矩阵。

这个解决方案完全基于this post,只是更多的关注是实际删除背景子块。

enter image description here

import matplotlib.pyplot as plt

fig, big_axes = plt.subplots( figsize=(15.0, 15.0) , nrows=3, ncols=1, sharey=True) 

for row, big_ax in enumerate(big_axes, start=1):
    big_ax.set_title("Subplot row %s \n" % row, fontsize=16)

    # Turn off axis lines and ticks of the big subplot 
    # obs alpha is 0 in RGBA string!
    big_ax.tick_params(labelcolor=(1.,1.,1., 0.0), top='off', bottom='off', left='off', right='off')
    # removes the white frame
    big_ax._frameon = False


for i in range(1,10):
    ax = fig.add_subplot(3,3,i)
    ax.set_title('Plot title ' + str(i))

fig.set_facecolor('w')
plt.tight_layout()
plt.show()

相关问题 更多 >