如何在 Plotly 时间轴中插入 x 轴标签?
How to insert x axis labels in a Plotly timeline?
使用 DougR 的 in my other :
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Type="Plan", Start='2009-01-01', Finish='2009-02-28'),
dict(Task="Job A", Type="Actual", Start='2009-01-05', Finish='2009-02-15'),
dict(Task="Job C", Type="Plan", Start='2009-02-20', Finish='2009-05-30'),
dict(Task="Job C", Type="Actual", Start='2009-02-23', Finish='2009-06-04')
])
colours = {}
colours['Plan'] = 'red' # or 'rgb(220, 0, 0)' for rgb colours
colours['Actual'] = 'blue'
fig = px.timeline(df,
x="Date",
x_start="Start",
x_end="Finish",
y="Task",
color='Type',
color_discrete_map = colours
)
fig.data[1].width=0.5 # update the width of the second trace
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
使用 x="Date"
给我一个错误:
TypeError: timeline() got an unexpected keyword argument 'x'
但它在其他 charts:
中有效
fig = px.line(df, x='date', y="GOOG")
如何将 x 轴和 y 轴标签添加到我的时间轴?
参考timeline文档:https://plotly.com/python-api-reference/generated/plotly.express.timeline.html
您应该指定标签并传递相应名称的字典。
fig = px.timeline(df,
x_start="Start",
x_end="Finish",
y="Task",
color='Type',
color_discrete_map = colours
)
fig.update_layout(xaxis_title="Date") #to add a title to xaxis
使用 DougR 的
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Type="Plan", Start='2009-01-01', Finish='2009-02-28'),
dict(Task="Job A", Type="Actual", Start='2009-01-05', Finish='2009-02-15'),
dict(Task="Job C", Type="Plan", Start='2009-02-20', Finish='2009-05-30'),
dict(Task="Job C", Type="Actual", Start='2009-02-23', Finish='2009-06-04')
])
colours = {}
colours['Plan'] = 'red' # or 'rgb(220, 0, 0)' for rgb colours
colours['Actual'] = 'blue'
fig = px.timeline(df,
x="Date",
x_start="Start",
x_end="Finish",
y="Task",
color='Type',
color_discrete_map = colours
)
fig.data[1].width=0.5 # update the width of the second trace
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
使用 x="Date"
给我一个错误:
TypeError: timeline() got an unexpected keyword argument 'x'
但它在其他 charts:
中有效fig = px.line(df, x='date', y="GOOG")
如何将 x 轴和 y 轴标签添加到我的时间轴?
参考timeline文档:https://plotly.com/python-api-reference/generated/plotly.express.timeline.html
您应该指定标签并传递相应名称的字典。
fig = px.timeline(df,
x_start="Start",
x_end="Finish",
y="Task",
color='Type',
color_discrete_map = colours
)
fig.update_layout(xaxis_title="Date") #to add a title to xaxis