如何在绘图中将 x-tick 标签更改为自定义标签
How to change x-tick labels to custom labels, in plotly figure
我想使用 Plotly 将 xticks 标签更改为自定义标签。 (类似于 'Area1'、'Area2'、'Area3'...)
我需要在哪里以及如何添加代码?
目前,它直接从数据帧中获取 xtick 标签。
以下是更新我现在使用的xticks的代码。
fig = px.bar(df2,
x='Area_Name',
y='Per_User_Averge_Download_Speed_Mbp',
hover_data=['Users_in_Area',
'Per_User_Averge_Download_Speed_Mbp'],
color='Per_User_Averge_Download_Speed_Mbp',
height=400,
color_continuous_scale=px.colors.qualitative.Antique,
labels=dict(
Area_Name="Area",
Per_User_Averge_Download_Speed_Mbp="Download Speed/(Mbps)",
Users_in_Area="User Count")
)
fig.update_xaxes(showline=True,
linewidth=1,
linecolor='black',
mirror=True,
tickangle=-90,
tickfont=dict(family='Rockwell', color='crimson', size=14))
我认为这会有点棘手,因为 Plotly Express 会覆盖 x 轴刻度标签,所以即使您尝试修改此参数,DataFrame 的列仍会显示在绘图上。
但是,您可以直接访问 xaxis 刻度文本,因为它连接到 fig.data
中的 go.Bar
个对象集合:
for idx in range(len(fig.data)):
fig.data[idx].x = ['Area1','Area2','Area3']
例如,以Plotly documentation on bar charts为例:
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
fig.show()
在代码段中添加:
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
for idx in range(len(fig.data)):
fig.data[idx].x = ['Area1','Area2','Area3']
fig.show()
我想使用 Plotly 将 xticks 标签更改为自定义标签。 (类似于 'Area1'、'Area2'、'Area3'...) 我需要在哪里以及如何添加代码?
目前,它直接从数据帧中获取 xtick 标签。
以下是更新我现在使用的xticks的代码。
fig = px.bar(df2,
x='Area_Name',
y='Per_User_Averge_Download_Speed_Mbp',
hover_data=['Users_in_Area',
'Per_User_Averge_Download_Speed_Mbp'],
color='Per_User_Averge_Download_Speed_Mbp',
height=400,
color_continuous_scale=px.colors.qualitative.Antique,
labels=dict(
Area_Name="Area",
Per_User_Averge_Download_Speed_Mbp="Download Speed/(Mbps)",
Users_in_Area="User Count")
)
fig.update_xaxes(showline=True,
linewidth=1,
linecolor='black',
mirror=True,
tickangle=-90,
tickfont=dict(family='Rockwell', color='crimson', size=14))
我认为这会有点棘手,因为 Plotly Express 会覆盖 x 轴刻度标签,所以即使您尝试修改此参数,DataFrame 的列仍会显示在绘图上。
但是,您可以直接访问 xaxis 刻度文本,因为它连接到 fig.data
中的 go.Bar
个对象集合:
for idx in range(len(fig.data)):
fig.data[idx].x = ['Area1','Area2','Area3']
例如,以Plotly documentation on bar charts为例:
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
fig.show()
在代码段中添加:
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
for idx in range(len(fig.data)):
fig.data[idx].x = ['Area1','Area2','Area3']
fig.show()