【发布时间】:2018-09-12 02:10:14
【问题描述】:
我正在寻找一个优雅的解决方案,使用 matplotlib 在每个子图的角上方放置一个图形标签(A、B、C),但每个标签与子图轴角的距离完全相同。我遇到的问题是典型的标签解决方案利用了轴分数-因此很容易将 A、B、C 放置在相对于每个图的相同位置。
import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2,2, figsize = (10,10))
texts = ['A', 'B', 'C', 'D']
axes = fig.get_axes()
for a,l in zip(axes, texts):
a.annotate(l, xy=(-0.1, 1.1), xycoords="axes fraction", fontsize=15, weight = 'bold')
plt.suptitle('Label_Distance Consistent', fontsize = 20)
但是,如果图的大小不同,您将获得远离图角的标签(由于纵横比)。例如,参见标签 A 和 C。我正在寻找一种好方法来确保包含多个尺寸/纵横比子图的面板的标签与轴角的比例距离,和/或显式设置文本与轴的特定距离(以英寸或图形坐标单位为单位)角落。
过去我在面板中每个子图轴的角落放置了相同大小的方轴,使它们不可见,并将文本缩放到这些,但这是一种复杂的方法。
fig, ax = plt.subplots(2,2, figsize = (10,10))
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
axes = fig.get_axes()
texts = ['A', 'B', 'C', 'D', 'E']
for a,l in zip(axes, texts):
a.annotate(l, xy=(-0.1, 1.1), xycoords="axes fraction", fontsize=15, weight = 'bold')
plt.suptitle('Label Distance Inconsistent', fontsize = 20)
【问题讨论】:
标签: python matplotlib annotate