简体   繁体   English

绘图图未正确显示 x 轴值

[英]Plotly graph does not show x-axis values correctly

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输出是

在此处输入图像描述

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.我想修改代码,使 mtds 列表字符串值“2022-03”、“2022-04”、“2022-05”、“2022-06”显示在 x 轴而不是日期中。 Could you please assist with this ?你能帮忙吗?

Thank you.谢谢你。

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:根据有关时间序列的plotly ,您可以使用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()

在此处输入图像描述

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

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