簡體   English   中英

Plot 不對稱誤差條在不同 colors

[英]Plot asymmetric error bars in different colors

I have a data frame in pandas and I would like to plot the error bars in different colors (The colors are given in a column 'Colors').

我在matplotlib中使用錯誤欄 function ,如果錯誤是對稱的,我的代碼可以工作。

這是我的代碼:

import pandas as pd

from matplotlib.pyplot import plot, show, subplots

import numpy as np  

# Definition of the dataframe

df = pd.DataFrame({'Colors': {0: 'Red', 1: 'Blue', 2: 'Green', 3: 'Blue'}, 'X_values': {0: 1, 1: 2, 2: 3, 3: 4}, 'Y_values': {0: 2, 1: 4, 2: 8, 3: 10}, 'MinY_values': {0: 1.5, 1: 3, 2: 7.5, 3: 8}, 'MaxY_values': {0: 2.5, 1: 5, 2: 9.5, 3: 11}})



# Definition of the different colors

color = []
 

for i in df['Colors']:
    if i == 'Red':
        color.append('red')
    if i == 'Blue':
        color.append('blue')
    if i == 'Green':
        color.append('green')   
        
# Figure

fig,axes = subplots(2,1,sharex = True)

for x_val,y_val,min_val,max_val,colors in zip(df['X_values'],df['Y_values'],df['MinY_values'],df['MaxY_values'],color):
    axes[0].errorbar(x_val,y_val,yerr = max_val,color = colors,barsabove='True',fmt = '+')
    
for x_val,y_val,min_val,max_val,colors in zip(df['X_values'],df['Y_values'],df['MinY_values'],df['MaxY_values'],color):
    axes[1].errorbar(x_val,y_val,yerr = max_val,color = colors,barsabove='True',fmt = '+')
    
show()

它返回以下 plot:

陰謀

現在,我有不對稱錯誤,然后在錯誤errorbar中, yerr定義為yerr = [min_val,max_val]使用前面代碼中的相同名稱。 (這里有一個關於如何得到不對稱錯誤例子)

當我這樣做時,會出現以下錯誤:

ValueError: The lengths of the data (1) and the error 2 do not match

我閱讀了這個主題,但我的所有數據框列 (4) 中的元素數量相同。

我該怎么做才能在下面有相同的 plot 但出現不對稱錯誤?

這是我有問題的完整代碼:

import pandas as pd

from matplotlib.pyplot import plot, show, subplots

import numpy as np  

# Definition of the dataframe

df = pd.DataFrame({'Colors': {0: 'Red', 1: 'Blue', 2: 'Green', 3: 'Blue'}, 'X_values': {0: 1, 1: 2, 2: 3, 3: 4}, 'Y_values': {0: 2, 1: 4, 2: 8, 3: 10}, 'MinY_values': {0: 1.5, 1: 3, 2: 7.5, 3: 8}, 'MaxY_values': {0: 2.5, 1: 5, 2: 9.5, 3: 11}})



# Definition of the different colors

color = []
 

for i in df['Colors']:
    if i == 'Red':
        color.append('red')
    if i == 'Blue':
        color.append('blue')
    if i == 'Green':
        color.append('green')   
        
# Figure

fig,axes = subplots(2,1,sharex = True)

for x_val,y_val,min_val,max_val,colors in zip(df['X_values'],df['Y_values'],df['MinY_values'],df['MaxY_values'],color):
    axes[0].errorbar(x_val,y_val,yerr = [min_val,max_val] ,color = colors,barsabove='True',fmt = '+')
    
for x_val,y_val,min_val,max_val,colors in zip(df['X_values'],df['Y_values'],df['MinY_values'],df['MaxY_values'],color):
    axes[1].errorbar(x_val,y_val,yerr = [min_val,max_val] ,color = colors,barsabove='True',fmt = '+')
    
show()

出現問題是因為您正在繪制長度為 1 的數據和錯誤。 首先,讓我們看看文檔字符串

xerryerr浮點數或類似數組,shape(N,) 或 shape(2, N),可選

誤差條大小:

  • 標量:所有數據點的對稱 +/- 值。
  • shape(N,):每個數據點的對稱 +/- 值。
  • shape(2, N):為每個條形分隔 - 和 + 值。 第一行包含較低的錯誤,第二行包含較高的錯誤。
  • 無:沒有錯誤欄。

當您給出yerr = [min_val, max_val]時,它被解釋為一個 shape(N,) 數組 - 正如您所看到的,它認為每個點的對稱值。 但是由於您只有一個數據點( N=1 ),因此會感到困惑。

相反,您希望將錯誤作為 shape(2, N) 列表或數組。 例如:

yerr=[[min_val], [max_val]]

在您對errorbar的一次調用中看起來像這樣:

for x_val, y_val, min_val, max_val, colors in zip(df['X_values'], df['Y_values'], df['MinY_values'], df['MaxY_values'], color):
    axes[0].errorbar(x_val, y_val, yerr=[[min_val], [max_val]], color=colors, barsabove='True', fmt='+')

在此處輸入圖像描述

暫無
暫無

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

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