Plot 95% confidence interval errorbar Python Pandas dataframes in Matplotlib


To plot 95% confidence interval errorbar Python Pandas dataframes, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Get a dataframe instance of two-dimensional, size-mutable, potentially heterogeneous tabular data.
  • Make a dataframe with two columns, category and number.
  • Find the mean and std of category and number.
  • Plot y versus x as lines and/or markers with attached errorbars.
  • To display the figure, use show() method.

Example

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame()
df['category'] = np.random.choice(np.arange(10), 1000, replace=True)
df['number'] = np.random.normal(df['category'], 1)

mean = df.groupby('category')['number'].mean()
std = df.groupby('category')['number'].std()
plt.errorbar(mean.index, mean, xerr=0.5, yerr=2*std,
               linestyle='--', c='red')

plt.show()

Output

Updated on: 08-Jul-2021

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements