Python:制作一个跨越两个子情节的图例

Python:制作一个跨越两个子情节的图例,python,matplotlib,subplot,Python,Matplotlib,Subplot,我正在寻找的基本设计是,我有两个并排的散点图,然后我想在两个子图下面创建一个图例,跨越这两个子图。这是一幅草图: 我可以使情节没有问题,但我很难让传奇人物做我想做的事。下面是我的一个代码示例,它制作了两个散点图(我有更多的数据点,但对于空间,我只包括一些): 使图例如上图所示的最佳方式是什么 您想使用() 再看看这个问题的答案 您想使用() 再看看这个问题的答案 从中借用代码,您可以使用以下命令执行此操作: #获取lengend句柄和标签 h1,l1=ax1。获取\u图例\u句柄\u标签()

我正在寻找的基本设计是,我有两个并排的散点图,然后我想在两个子图下面创建一个图例,跨越这两个子图。这是一幅草图:

我可以使情节没有问题,但我很难让传奇人物做我想做的事。下面是我的一个代码示例,它制作了两个散点图(我有更多的数据点,但对于空间,我只包括一些):

使图例如上图所示的最佳方式是什么

您想使用()

再看看这个问题的答案 您想使用()

再看看这个问题的答案

从中借用代码,您可以使用以下命令执行此操作:

#获取lengend句柄和标签 h1,l1=ax1。获取\u图例\u句柄\u标签() h2,l2=ax2。获取\u图例\u句柄\u标签()

其中,ax1和ax2是子批次的轴

在您的示例中,您可以这样实现:

import matplotlib.pyplot as plt
plt.style.use('ggplot')

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]

ax1 = plt.subplot(121)
plt.scatter(x, med1, marker='^',color='black', alpha=0.5, label='Triangle!')
plt.scatter(x, med2, color='blue', alpha=0.5, label='Blue Dot')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('Median')
# ax1 = plt.gca()

ax2 = plt.subplot(122)
plt.scatter(x, iqr1, color='red', alpha=0.5, label='Red Dot')
plt.scatter(x, iqr2, marker='D',color='blue', alpha=0.5, label = 'Diamonds')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('IQR')
# ax2=plt.gca()

plt.tight_layout() # No overlap of subplots

#Get the lengend handles and labels
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()

#Shrink the subplots to make room for the legend
box = ax1.get_position()
ax1.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
box = ax2.get_position()
ax2.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
#Make the legend
ax1.legend(h1+h2, l1+l2,  bbox_to_anchor=(0,-.05, 2.2,-0.15), loc=9,
           ncol=4)
plt.show()
您可以使用
bbox
ncol
,以及
mode=“expand”
borderaxespad=0
。更多信息,请参阅。上述代码应生成此图:

从中借用代码,您可以使用以下命令执行此操作:

#获取lengend句柄和标签 h1,l1=ax1。获取\u图例\u句柄\u标签() h2,l2=ax2。获取\u图例\u句柄\u标签()

其中,ax1和ax2是子批次的轴

在您的示例中,您可以这样实现:

import matplotlib.pyplot as plt
plt.style.use('ggplot')

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]

ax1 = plt.subplot(121)
plt.scatter(x, med1, marker='^',color='black', alpha=0.5, label='Triangle!')
plt.scatter(x, med2, color='blue', alpha=0.5, label='Blue Dot')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('Median')
# ax1 = plt.gca()

ax2 = plt.subplot(122)
plt.scatter(x, iqr1, color='red', alpha=0.5, label='Red Dot')
plt.scatter(x, iqr2, marker='D',color='blue', alpha=0.5, label = 'Diamonds')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('IQR')
# ax2=plt.gca()

plt.tight_layout() # No overlap of subplots

#Get the lengend handles and labels
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()

#Shrink the subplots to make room for the legend
box = ax1.get_position()
ax1.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
box = ax2.get_position()
ax2.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
#Make the legend
ax1.legend(h1+h2, l1+l2,  bbox_to_anchor=(0,-.05, 2.2,-0.15), loc=9,
           ncol=4)
plt.show()
您可以使用
bbox
ncol
,以及
mode=“expand”
borderaxespad=0
。更多信息,请参阅。上述代码应生成此图:


这看起来真不错!为了便于学习,您能否告诉我这部分代码对ax1.legen的作用:h1+h2、l1+l2、bbox_to_anchor=(0、.05、2.2、-0.15)和loc=9?我不知道这些对图例有什么影响,所以我很好奇它们做了什么这看起来真的很好!出于学习的目的,你能告诉我这部分代码对ax1.legen做了什么吗:h1+h2,l1+l2,bbox_to_anchor=(0,-.05,2.2,-0.15)和loc=9?我不确定它们对图例有什么影响,所以我很好奇它们做了什么
#Shrink the subplots to make room for the legend
box = ax1.get_position()
ax1.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
box = ax2.get_position()
ax2.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
#Make the legend
ax1.legend(h1+h2, l1+l2,  bbox_to_anchor=(0,-.05, 2.2,-0.15), loc=9,
           ncol=4)
import matplotlib.pyplot as plt
plt.style.use('ggplot')

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]

ax1 = plt.subplot(121)
plt.scatter(x, med1, marker='^',color='black', alpha=0.5, label='Triangle!')
plt.scatter(x, med2, color='blue', alpha=0.5, label='Blue Dot')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('Median')
# ax1 = plt.gca()

ax2 = plt.subplot(122)
plt.scatter(x, iqr1, color='red', alpha=0.5, label='Red Dot')
plt.scatter(x, iqr2, marker='D',color='blue', alpha=0.5, label = 'Diamonds')
plt.xlim([0,35])
plt.ylim([0,75])
plt.xlabel('Channel Area')
plt.ylabel('IQR')
# ax2=plt.gca()

plt.tight_layout() # No overlap of subplots

#Get the lengend handles and labels
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()

#Shrink the subplots to make room for the legend
box = ax1.get_position()
ax1.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
box = ax2.get_position()
ax2.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
#Make the legend
ax1.legend(h1+h2, l1+l2,  bbox_to_anchor=(0,-.05, 2.2,-0.15), loc=9,
           ncol=4)
plt.show()