Doubt in Grid Chart question. Please help!

Screen Link:

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

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 (%)')**

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! :sunglasses:

Also, check out this article from the community with a similar question.

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.


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)