2

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')
3
  • 2
    plt.plot(np.arange(len(my_mean))+1, my_mean, color='r') and plt.xticks(np.arange(len(label))+1, label) should also let the lineplot start from position 1, just like the boxplots. Note that plt.plot should be called earlier than plt.xticks. If you have plt.boxplot later than plt.plot the labels will be set via the boxplot, so no need to set them again. Commented Feb 16, 2021 at 12:37
  • @JohanC it worked, Shall I answer my question by my own? in order to not become down by the stack Commented Feb 16, 2021 at 12:42
  • For future reference: it would really help for reproducibility if you cleaned up your code and provided a minimum working example. Most of it is not relevant to the question. Commented Feb 16, 2021 at 12:46

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.