【问题标题】:How can I set the width and height in a plotly theme?如何在情节主题中设置宽度和高度?
【发布时间】:2021-12-26 01:12:35
【问题描述】:

上下文

我在整个 JupyterLab 笔记本中使用 pandas 数据帧的 .plot 方法,并将绘图后端设置为 plotly 并将默认 plotly 主题设置为 plotly

每次绘图时,我都会在之后执行.update_layout 来设置宽度、高度和边距。我这样做是因为我计划将 notebook 导出到reveal.js 幻灯片,不设置这些属性会导致不可预测的输出。

这是我的示例代码,它创建了一个没有任何边距的 200x200 绘图。

import pandas as pd
import plotly.io as pio

pd.
options.plotting.backend = "plotly"
pio.templates.default = "plotly"

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
df = pd.DataFrame({"x": x, "y": y})

fig = df.plot(x=x, y=y)
fig.update_layout(width=200, height=200, margin=dict(l=0, r=0, t=0, b=0))
fig.show()

由于我希望在我的所有绘图中都有这个绘图大小和边距,我想制作一个可以在开始时设置的主题,这样我就不必在每个图上调用.udpate_layout

我尝试过的

我试过了:

import pandas as pd
import plotly.io as pio

# Creat a custom theme and set it as default
pio.templates["custom"] = pio.templates["plotly"]
pio.templates["custom"].layout.margin = dict(l=0, r=0, t=0, b=0)
pio.templates["custom"].layout.width = 200
pio.templates["custom"].layout.height = 200
pio.templates.default = "custom"
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
df = pd.DataFrame({"x": x, "y": y})

fig = df.plot(x=x, y=y)
fig.show()

不幸的是,生成的绘图不符合尺寸规格。但是,边距设置受到尊重。

问题

如何创建情节主题以创建指定大小的情节?

【问题讨论】:

    标签: python jupyter-notebook plotly plotly-python


    【解决方案1】:

    原来我的模板中缺少autosize 属性。

    当我将其设置为 False 时:

    pio.templates["custom"].layout.autosize = False
    

    一个 200x200 的情节出来了。

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2015-03-18
      • 2020-10-18
      • 1970-01-01
      • 2011-08-20
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多