Python 中的 Plotly 图表动画不正确
Plotly chart not animating properly in Python
我有一个相同的数据框 df,我在 Plotly 中设置了样式并创建了一个折线图。我添加了动画功能,但图表似乎出现错误。
Size Date POD
0.027289 11/1/2020 S
0.013563 11/1/2020 S
0.058498 11/1/2020 S
0.281953 11/1/2020 S
0.479725 11/1/2020 S
0.007358 11/1/2020 S
0.075818 11/1/2020 S
0.069744 11/1/2020 S
0.029844 11/1/2020 S
0.38383 11/1/2020 S
0.3451 11/1/2020 S
0.034024 11/1/2020 S
0.292939 11/1/2020 S
0.208562 11/1/2020 S
0.013108 11/1/2020 S
这就是我正在做的
import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter(df1, x = "Date", y = "Size", color = "POD",title = "POD Growth in US",
animation_frame="Date",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90],labels = {"Size": "Size in TB"})
fig.update_layout(
font_family="Arial",
font_color="black",
title_font_family="Arial",
title_font_color="black",
legend_title_font_color="black"
)
fig.update_xaxes(title_font_family="Arial")
fig.update_layout(
title={
'text': "POD Growth in US",
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
fig.update_traces(marker=dict(size=8,
line=dict(width=1,
color='DarkSlateGrey')),
selector=dict(mode='markers'))
fig.show()
我认为问题出在这行代码中,但我不能 100% 确定我做错了什么:
animation_frame="Date",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90]
我得到的错误:
Invalid value of type 'pandas._libs.tslibs.timestamps.Timestamp' received for the 'name' property
of frame
Received value: Timestamp('2020-09-01 00:00:00
The 'name' property is a string and must be specified as:
- A string
- A number that will be converted to a string
我是否必须转换为“日期”列才能解决问题?
这是我想要制作动画的图表:
我不确定您的烛台动画是否会按预期 运行,但是将 DataFrame 的 Date
列从时间戳更改为字符串(使用日期时间库)应该可以解决该错误.
import datetime as dt
df1['Date'] = df1['Date'].map(lambda x: x.strftime('%m/%d/%Y'))
我有一个相同的数据框 df,我在 Plotly 中设置了样式并创建了一个折线图。我添加了动画功能,但图表似乎出现错误。
Size Date POD
0.027289 11/1/2020 S
0.013563 11/1/2020 S
0.058498 11/1/2020 S
0.281953 11/1/2020 S
0.479725 11/1/2020 S
0.007358 11/1/2020 S
0.075818 11/1/2020 S
0.069744 11/1/2020 S
0.029844 11/1/2020 S
0.38383 11/1/2020 S
0.3451 11/1/2020 S
0.034024 11/1/2020 S
0.292939 11/1/2020 S
0.208562 11/1/2020 S
0.013108 11/1/2020 S
这就是我正在做的
import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter(df1, x = "Date", y = "Size", color = "POD",title = "POD Growth in US",
animation_frame="Date",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90],labels = {"Size": "Size in TB"})
fig.update_layout(
font_family="Arial",
font_color="black",
title_font_family="Arial",
title_font_color="black",
legend_title_font_color="black"
)
fig.update_xaxes(title_font_family="Arial")
fig.update_layout(
title={
'text': "POD Growth in US",
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
fig.update_traces(marker=dict(size=8,
line=dict(width=1,
color='DarkSlateGrey')),
selector=dict(mode='markers'))
fig.show()
我认为问题出在这行代码中,但我不能 100% 确定我做错了什么:
animation_frame="Date",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90]
我得到的错误:
Invalid value of type 'pandas._libs.tslibs.timestamps.Timestamp' received for the 'name' property
of frame
Received value: Timestamp('2020-09-01 00:00:00
The 'name' property is a string and must be specified as:
- A string
- A number that will be converted to a string
我是否必须转换为“日期”列才能解决问题?
这是我想要制作动画的图表:
我不确定您的烛台动画是否会按预期 运行,但是将 DataFrame 的 Date
列从时间戳更改为字符串(使用日期时间库)应该可以解决该错误.
import datetime as dt
df1['Date'] = df1['Date'].map(lambda x: x.strftime('%m/%d/%Y'))