简体   繁体   English

Plotly:How to plot time series in Dash Plotly

[英]Plotly: How to plot time series in Dash Plotly

I've searched for days and didn't find an answer.我已经搜索了几天,但没有找到答案。 How can I plot a time series data in Dash Plotly as a linegraph with selectable lines?我如何将 Dash Plotly 中的时间序列数据 plot 作为具有可选线条的折线图?

My data (pandas dataframe) describes GDP of different countrys.我的数据(熊猫数据框)描述了不同国家的 GDP。 Index is country, column is years.索引是国家,列是年份。

在此处输入图像描述

I don't find a solution to pass the data to Dash Plotly linegraph.我找不到将数据传递给 Dash Plotly 线图的解决方案。 What are my x and y values?我的 x 和 y 值是多少?

fig = px.line(df, x=?, y=?)

By the looks of it, the solution in your example should be:从它的外观来看,您示例中的解决方案应该是:

fig = px.line(df, x=df.index, y = df.columns)

Plot 1 - plot by columns as they appear in your dataset Plot 1 - plot 按列显示在您的数据集中

在此处输入图像描述

From here, if you'd like to display countries in the legend and have time on the x-axis, you can just add df = df.T into the mix and get:从这里开始,如果您想在图例中显示国家并在 x 轴上显示时间,您只需将df = df.T添加到组合中并获得:

Plot 2 - transposed dataframe to show time on the x-axis Plot 2 - 转置 dataframe 以在 x 轴上显示时间

在此处输入图像描述

Details细节

There's a multitude of possibilites when it comes to plotting time series with plotly. Your example displays a dataset of a wide format.使用 plotly 绘制时间序列时有多种可能性。您的示例显示了宽格式的数据集。 With the latest versions, plotly handles both datasets of long and wide format elegantly straight out of the box.在最新版本中,plotly 可以开箱即用地优雅地处理长格式和宽格式的数据集。 If you need specifics of long and wide data in plotly you can also take a closer look here .如果您需要 plotly 中长宽数据的详细信息,您也可以在这里仔细查看。

The code snippet below uses the approach described above, but in order for this to work for you exactly the same way, your countries will have to be set as the dataframe row index.下面的代码片段使用上述方法,但为了使其以完全相同的方式为您工作,您的国家/地区必须设置为 dataframe 行索引。 But you've stated that they are, so give it a try and let me know how it works out for you.但是您已经说过它们是,所以请尝试一下,让我知道它对您有何影响。 And one more thing: you can freely select which traces to display by clicking the years in the plot legend.还有一件事:您可以通过单击 plot 图例中的年份来自由显示 select。 The figure produced by the snippet below can also be directly implemented in Dash by following the steps under the section What About Dash?下面代码片段生成的图形也可以按照“ What About Dash? ”一节中的步骤直接在 Dash 中实现。 here .在这里

Complete code:完整代码:

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio

import plotly.io as pio

# sample dataframe of a wide format
np.random.seed(5); cols = ['Canada', 'France', 'Germany']
X = np.random.randn(6,len(cols))  
df=pd.DataFrame(X, columns=cols)
df.iloc[0]=0;df=df.cumsum()
df['Year'] =  pd.date_range('2020', freq='Y', periods=len(df)).year.astype(str)
df = df.T
df.columns = df.iloc[-1]
df = df.head(-1)
df.index.name = 'Country'

# Want time on the x-axis? ###
# just include:
# df = df.T
##############################

# plotly
fig = px.line(df, x=df.index, y = df.columns)
fig.update_layout(template="plotly_dark")
fig.show()

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

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