【问题标题】:Get width and height of plotly figure获取绘图图形的宽度和高度
【发布时间】:2022-01-15 19:17:39
【问题描述】:

例如如果我写

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex")

那我怎样才能得到fig的宽高呢?

如果我这样做

fig.layout.width

然后我得到None

【问题讨论】:

    标签: python plotly plotly-python


    【解决方案1】:

    这是获取width的正确方法。

    fig.layout.widthNone,因为尚未设置宽度。

    如果你明确设置它,你可以检索它

    fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex", width=200)
    >>> fig.layout.width
    200
    

    If not set explicitly the width is initialised when executing the show method based on defaults defined by plotly.js.

    显示(*args, **kwargs) 使用默认渲染器或渲染器参数指定的渲染器显示图形

    参数
    ...
    width (int or float) -- 一个整数或浮点数,用于确定绘图宽度的像素数。默认设置在 plotly.js 中。
    ...

    如果我们查看plotly.js 文档,我们会看到default width is 700default height is 450

    如果您设置fig.layout.autosize = False,您可以看到这些默认值是正确的。否则,widthheight 在每次重新布局时都会重新初始化。

    autosize:确定是否在每次重新布局时初始化用户未定义的布局宽度或高度。请注意,无论此属性如何,在第一次调用 plot 时始终会初始化未定义的布局宽度或高度。

    https://plotly.com/javascript/reference/layout/#layout-autosize

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2017-05-23
      • 2011-11-26
      • 2011-01-02
      相关资源
      最近更新 更多