PATHIK GHUGARE
PATHIK GHUGARE

Reputation: 155

Not able to plot using plotly in python

I have following Python dataframe and I want to plot Sno. vs each column values using plotly .

    TT  AN  AP  AR  AS  BR  CH  CT  DN  DL  ... PY  PB  RJ  SK  TN  TG  TR  UP  UT  WB
Sno.                                                                                    
1   81  0   1   0   0   0   0   0   0   7   ... 0   1   3   0   1   1   0   12  0   0
2   27  0   0   0   0   0   0   0   0   0   ... 0   0   1   0   0   2   0   1   0   0
3   15  0   0   0   0   0   0   0   0   0   ... 1   0   0   0   0   1   0   0   1   0
4   11  0   0   0   0   0   0   0   0   1   ... 0   0   0   0   0   1   0   2   0   1
5   37  0   0   0   0   0   0   0   0   2   ... 0   1   3   0   1   8   0   2   1   0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
147 rows × 36 columns

So here is my approach :

def plot_case(df):
    for i in df.columns.values :
        dn = df.index.values
        dc = df[i].values
        xaxis = go.layout.XAxis(title="Day number")
        yaxis = go.layout.YAxis(title="New cases")

        fig = go.Figure(layout=go.Layout(title=i, xaxis=xaxis, yaxis=yaxis))
        fig.add_trace(go.Scatter(x=dn, y=dc))
plot_case(df)

But I am not getting any output in jupyter notebook, that cell just runs without giving any errors .

So I tried this approach for single column TT

xaxis = go.layout.XAxis(title="Day number")
yaxis = go.layout.YAxis(title="New cases")

fig = go.Figure(layout=go.Layout(title="TT", xaxis=xaxis, yaxis=yaxis))
fig.add_trace(go.Scatter(x=df.index.values, y=df.TT.values))

And it worked ! So can someone please explain me what's wrong in that for loop ? Thank you !

Upvotes: 3

Views: 100

Answers (2)

Christian Steinmeyer
Christian Steinmeyer

Reputation: 954

The root of the problem is the behavior of jupyter notebooks and how they determine cell output. Say you have a pandas DataFrame called df. If you now create a cell saying:

df

you will receive the DataFrame as output to that cell upon execution. However, if you change the cell to

new_df = df

or

for i in range(5):
    df

you will no longer receive any output. The reason is for that is, that jupyter notebooks always output the last received output by default. The assignment of new_df = df doesn't return anything. Neither does the for loop. In order to see what you expect, you should use vestland's suggestion and add fig.show() or matplotplib.pyplot.plot() to your for loop. That way you don't rely on the default output behavior of jupyter notebooks, but are guaranteed some output.

In the end, your code becomes:

def plot_case(df):
    for i in df.columns.values :
        dn = df.index.values
        dc = df[i].values
        xaxis = go.layout.XAxis(title="Day number")
        yaxis = go.layout.YAxis(title="New cases")

        fig = go.Figure(layout=go.Layout(title=i, xaxis=xaxis, yaxis=yaxis))
        fig.add_trace(go.Scatter(x=dn, y=dc))
        fig.show()

if you want individual plots, or

def plot_case(df):
    xaxis = go.layout.XAxis(title="Day number")
    yaxis = go.layout.YAxis(title="New cases")
    fig = go.Figure(layout=go.Layout(title='comparison', xaxis=xaxis, yaxis=yaxis))
    dn = df.index.values
    for i in df.columns.values :
        dc = df[i].values
        fig.add_trace(go.Scatter(x=dn, y=dc))
    fig.show()

If you want to have a comparison in just one plot, instead. Note that I pulled those lines out of the for loop that do not depend on your iterated values for efficiency.

Upvotes: 5

vestland
vestland

Reputation: 61204

There may be more issues here, but you will in any case need to call

fig.show()

Upvotes: 2

Related Questions