Aman Singh
Aman Singh

Reputation: 1241

Plotly How to move x-axis to top and y-axis to the right?

Is there a way to move the Plotly-plot's xtick labels to "top" of the plot
and ytick labels to the "right" of the plot?

enter image description here

In the above plot, I basically want to move the xtick labels to top, and ytick labels to the right. I know this is not the prefered way, but i just want to know if this is even possible.

Here's a sample code, for re-constructing the above plot,

import plotly.graph_objects as go


fig = go.Figure()

fig.add_trace(go.Bar(
    x=["Apples", "Oranges", "Watermelon", "Pears"],
    y=[3, 2, 1, 4]
))

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    yaxis=dict(
        title_text="Y-axis Title",
        ticktext=["Very long label", "long label", "3", "label"],
        tickvals=[1, 2, 3, 4],
        tickmode="array",
        titlefont=dict(size=30),
    )
)

fig.update_yaxes(automargin=True)

fig.show()

Upvotes: 9

Views: 9229

Answers (1)

vestland
vestland

Reputation: 61104

You can use:

fig.update_layout(
    xaxis={'side': 'top'}, 
    yaxis={'side': 'right'}  
)

Plot:

enter image description here

Complete code:

import plotly.graph_objects as go


fig = go.Figure()

fig.add_trace(go.Bar(
    x=["Apples", "Oranges", "Watermelon", "Pears"],
    y=[3, 2, 1, 4]
))

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    yaxis=dict(
        title_text="Y-axis Title",
        ticktext=["Very long label", "long label", "3", "label"],
        tickvals=[1, 2, 3, 4],
        tickmode="array",
        titlefont=dict(size=30),
    )
)

fig.update_layout(
    xaxis={"mirror" : "allticks", 'side': 'top'}, 
    yaxis={"mirror" : "allticks", 'side': 'right'}  
)

fig.show()

Upvotes: 21

Related Questions