簡體   English   中英

使用errorbar繪制單個點的非對稱誤差線

[英]Plotting asymmetric error bars for a single point using errorbar

目標:使用errorbar繪制單個點的非對稱x誤差線。 我想顯示數據集的四分位數范圍(IQR)。

碼:

import numpy as np
import matplotlib.pyplot as plt

y = 1.0
data = np.random.rand(100)

median = np.median(data)
upper_quartile = np.percentile(data, 75)
lower_quartile = np.percentile(data, 25)
IQR = upper_quartile - lower_quartile

plt.errorbar(median, y, xerr=[lower_quartile ,upper_quartile], fmt='k--')

plt.savefig('IQR.eps')
plt.show()

錯誤:

Traceback (most recent call last):
  File "IQR.py", line 15, in <module>
    plt.errorbar(median, y, xerr=[0.5,0.75], fmt='k--')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2251, in errorbar
    ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 5327, in errorbar
    in cbook.safezip(x,xerr)]
  File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1294, in safezip
    raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))
ValueError: In safezip, len(args[0])=1 but len(args[1])=2

我的問題是我無法為單個點創建非對稱誤差線,其中該點表示平均值,誤差線的上限和下限將是上下四分位數。

我通常使用vlineshlines (我認為大寫只是分散注意力):

 plt.hlines( y, median-lower_quartile, median+upper_quartile)
 plt.plot(median, y, 'o')

簡單的情節

如果您仍想使用errorbar ,可以嘗試

plt.errorbar(median, y, xerr=np.array([[lower_quartile ,upper_quartile]]).T, 
        fmt='ko')

帶有錯誤欄

請注意,我真的不知道你如何在這里定義四分位數,所以你可能需要確保你得到正確的數字!!!

確保xerr獲取列表列表。 如果它只是一個列表,它將假設它包含兩個Y的對稱誤差條。 但是只有一個Y,這就是你得到錯誤的原因。

您的錯誤欄也是錯誤的。 將錯誤欄調用更改為

plt.errorbar(median, y, xerr=[[median-lower_quartile ,upper_quartile-median]], fmt='k--')

您傳遞給safezip的兩個參數大小不同。 您發布的堆棧跟蹤就在這里說:

ValueError: In safezip, len(args[0])=1 but len(args[1])=2

這就是說一個參數的長度為1但參數二的長度為2,所以zip實際上並不能將這兩個列表組合在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM