以不同颜色绘制不对称错误条

2024-06-30 16:05:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pandas中有一个数据框,我想用不同的颜色绘制错误条(颜色在“颜色”列中给出)

我正在matplotlib中使用errorbar 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],使用前面代码中相同的名称。(有一个关于如何获取不对称错误here的示例)

执行此操作时,会出现以下错误:

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

我读取了this topic,但是在我的所有数据帧列(4)中有相同数量的元素

我该怎么做才能得到下面相同的图,但有不对称错误

以下是我的完整代码和问题:

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

Tags: theimportdffor错误valbluemin
1条回答
网友
1楼 · 发布于 2024-06-30 16:05:56

出现问题的原因是您正在打印长度为1的数据和错误。首先,让我们看看doc string

xerr, yerr float or array-like, shape(N,) or shape(2, N), optional

The errorbar sizes:

  • scalar: Symmetric +/- values for all data points.
  • shape(N,): Symmetric +/-values for each data point.
  • shape(2, N): Separate - and + values for each bar. First row contains the lower errors, the second row contains the upper errors.
  • None: No errorbar.

当您给出yerr = [min_val, max_val]时,它被解释为一个形状(N,)数组-正如您所看到的,它认为每个点都是对称值。但是由于您只有一个数据点(N=1),所以它会变得混乱

相反,您希望以形状(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='+')

enter image description here

相关问题 更多 >