强制传入的 X 轴数据映射到静态 X 轴 - Plotly
Enforcing Incoming X-Axis Data to map with Static X-Axis - Plotly
我正在尝试在 Plotly 中绘制多轴折线图,我的数据基于百分比(y 轴)v/s 日期(x 轴)。
X and Y-axis coming from the database via pandas
现在,由于 Plotly 不理解字符串日期在 x 轴上的顺序,因此它会自动调整它。
我正在寻找我的 x 轴在日期和顺序上保持静态的东西,并根据日期匹配参数在该映射之上绘制图表。
static_x_axis = ['02-11-2021', '03-11-2021', '04-11-2021', '05-11-2021', '06-11-2021', '07-11-2021', '08-11-2021', '09-11-2021', '10-11-2021', '11-11-2021', '12-11-2021', '13-11-2021', '14-11-2021', '15-11-2021', '16-11-2021', '17-11-2021', '18-11-2021', '19-11-2021', '20-11-2021', '21-11-2021', '22-11-2021', '23-11-2021']
上面的列表确定了 x 轴映射。
我尝试使用范围,但似乎不支持静态映射或映射从第 0 个点开始的所有图形。
总的来说,我正在寻找一种方法,既可以遵循静态日期范围,也可以不破坏当前日期顺序,就像上图中发生的那样。
在此先感谢您的帮助。
- 根据你的问题你的数据:
- x 日期作为字符串表示(即分类)
- y 0 到 1 之间的数字(百分比)
- 三道痕迹
- 您描述 x 作为来源是无序的。要求按x轴排序
- 下面这样模拟一个图
- 然后应用分类轴排序
import pandas as pd
import numpy as np
import plotly.graph_objects as go
s = pd.Series(pd.date_range("2-nov-2021", periods=40).strftime("%d-%m-%Y"))
fig = go.Figure(
[
go.Scatter(
x=s.sample(10).sort_index().values,
y=np.linspace(n/4, n/3, 10),
mode="lines+markers+text",
)
for n in range(1,4)
]
).update_traces(texttemplate="%{y:.2f}", textposition="top center")
fig.show()
fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": s.values})
fig.show()
我正在尝试在 Plotly 中绘制多轴折线图,我的数据基于百分比(y 轴)v/s 日期(x 轴)。
X and Y-axis coming from the database via pandas
现在,由于 Plotly 不理解字符串日期在 x 轴上的顺序,因此它会自动调整它。
我正在寻找我的 x 轴在日期和顺序上保持静态的东西,并根据日期匹配参数在该映射之上绘制图表。
static_x_axis = ['02-11-2021', '03-11-2021', '04-11-2021', '05-11-2021', '06-11-2021', '07-11-2021', '08-11-2021', '09-11-2021', '10-11-2021', '11-11-2021', '12-11-2021', '13-11-2021', '14-11-2021', '15-11-2021', '16-11-2021', '17-11-2021', '18-11-2021', '19-11-2021', '20-11-2021', '21-11-2021', '22-11-2021', '23-11-2021']
上面的列表确定了 x 轴映射。
我尝试使用范围,但似乎不支持静态映射或映射从第 0 个点开始的所有图形。
总的来说,我正在寻找一种方法,既可以遵循静态日期范围,也可以不破坏当前日期顺序,就像上图中发生的那样。
在此先感谢您的帮助。
- 根据你的问题你的数据:
- x 日期作为字符串表示(即分类)
- y 0 到 1 之间的数字(百分比)
- 三道痕迹
- 您描述 x 作为来源是无序的。要求按x轴排序
- 下面这样模拟一个图
- 然后应用分类轴排序
import pandas as pd
import numpy as np
import plotly.graph_objects as go
s = pd.Series(pd.date_range("2-nov-2021", periods=40).strftime("%d-%m-%Y"))
fig = go.Figure(
[
go.Scatter(
x=s.sample(10).sort_index().values,
y=np.linspace(n/4, n/3, 10),
mode="lines+markers+text",
)
for n in range(1,4)
]
).update_traces(texttemplate="%{y:.2f}", textposition="top center")
fig.show()
fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": s.values})
fig.show()