简体   繁体   English

在图例中绘制代表性的误差线大小?

[英]Plot representative errorbar size in legend?

I have some data (with errors) to be plotted in a rather dense display. 我有一些数据(有错误)要绘制在相当密集的显示中。 I would like to plot these points without errorbars (because it makes it too busy), but to plot a representative error bar in a legend (which shows the errorbar with an accurate size). 我想在没有误差线的情况下绘制这些点(因为它太忙了),但是要在图例中绘出代表性的误差线(以正确的大小显示误差线)。

Here is what I have so far (which is not successful). 这是到目前为止我所拥有的(不成功)。

import pylab as pl
p1, = pl.plot([1,2,3], label="test1")
p2, = pl.plot([3,2,1], label="test2")

errorbarsize = 1.65 # Need this to be properly scaled in the legend

# p3, = pl.plot([1], label='data', color="red") # works
# p3, = pl.scatter(1, 1, label='data', color="red")
# p3, = pl.errorbar(1, 1, yerr=errorbarsize, label='data', color="red")

l1 = pl.legend([p1], ["Label 1"], loc=1)
l2 = pl.legend([p2], ["Label 2"], loc=2) # this removes l1 from the axes.
l3 = pl.legend([p3], ["Label 3"], loc=3, numpoints=1)

gca().add_artist(l1) # add l1 as a separate artist to the axes
gca().add_artist(l2) # add l2 as a separate artist to the axes

Also, it would be best if I could plot this in a separate legend, but that might be asking too much. 另外,最好是将其绘制在单独的图例中,但这可能要求太多。

Here is an example using Thorsten Kranz's suggestion, and another example of how to show representative error bars... 这是一个使用Thorsten Kranz的建议的示例,以及另一个如何显示代表性误差线的示例...

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM