john22
john22

Reputation: 395

How to overlay a boxplot and a lineplot?

I have created a box plot. Then I wanted to draw a line that represents the mean value of each box-plot. I am dealing with a problem of not overlaying. The line plot starts from a point earlier than the box-plot:

enter image description here

The red line should be moved one point further. My code:

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib qt


my_list=[]
label=[]
filenames = [f for f in sorted(os.listdir('.')) if f.endswith('.dat')]
my_mean = []
for filename in filenames:
#     df = pd.read_csv('fras2009_flat_r9_parameters.dat', sep="\s+", header=None)
    df=pd.read_csv(filename,sep="\s+", header=None)

    
    beta = df[4]
    beta = beta.drop(beta.index[0:100])
    beta_1 = pd.to_numeric(beta, errors='coerce')
    my_list.append(beta_1)
    mean = beta_1.mean()
    my_mean.append(mean)
    

#     mean.index = np.arange(1,len(mean)+1)
    #label.append(filename)
labels = ['C_r 03','C_r 05','C_r 0.1','C_r 0.2','C_r 0.5','C_r 1','C_r 2','Unconfined']

plt.xticks(np.arange(len(label)),label)
plt.boxplot(my_list,labels=labels)
# _, ax = plt.subplots()
# # mean.plot(ax=ax)
plt.plot(my_mean,color='r')

Upvotes: 2

Views: 5905

Answers (1)

JohanC
JohanC

Reputation: 80449

The boxplots are by default indexed starting from 1. This indexing could be changed by using positions=.... Or you could just start the lineplot one position further. If you call boxplot later than lineplot, it will set the correct tick labels.

import numpy as np
import matplotlib.pyplot as plt

labels = ['C_r 03', 'C_r 05', 'C_r 0.1', 'C_r 0.2', 'C_r 0.5', 'C_r 1', 'C_r 2', 'Unconfined']

my_list = [np.random.uniform(10, 30, 5) for _ in labels]
my_mean = [values.mean() for values in my_list]

plt.plot(np.arange(len(my_mean)) + 1, my_mean, color='r')
plt.boxplot(my_list, labels=labels)
plt.show()

resulting plot

Upvotes: 2

Related Questions