[英]errorbar, but not line, as marker symbol in python matplotlib legend
我有一个误差线图,每个数据集只有一个数据点(即一个误差线)。 因此,我也想在图例中使用单个errorbar符号。 可以通过legend(numpoints=1)
来实现。 在以下代码中使用它:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker line')
ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is too long')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.legend(numpoints=1) # I want only one symbol
plt.show()
如您所见,误差线与水平线混合在一起,当要连接的误差线不止一个时(使用legend(numpoints=2)
或更高),这是有意义的,但在我的情况下看起来很难看。
如何摆脱图例标记中的线条而又不丢失错误栏?
这是由于matplotlib中的默认设置。 在代码的开头,您可以通过使用rcParams
更改设置来更改它们:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['legend.handlelength'] = 0
mpl.rcParams['legend.markerscale'] = 0
fig, ax = plt.subplots()
ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker')
ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is gone')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.legend(numpoints=1)
plt.show()
注意:这将更改将在代码中绘制的所有图形的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.