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.
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:
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. (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).
What can I do to have the same plot below but with the asymmetric errors?
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. First, let's see the doc string :
xerr
,yerr
float or array-like, shape(N,) or shape(2, N), optionalThe 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.
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. But since you only have one data point ( N=1
), it gets confused.
Instead, you want to give your errors as a shape(2, N) list or array. For example:
yerr=[[min_val], [max_val]]
Which looks like this in one of your calls to 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='+')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.