[英]global legend for all subplots
我创建了一个Matplot子图的nxn矩阵,其中包含相同类型的曲线(将它们命名为signal1和signal2):
n=5
f, axarr = plt.subplots(n,n)
for i,signal_generator in enumerate(signal_generators):
y=i%n
x=(i-y)/n
axarr[x, y].plot(signal_generator.signal1)
axarr[x, y].plot(signal_generator.signal2)
由于每个子图中的2个信号分别表示相同的类型,因此我想将图形全局图例与两个条目“ signal1”和“ signal2”一起使用,而不是将相同的图例附加到每个子图中。
我该怎么做?
一种方法是在绘图下方施加一些额外的空间。 然后,您可以在此处安装图例,并拥有一个“全局”图例。
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
fig, axlist = plt.subplots(3, 3)
for ax in axlist.flatten():
line1, = ax.plot(np.random.random(100), label='data1')
line2, = ax.plot(np.random.random(100), label='data2')
line3, = ax.plot(np.random.random(100), 'o', label='data3')
fig.subplots_adjust(top=0.9, left=0.1, right=0.9, bottom=0.12) # create some space below the plots by increasing the bottom-value
axlist.flatten()[-2].legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), ncol=3)
# it would of course be better with a nicer handle to the middle-bottom axis object, but since I know it is the second last one in my 3 x 3 grid...
fig.show()
由于bbox_to_anchor=(x, y)
的y值为y bbox_to_anchor=(x, y)
现在在倒数第二个(底部中间)轴区域下方将有一个标签。 根据您拥有多少个不同的子图,以及在每个子图中绘制多少条不同的线,最好更好地跟踪不同的线对象。 也许将它们附加到列表中。
对我来说,输出图看起来像
这是否给您想要的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.