Aligning subplots in pyplot when using line plots over a boxplot

27 views Asked by At

I'm trying to overlay subplots for specific rows in my dataframe as line plots onto a box and whisker plot of the same data. I'm trying to illustrate that my outliers come from two specific rows, where each row is a specific device I have data on. The issue I have is that the subplots are offset by one 'unit' of the x axis label spacings. I've tried sharex=True but this doesn't seem to work - I have a suspicion it might be to do with the rotation of the boxplot, but am stuck. The output plot with lines out by one 'tick' spacing In the plot above the line plots should have all x values relate to each boxplot. Does anyone have an idea what I should try to solve this?

Here's my code to get the plot above:

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


df = pd.read_csv("PlotData2.csv")
df_plot = df.iloc[:, 0:20]
line_plot1 = df_plot.iloc[1]
line_plot2 = df_plot.iloc[0]
fig, ax = plt.subplots()
ax.set_ylim(-3.2, -1.6)

ax = df_plot.boxplot(grid = False, rot=90, showmeans=True, manage_ticks=True)
ax.set(ylabel ='Voltage (V)')
ax.invert_yaxis()
fig.tight_layout()
ax.plot(line_plot1)
ax.plot(line_plot2)
plt.figure()
0

There are 0 answers