Python 图例中的绘图代表错误条大小?

Python 图例中的绘图代表错误条大小?,python,matplotlib,legend,subplot,Python,Matplotlib,Legend,Subplot,我有一些数据(有错误)要在一个相当密集的显示中绘制。我想在没有错误条的情况下绘制这些点(因为这会使它太忙),但要在图例中绘制一个代表性的错误条(它以精确的大小显示错误条) 以下是我到目前为止所做的(不成功) 另外,如果我能在一个单独的传说中描绘这一点,那将是最好的,但这可能要求太多了 下面是一个使用托尔斯滕·克兰兹建议的示例,以及如何显示代表性误差条的另一个示例 import matplotlib.pyplot as plt import numpy as np fig, axs = plt.

我有一些数据(有错误)要在一个相当密集的显示中绘制。我想在没有错误条的情况下绘制这些点(因为这会使它太忙),但要在图例中绘制一个代表性的错误条(它以精确的大小显示错误条)

以下是我到目前为止所做的(不成功)


另外,如果我能在一个单独的传说中描绘这一点,那将是最好的,但这可能要求太多了

下面是一个使用托尔斯滕·克兰兹建议的示例,以及如何显示代表性误差条的另一个示例

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2,1)

# -- make some fake data
x = np.random.normal(loc=4, size=100)
x.sort()
y = np.random.normal(loc=5, size=100)
y.sort()
y_errorbarsize = y.std()
x_errorbarsize = y.std()

### Example 1
# From Thorsten Kranz comment...
axs[0].plot(x, y, label="Example #1")

axs[0].fill_between(x,y-y_errorbarsize/2, y+y_errorbarsize/2,alpha=0.3)

### Example 2

axs[1].scatter(x, y, label="Example #2", alpha=0.8)

# Place our representative error bar by hand.
axis_coordinates_of_representative_error_bar = (0.1, 0.8)
screen_coordinates_of_representative_error_bar = axs[1].transAxes.transform(axis_coordinates_of_representative_error_bar)
screen_to_data_transform = axs[1].transData.inverted().transform
data_coordinates_of_representative_error_bar = screen_to_data_transform(screen_coordinates_of_representative_error_bar)
foo = data_coordinates_of_representative_error_bar

axs[1].errorbar(foo[0], foo[1], xerr=x_errorbarsize/2, yerr=y_errorbarsize/2, capsize=3)

plt.show()
plt.close()

你看过
errorevery
kwarg
errorbar(…,errorevery=15)
只会每隔15个数据点绘制一个错误条。也许你也可以尝试
填充(x,y-errorbarsize/2,y+errorbarsize/2,alpha=0.3)
import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2,1)

# -- make some fake data
x = np.random.normal(loc=4, size=100)
x.sort()
y = np.random.normal(loc=5, size=100)
y.sort()
y_errorbarsize = y.std()
x_errorbarsize = y.std()

### Example 1
# From Thorsten Kranz comment...
axs[0].plot(x, y, label="Example #1")

axs[0].fill_between(x,y-y_errorbarsize/2, y+y_errorbarsize/2,alpha=0.3)

### Example 2

axs[1].scatter(x, y, label="Example #2", alpha=0.8)

# Place our representative error bar by hand.
axis_coordinates_of_representative_error_bar = (0.1, 0.8)
screen_coordinates_of_representative_error_bar = axs[1].transAxes.transform(axis_coordinates_of_representative_error_bar)
screen_to_data_transform = axs[1].transData.inverted().transform
data_coordinates_of_representative_error_bar = screen_to_data_transform(screen_coordinates_of_representative_error_bar)
foo = data_coordinates_of_representative_error_bar

axs[1].errorbar(foo[0], foo[1], xerr=x_errorbarsize/2, yerr=y_errorbarsize/2, capsize=3)

plt.show()
plt.close()