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