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:
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')


plt.plot(np.arange(len(my_mean))+1, my_mean, color='r')andplt.xticks(np.arange(len(label))+1, label)should also let the lineplot start from position1, just like the boxplots. Note thatplt.plotshould be called earlier thanplt.xticks. If you haveplt.boxplotlater thanplt.plotthe labels will be set via the boxplot, so no need to set them again.