Screen Link:
https://app.dataquest.io/m/523/pandas-visualizations-and-grid-charts/11/grid-charts-ii
My Code:
import pandas as pd
import matplotlib.pyplot as plt
traffic = pd.read_csv('traffic_sao_paulo.csv', sep=';')
traffic['Slowness in traffic (%)'] = traffic['Slowness in traffic (%)'].str.replace(',', '.')
traffic['Slowness in traffic (%)'] = traffic['Slowness in traffic (%)'].astype(float)
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
traffic_per_day = {}
for i, day in zip(range(0, 135, 27), days):
each_day_traffic = traffic[i:i+27]
traffic_per_day[day] = each_day_traffic
plt.figure(figsize=(10,12))
for i, day in zip(range(1,6), days):
plt.subplot(3, 2, i)
**traffic_per_day[day].plot.line(x='Hour (Coded)',y='Slowness in traffic (%)')**
plt.title(day)
plt.ylim([0,25])
plt.show()
I am confused why the line :
plt.plot(traffic_per_day[day][âHour (Coded)â],traffic_per_day[day][âSlowness in traffic (%)â])
( plt.plot() )
works fine here but my code in the form: Dataframe.plot.line() does not work??
Hi @bhumikagupta100366 and welcome to the community!
Itâs early Saturday morning and I havenât had my morning coffee yet so please forgive my fogginess!
I think it should be possible to use either form but since one is using pyplot and the other is using pandas (which, technically is also using pyplot behind the scenes) their implementations are slightly different and therefore canât simply be swapped one for the other as is. I think it has to do with how each handles creating graph objects (specifically: fig
and ax
objects) behind the scenes when you call .plot()
but Iâm not 100% (hmmmmâŚcoffee!). The pandas version is really just a shortcut and doesnât have the âfull customizationâ that you have when using pyplot directly.
I found this article on SO that hints that this is the case.
My apologies for not providing a more complete answer with examplesâŚmy brains just arenât online! 
EDIT_1:
Also, check out this article from the community with a similar question.
EDIT_2:
OkâŚalmost done my 1st cup of coffee and this will be my last edit to the postâŚI swear! This article does a good job of explaining (with examples) how pandas deals with graphing objects and could shed some light on how to implement the pandas version of plotting a graph.
2 Likes
Now that I am actually doing these missions (and itâs not early Saturday morningâŚ) I can better answer your question as to why plt.plot()
works but df.plot.line()
will not.
This little block of text on screen 9 of the same mission tells us why it wonât work:
Letâs generate this plot ourselves in the next exercise. However, weâre going to use plt.plot()
instead of the DataFrame.plot.line()
method. Thatâs because DataFrame.plot.line()
plots separate graphs by default, which means we wonât be able to put all the lines on the same graph.
Here is how you could write the exercise using pandas plotting (and the system checker does accept this as the correct answer!)
fig, ax = plt.subplots()
for day in days:
traffic_per_day[day].plot.line('Hour (Coded)', 'Slowness in traffic (%)',
ax=ax, label=day)
plt.legend()
plt.xlabel('')
plt.show()
2 Likes