Ale*_*ont 13 python matplotlib
我正在运行200次模拟,并将3个输出列表绘制为3行,具有高透明度.这允许我显示模拟之间的差异.
问题是我的图例显示3x200项而不是3项.如何让它一次显示每行的图例?
for simulation in range(200):
plt.plot(num_s_nodes, label="susceptible", color="blue", alpha=0.02)
plt.plot(num_r_nodes, label="recovered", color="green", alpha=0.02)
plt.plot(num_i_nodes, label="infected", color="red", alpha=0.02)
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
beh*_*uri 16
加
plt.plot( ... , label='_nolegend_')
Run Code Online (Sandbox Code Playgroud)
对于您不想在图例中显示的任何绘图.所以在你的代码中你可能会这样做:
..., label='_nolegend_' if simulation else 'susceptible', ...
Run Code Online (Sandbox Code Playgroud)
和其他人类似,或者如果你不喜欢iffy代码:
..., label=simulation and '_nolegend_' or 'susceptible',...
Run Code Online (Sandbox Code Playgroud)
为了避免在绘图中使用额外的逻辑,请使用"代理"艺术家作为图例条目:
# no show lines for you ledgend
plt.plot([], label="susceptible", color="blue", alpha=0.02)
plt.plot([], label="recovered", color="green", alpha=0.02)
plt.plot([], label="infected", color="red", alpha=0.02)
for simulation in range(200):
# your actual lines
plt.plot(num_s_nodes, color="blue", alpha=0.02)
plt.plot(num_r_nodes, color="green", alpha=0.02)
plt.plot(num_i_nodes, color="red", alpha=0.02)
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)