0

After importing the laptop_defect file, I plotted 3 graphs for 1) Defects over time 2) Opportunities over time 3) defect rate with mean rate and limits over time. I was wondering how can I combine all 3 graphs in 1 figure?

Here's what I have so far:

import pandas as pd
laptop_defects = pd.read_excel('/content/Data_Analysis_Project.xlsx')
laptop_defects
import matplotlib.pyplot as plt
plt.plot(laptop_defects.Defects)
plt.plot(laptop_defects.Opportunities)

Calculate Defect Rate

laptop_defects['Defect Rate'] = round(laptop_defects['Defects']/ laptop_defects['Opportunities'] * 1000000,0)
laptop_defects.head()

plt.plot(laptop_defects ['Month ID(YYYYMM)'], laptop_defects['Defect Rate']);
plt.plot(laptop_defects ['Month ID(YYYYMM)'], laptop_defects['Mean Rate']);
plt.plot(laptop_defects ['Month ID(YYYYMM)'], laptop_defects['2 Sigma limit']);
plt.plot(laptop_defects ['Month ID(YYYYMM)'], laptop_defects['3 Sigma limit']);

Defects Graph

Opportunities Graph

Defect rate with mean rate and limits over time

Alice T
  • 25
  • 4
  • You can try this: `fig, [ax1, ax2, ax3] = plt.subplots(nrows=3, ncols=1)`, then supply each plot command to the appropriate axis. – K.Cl Jun 14 '22 at 20:31
  • Which part of the [Chart Visualization](https://pandas.pydata.org/docs/user_guide/visualization.html) section of the Pandas User Guide didn't you understand? I think maybe the second example shows what you want. – wwii Jun 14 '22 at 20:32
  • If one of your columns is a datetime Series you might want to set it as the index before you plot the DataFrame. – wwii Jun 14 '22 at 20:37
  • @wwii I think I'm confused about the axis part, each of the graph has individual (x, y) axis, how can I stack them together? – Alice T Jun 14 '22 at 20:40
  • Can't you also plot the 'Defects' and 'Opportunities' columns against the 'Month ID(YYYYMM)' column? You should provide a minimal example of the DataFrame, `laptop_defects`. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jun 14 '22 at 20:47
  • 1
    Try `ax = laptop_defects.plot(x='Month ID(YYYYMM)', y=['Defects','Opportunities','Defect Rate','Mean Rate','2 Sigma limit','3 Sigma limit'])` – wwii Jun 14 '22 at 20:56
  • @wwii, thank you for your help, got it! Really new to this, do you have any resources where I can look into futher? – Alice T Jun 14 '22 at 21:37
  • 1
    The [Pandas User Guide](https://pandas.pydata.org/docs/user_guide/index.html) - start at the beginning and work through the examples - at least those first three sections anyway - 10 minutes to pandas, Intro to data structures, and Essential basic functionality. – wwii Jun 14 '22 at 21:45

0 Answers0