Pd.dataframe.plot x matplotlib.pyplot

Hi, I would like to check if the pd.dataframe.plot has or has not the same functionality when it comes to plot two or more lines into the same line chart area. In simple terms, would be possible to do the same thing as in the link below using pd.dataframe.plot?

Screen Link:

https://app.dataquest.io/m/147/improving-plot-aesthetics/4/visualizing-the-gender-gap

Hey @italo.moises

Yes you should be able to do it. Since the pd.DataFrame.plot function returns a matplotlib.axes.Axes object, you can use all functionalities.
You could perhaps try the following :-

woman_degrees.plot(x = 'Year', y = 'Biology', label = 'Women', c = 'blue')

This should work.

Thank you for your reply. However, can I use the method above to address the problem below?

"We can calculate the percentages of Biology degrees awarded to men by subtracting each value in the Biology column from 100"

How can I subtract a value from Biology to draw the second line?

Hey @italo.moises

Ahh!! okay, I understand your question now.
Since the method is DataFrame.plot, the DataFrame supplied becomes the input for the plot function, after which only the column names via strings can be accessed.
Here is a work around for what you want to achieve -

women_degrees[‘Biology_men’] = 100 - women_degrees[‘Biology’]
fig, ax = plt.subplots()
women_degrees.plot(x= ‘Year’, y= ‘Biology’, ax= ax, c= ‘b’, label= ‘Women’)
women_degrees.plot(x= ‘Year’, y= ‘Biology_men’, ax= ax, c= ‘g’, label= ‘Men’)

This will plot two lines on the same figure. Only drawback is that you would have to create a new column in the DataFrame. I know this doesn’t exactly achieve what you wanted to do, but you can try this out once.

Thanks
Raj Tulluri

1 Like

Thank you very much. I’m just exploring the language and just wondered if that could have been done differently.

1 Like