如何确定matplotlib图例?
How to determine the matplotlib legend?
我有 3 个列表可以绘制成曲线。但每次我 运行 相同的 plt 行时,即使使用 ax.legend(loc='lower right', handles=[line1, line2, line3])
,这 3 个列表也会在图例中随机跳转,如下所示。是否可以修复图例的顺序和颜色以及图中的曲线?
编辑:
我的代码如下:
def plot_with_fixed_list(n, **kwargs):
np.random.seed(0)
fig, ax1 = plt.subplots()
my_handles = []
for key, values in kwargs.items():
value_name = key
temp, = ax1.plot(np.arange(1, n+ 1, 1).tolist(), values, label=value_name)
my_handles.append(temp)
ax1.legend(loc='lower right', handles=my_handles)
ax1.grid(True, which='both')
plt.show()
plot_with_fixed_list(300, FA_Hybrid=fa, BP=bp, Ssym_Hybrid=ssym)
这个不确定性错误存在于 python==3.5,matplotlib==3.0.0。我更新到python==3.6,matplotlib==3.3.2后,问题解决。
我有 3 个列表可以绘制成曲线。但每次我 运行 相同的 plt 行时,即使使用 ax.legend(loc='lower right', handles=[line1, line2, line3])
,这 3 个列表也会在图例中随机跳转,如下所示。是否可以修复图例的顺序和颜色以及图中的曲线?
编辑: 我的代码如下:
def plot_with_fixed_list(n, **kwargs):
np.random.seed(0)
fig, ax1 = plt.subplots()
my_handles = []
for key, values in kwargs.items():
value_name = key
temp, = ax1.plot(np.arange(1, n+ 1, 1).tolist(), values, label=value_name)
my_handles.append(temp)
ax1.legend(loc='lower right', handles=my_handles)
ax1.grid(True, which='both')
plt.show()
plot_with_fixed_list(300, FA_Hybrid=fa, BP=bp, Ssym_Hybrid=ssym)
这个不确定性错误存在于 python==3.5,matplotlib==3.0.0。我更新到python==3.6,matplotlib==3.3.2后,问题解决。