设置绘图区域的大小

sou*_*alo 6 python figure plotly plotly-python

让我们举一个简单的例子:

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()
Run Code Online (Sandbox Code Playgroud)

结果 我将宽度和高度设置为相同的值,但得到的结果却截然不同。我知道这是因为轴刻度、标签和图例占用的空间。那么如何设置绘图区域的大小呢?假设我想要一个正方形图。我应该怎么办?
我尝试添加:

fig.update_yaxes(
    scaleanchor = "x",
    scaleratio = 1,
)
Run Code Online (Sandbox Code Playgroud)

但这并没有改变结果。有任何想法吗?

Rob*_*ond 2

一个选项是将图例放置在绘图区域中

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述

  • 谢谢,但我不想在绘图区域中使用图例...此外,轴标签仍然会影响绘图区域的大小。所以我会等待更好的答案(如果存在)。 (2认同)