我要找的基本设计是我有两个并排的散点图,然后我想在两个子图下面创建一个图例,跨越它们。这是一个粗略的草图:
我可以使情节没有问题,但我很难让传奇人物做我想做的事。下面是我的代码示例,它生成了两个散点图(我的数据点比这个多,但是对于空间,我只包括几个):
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
x = [5,10,20,30]
med1 = [9.35,15.525,26.1,48.275]
med2 = [8.75,14.025,23.95,41.025]
iqr1 = [13.5125,19.95,38.175,69.9]
iqr2 = [12.05,19.075,35.075,62.875]
plt.subplot(121)
plt.scatter(x, med1, color='red', alpha=0.5, label='Red stuff')
plt.scatter(x, med2, color='blue', alpha=0.5, label='Blue stuff')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('Median')
plt.subplot(122)
plt.scatter(x, iqr1, color='red', alpha=0.5, label='More Red Stuff')
plt.scatter(x, iqr2, color='blue', alpha=0.5, label = 'More Blue Stuff')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('IQR')
什么是最好的方式使传奇出现在上图中?在
从matplotlib - Legend with multiple axes with errorbar object和How to put the legend out of the plot借用代码可以使用以下命令:
#获取长度手柄和标签 h1,l1=ax1.get_legend_handles_labels() h2,l2=ax2.get_legend_handles_labels()
其中ax1和ax2是子批次的轴
在您的示例中,您可以这样实现:
^{pr2}$您可以使用},
bbox
,ncol
,和{borderaxespad=0
。更多信息请参见http://matplotlib.org/users/legend_guide.html#legend-location。上述代码应生成以下绘图:您想使用figlegend(demo)
再看看这个问题的答案 How to position and align a matplotlib figure legend?
相关问题 更多 >
编程相关推荐