soungalo
soungalo

Reputation: 1338

Setting the size of the plotting area

Let's take a simple example:

import plotly.express as px
x = ['A', 'B']
y = [10, 20]
fig = px.bar(x=x, y=y, color=x)
fig.update_layout(autosize=False, width=300, height=300, showlegend=True)
fig.show()

result I set the width ad height to the same value, but get something rather different. I understand this is because of the space taken by the axes ticks and labels and the legend. So how do I set the size of the plotting area? Say I want a square plot. What should I do?
I tried adding:

fig.update_yaxes(
    scaleanchor = "x",
    scaleratio = 1,
)

but this didn't change the result. Any ideas?

Upvotes: 6

Views: 4674

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31236

An option is to place legend such that it is in the plot area

import plotly.express as px
x = ['A', 'B']
y = [10, 20]
fig = px.bar(x=x, y=y, color=x)
fig.update_layout(autosize=False, width=200, height=200, showlegend=True, margin={"l":0,"r":0,"t":0,"b":0})
fig.show()
fig.update_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.01
))
fig.show()

enter image description here enter image description here

Upvotes: 2

Related Questions