简体   繁体   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'). 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').

I'm using the errorbar function in matplotlib and my code works if the error is symmetric.我在matplotlib中使用错误栏 function ,如果错误是对称的,我的代码可以工作。

Here is my code:这是我的代码:

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

It returns the following plot:它返回以下 plot:

阴谋

Now, I have asymmetric errors, then in the errorbar function, yerr should be define as yerr = [min_val,max_val] using the same names in the precedent code.现在,我有不对称错误,然后在错误errorbar中, yerr定义为yerr = [min_val,max_val]使用前面代码中的相同名称。 (There is an example on how to get asymmetric errors here ) (这里有一个关于如何得到不对称错误例子)

When I do that, the following error appears:当我这样做时,会出现以下错误:

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

I read this topic , but there is the same number of elements in all my data frame columns (4).我阅读了这个主题,但我的所有数据框列 (4) 中的元素数量相同。

What can I do to have the same plot below but with the asymmetric errors?我该怎么做才能在下面有相同的 plot 但出现不对称错误?

Here is my complete code with issue:这是我有问题的完整代码:

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

The problem arises because you are plotting length-1 data and errors.出现问题是因为您正在绘制长度为 1 的数据和错误。 First, let's see the doc string :首先,让我们看看文档字符串

xerr , yerr float or array-like, shape(N,) or shape(2, N), optional xerryerr浮点数或类似数组,shape(N,) 或 shape(2, N),可选

The errorbar sizes:误差条大小:

  • scalar: Symmetric +/- values for all data points.标量:所有数据点的对称 +/- 值。
  • shape(N,): Symmetric +/-values for each data point. shape(N,):每个数据点的对称 +/- 值。
  • shape(2, N): Separate - and + values for each bar. shape(2, N):为每个条形分隔 - 和 + 值。 First row contains the lower errors, the second row contains the upper errors.第一行包含较低的错误,第二行包含较高的错误。
  • None: No errorbar.无:没有错误栏。

When you give yerr = [min_val, max_val] , it is interpreted as a shape(N,) array - which as you can see, it then thinks are symmetric values for each point.当您给出yerr = [min_val, max_val]时,它被解释为一个 shape(N,) 数组 - 正如您所看到的,它认为每个点的对称值。 But since you only have one data point ( N=1 ), it gets confused.但是由于您只有一个数据点( N=1 ),因此会感到困惑。

Instead, you want to give your errors as a shape(2, N) list or array.相反,您希望将错误作为 shape(2, N) 列表或数组。 For example:例如:

yerr=[[min_val], [max_val]]

Which looks like this in one of your calls to errorbar :在您对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