Plotly Graph不能正确显示X轴值

发布于 2025-02-11 13:52:58 字数 639 浏览 0 评论 0原文

考虑以下代码:

from plotly import graph_objs as go
import pandas as pd

mtds = ['2022-03', '2022-04', '2022-05', '2022-06']
values = [28, 24, 20, 18]

data1 = []
for j in range(4):
  data1.append([mtds[j], values[j]])
df1 = pd.DataFrame(data1, columns=['month', 'counts'])

fig = go.Figure()

fig.add_trace(go.Scatter(
   x = df1['month'],
   y = df1['counts'],
  name = 'counts history'
))

fig.show()

输出为

​我想修改代码,以便 X轴而不是日期中显示了MTDS列表字符串值'2022-03','2022-04','2022-05','2022-06'。你能协助这一点吗?

谢谢。

Consider the following code :

from plotly import graph_objs as go
import pandas as pd

mtds = ['2022-03', '2022-04', '2022-05', '2022-06']
values = [28, 24, 20, 18]

data1 = []
for j in range(4):
  data1.append([mtds[j], values[j]])
df1 = pd.DataFrame(data1, columns=['month', 'counts'])

fig = go.Figure()

fig.add_trace(go.Scatter(
   x = df1['month'],
   y = df1['counts'],
  name = 'counts history'
))

fig.show()

The output is

enter image description here

However, this is not was I was expecting. I would like to amend the code such that
the mtds list string values '2022-03', '2022-04', '2022-05', '2022-06' are shown in the x-axis instead of the dates. Could you please assist with this ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2025-02-18 13:52:58

根据 plotly document 在时间序列上,您可以使用update_xaxes更改X轴标签的出现和格式的方法:

fig = go.Figure()
fig.add_trace(go.Scatter(x=df1["month"], y=df1["counts"], name="counts history"))
fig.update_xaxes(dtick="M1", tickformat="%Y-%m")
fig.show()

As per the plotly documentation on time series, you can use the update_xaxes method to change the ocurrence and format of the x-axis labels:

fig = go.Figure()
fig.add_trace(go.Scatter(x=df1["month"], y=df1["counts"], name="counts history"))
fig.update_xaxes(dtick="M1", tickformat="%Y-%m")
fig.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文