ValueError: 'conf_intervals' and 'x' have different lengths

Viewed 29

This is my code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb
import matplotlib.animation as animation


np.random.seed(12345)

df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                   np.random.normal(43000,100000,3650), 
                   np.random.normal(43500,140000,3650), 
                   np.random.normal(48000,70000,3650)], 
                  index=[1992,1993,1994,1995])
plt.rcParams['figure.figsize'] = [12, 10]

df=df.T

for index,column in df.iteritems():
    std=column.std()/np.sqrt(len(column))    _=plt.boxplot(np.array(column),showfliers=False,patch_artist=True,notch=True,conf_intervals=np.array([column+std,column-std]).T)

and I keep getting this error not matter what I do:

ValueError: 'conf_intervals' and 'x' have different lengths

and I checked that they are the same length and tried it with and without changing the type to numpy array

1 Answers

They do not have the same shape: np.array(column).shape is (3650,) while np.array([column+std,column-std]).T.shape is (3650, 2).

A simple patch could be to redefined your x as x = np.array([column, column]). This is the resulting for loop (including some format fixes) and its output:

for index, column in df.iteritems():
    std = column.std() / np.sqrt(len(column))
    x = np.array([column, column])
    conf_intervals = np.array([column + std, column - std]).T
    plt.boxplot(
        x,
        showfliers=False,
        patch_artist=True,
        notch=True,
        conf_intervals=conf_intervals,
    )
plt.show()

enter image description here

Related