[英]Why does plotly express throw out data points on a timeline?
I have a simple dataframe with two columns.我有一个简单的 dataframe 有两列。 A sample is shown below, the data is available here .示例如下所示,数据可在此处获得。
year-week users
0 2018-22 2
1 2018-23 3
2 2018-24 4
3 2018-25 3
4 2018-26 5
.. ... ...
69 2020-03 232
70 2020-04 226
71 2020-05 214
72 2020-06 203
73 2020-07 119
[74 rows x 2 columns]
When I try to plot those two columns with Plotly Express, it omits the data until 2019-30
.当我尝试使用 Plotly Express 对这两列进行 plot 时,它会忽略2019-30
之前的数据。
import pandas
import plotly.express as px
df = pd.read_csv("https://pastebin.com/raw/x164p1Zp")
fig = px.line(df, x="year-week", y="users")
fig.update_layout(xaxis=dict(tickformat="%Y-%W"))
fig.show()
If I plot the same data with matplotlib, the data is shown:如果我的plot数据跟matplotlib一样,数据显示:
import pandas
import matplotlib
df = pd.read_csv("https://pastebin.com/raw/x164p1Zp")
df.plot.line(x="year-week", y="users");
I cannot understand why two plotting libraries show the same data in a wildly different way.我不明白为什么两个绘图库以截然不同的方式显示相同的数据。
How can I plot all the data points in Plotly Express to get a plot similar to what matplotlib shows?我怎样才能 plot Plotly Express 中的所有数据点得到一个 plot 类似于 ZF0201313237EEB5A6F 显示的内容?
Plotly does not recognize your x axis as being a date. Plotly 无法将您的 x 轴识别为日期。 You need to convert it explicitly to a datetime format.您需要将其显式转换为日期时间格式。
Solution:解决方案:
df = pd.read_csv("https://pastebin.com/raw/x164p1Zp")
# convert column to datetime, weekday needed for conversion to work
df["year-week"] = pd.to_datetime(df["year-week"] + '-0', format="%Y-%W-%w")
fig = px.line(df, x="year-week", y="users")
fig.update_layout(xaxis=dict(tickformat="%Y-%W"))
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.