0

I am trying to put a y-axis on the right and left side of a graph. I am using pandas where I have a data frame take a certain range in an excel sheet and graph it out. The code is able to plot out the three columns that I want vs y however I'm confused on how to get the PM3 scatter plot (ax2) on the right side while keeping the PM1 and AFS scatter plot (ax1 and ax3) on the left. I tried using twinx() and other commands but it doesn't work how I want it. Any suggestions?

enter image description here

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

testproject = r"C:\Users\223070186\Documents\PleaseWork.xlsx"

var = pd.read_excel(testproject, sheet_name ="Test1")


df = pd.DataFrame(var, columns = ["Time", "PM1", "PM3", "AFS"])
df2 = df.iloc[1108:1142, 0:4]


ax1 = df2.plot(kind = "scatter", x = "Time", y = "PM1", color = "r")

ax2 = df2.plot(kind = "scatter", x="Time", y = "PM3", color = "purple", ax =ax1)

ax3 = df2.plot(kind = "scatter", x = "Time", y= "AFS", color = "orange", ax = ax2)

plt.xlabel("Time")

plt.ylabel("PM1, PM3, AFS")

plt.title("Time vs PM1, PM3, AFS splits")

plt.show(ax1 == ax2 == ax3)
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
kingjcp44
  • 3
  • 2
  • Do you mean this type of plot eh? [here](https://www.tutorialspoint.com/how-to-plot-single-data-with-two-y-axes-two-units-in-matplotlib) – Ameya Jun 30 '22 at 15:28
  • This answer shows you how to set up this type of plot (which I don't recommend doing, as a general practice, but you can't find the tide) https://stackoverflow.com/questions/14762181/adding-a-y-axis-label-to-secondary-y-axis-in-matplotlib/14762601#14762601 – Paul H Jun 30 '22 at 15:32
  • Yes something like that but I am trying to keep 2 sets on the left and 1 on the right if that is possible – kingjcp44 Jun 30 '22 at 15:32
  • Also look at the `secondary_y` parameter. – Scott Boston Jun 30 '22 at 15:46

0 Answers0