【发布时间】:2020-11-26 07:14:29
【问题描述】:
我有一个具有辅助轴的图。轴 1 绘制了两个数据集。轴 2 有一个数据集。 我可以得到两个图例(一个来自 Axis 1,一个来自 Axis 2),就像我想要的那样 - 一个在右侧的绘图之外的另一个下方。
我希望轴 1 的第二个数据集的图例低于上述两个图例。但它出现在两者之外。
我怎样才能让它工作?
下面是我的代码:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-',label='data1')
ax1.set_xlabel('time (s)')
ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.7), ncol=2,
borderaxespad=0, frameon=False)
ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r',label='data2')
ax2.legend(loc='lower left', bbox_to_anchor= (1.1, 0.6), ncol=2,
borderaxespad=0, frameon=False)
data3 = [10000]*len(t)
ax1.plot(t,data3,'k--',label='data3')
ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.5), ncol=2,
borderaxespad=0, frameon=False)
plt.show()
当我更改 bbox_to_anchor 的 y 值时,“data3”不会出现在与其他两个图例一起出现的列中,而是与两个图例中的任何一个一起显示在一行中。
谢谢
R
【问题讨论】:
标签: python matplotlib legend