Python 如何在matplotlib中使图例错误栏水平

Python 如何在matplotlib中使图例错误栏水平,python,matplotlib,Python,Matplotlib,我想用matplotlib制作一张图,图中的错误条是垂直的, 但图例中的错误栏是水平的。下面的示例代码生成一个 图例中错误条垂直的图形。 如何使图例错误栏水平 代码: 在生成的图形中,我希望图例中的错误条保持水平,而图形其余部分中的错误条保持垂直。我希望这样做,以便图例中的错误栏不会与数据点混淆。如何完成此操作?您可以从默认图例中检索错误条线对象,然后使用它创建自定义图例,并自动水平绘制,如下所示: import numpy as np # v 1.19.2 im

我想用matplotlib制作一张图,图中的错误条是垂直的, 但图例中的错误栏是水平的。下面的示例代码生成一个 图例中错误条垂直的图形。 如何使图例错误栏水平

代码:


在生成的图形中,我希望图例中的错误条保持水平,而图形其余部分中的错误条保持垂直。我希望这样做,以便图例中的错误栏不会与数据点混淆。如何完成此操作?

您可以从默认图例中检索错误条线对象,然后使用它创建自定义图例,并自动水平绘制,如下所示:

import numpy as np                 # v 1.19.2
import matplotlib.pyplot as plt    # v 3.3.2

x = np.linspace(0, 2*np.pi, 6)  
y = np.sin(x)  
dy = 0.1*np.abs(y)

fig, ax = plt.subplots()
plt.errorbar(x, y, yerr=dy, label='data', fmt='o', ecolor='red')

# Retrieve handles and labels: note the tuple within the tuple to
# unpack the handles
(errorbar_container,), labels = ax.get_legend_handles_labels()
point, line = errorbar_container.get_children()

# Create the custom legend: note that the handles are drawn on top of
# one another in the order that they are listed in the tuple
plt.legend([(line, point)], labels, frameon=False)

plt.show()

哪一个是你的主要问题?你应该将每个问题都作为单独的问题发布。不混淆数据点的最简单方法就是保持图例框架处于打开状态……请看,它并不像应该的那样向前,但你可以为艺术家类注册任意处理程序。
import numpy as np                 # v 1.19.2
import matplotlib.pyplot as plt    # v 3.3.2

x = np.linspace(0, 2*np.pi, 6)  
y = np.sin(x)  
dy = 0.1*np.abs(y)

fig, ax = plt.subplots()
plt.errorbar(x, y, yerr=dy, label='data', fmt='o', ecolor='red')

# Retrieve handles and labels: note the tuple within the tuple to
# unpack the handles
(errorbar_container,), labels = ax.get_legend_handles_labels()
point, line = errorbar_container.get_children()

# Create the custom legend: note that the handles are drawn on top of
# one another in the order that they are listed in the tuple
plt.legend([(line, point)], labels, frameon=False)

plt.show()