1
votes

Using Plotly I want to change the xticks labels to custom labels. (Something like 'Area1', 'Area2', 'Area3'....) Where and how I need to add the code?

Currently, it is taking the xtick labels directly from the dataframe. enter image description here

Following is the code for update the xticks I have used now.

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))
1

1 Answers

1
votes

I think this will be a bit tricky since Plotly express will overwrite the x-axis tick labels so even if you try to modify this parameter, the columns of the DataFrame will still be displayed on the plot.

However, you can directly access the xaxis tick text as it is connected to the collection of go.Bar objects in fig.data:

for idx in range(len(fig.data)):
    fig.data[idx].x = ['Area1','Area2','Area3']

For example, taking an example from 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()

enter image description here

Adding in the code snippet:

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()

enter image description here