[英]Plot representative errorbar size in legend?
我有一些數據(有錯誤)要繪制在相當密集的顯示中。 我想在沒有誤差線的情況下繪制這些點(因為它太忙了),但是要在圖例中繪出代表性的誤差線(以正確的大小顯示誤差線)。
這是到目前為止我所擁有的(不成功)。
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
另外,最好是將其繪制在單獨的圖例中,但這可能要求太多。
這是一個使用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.