简体   繁体   English

无法在 python 中使用 plotly plot

[英]Not able to plot using plotly in python

I have following Python dataframe and I want to plot Sno.我有关注 Python dataframe 我想 plot Sno。 vs each column values using plotly. vs 使用 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.但是我在 jupyter 笔记本中没有得到任何 output,该单元只是运行而没有给出任何错误。

So I tried this approach for single column TT所以我为单列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 ?那么有人可以解释一下那个for循环有什么问题吗? Thank you !谢谢 !

The root of the problem is the behavior of jupyter notebooks and how they determine cell output.问题的根源在于 jupyter 笔记本的行为以及它们如何确定单元格 output。 Say you have a pandas DataFrame called df .假设您有一个名为df的 pandas DataFrame 。 If you now create a cell saying:如果你现在创建一个单元格:

df

you will receive the DataFrame as output to that cell upon execution.在执行时,您将收到 DataFrame 作为 output 到该单元格。 However, if you change the cell to但是,如果您将单元格更改为

new_df = df

or或者

for i in range(5):
    df

you will no longer receive any output.您将不再收到任何 output。 The reason is for that is, that jupyter notebooks always output the last received output by default.原因是,默认情况下,jupyter 笔记本总是 output 最后收到 output 。 The assignment of new_df = df doesn't return anything. new_df = df的分配不返回任何内容。 Neither does the for loop. for 循环也不行。 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.为了看到您的期望,您应该使用vestland 的建议并将fig.show()matplotplib.pyplot.plot()添加到您的 for 循环中。 That way you don't rely on the default output behavior of jupyter notebooks, but are guaranteed some output.这样,您就不会依赖 jupyter 笔记本的默认 output 行为,但可以保证一些 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.如果您只想在一个 plot 中进行比较。 Note that I pulled those lines out of the for loop that do not depend on your iterated values for efficiency.请注意,我将那些不依赖于迭代值以提高效率的行从 for 循环中拉出。

There may be more issues here, but you will in any case need to call这里可能还有更多问题,但无论如何您都需要致电

fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM