如何更改 Plotly 保存的图像的图像大小?

How can I change the image size of a Plotly saved image?

我想弄清楚如何在我的 Plotly 保存的图像中更改图像的大小。现在尺寸很小,但我想更改它以便更好地查看细节。

为了以您为图形指定的分辨率将图形导出为所需的图像文件格式,您可以使用 plotly.io.write_image 功能。

示例来自下面的 link 文档(其中 fig 是您的图片,您想要 .PNG):

import plotly.io as pio
pio.write_image(fig, 'images/fig1.png')

这也适用于 .JPEG、.WebP 甚至矢量格式 .SVG、.PDF 和 .EPS。只需将 .PNG 替换为您需要的即可。

https://plot.ly/python/static-image-export/

进一步回应: 要获得您想要的精确尺寸,您可以将布局更改为 include 以下内容(fig 是您的数字):

layout = go.Layout(
    autosize=False,
    width=500,
    height=500
)
fig = go.Figure(data=data, layout=layout)
pio.write_image(fig, 'images/fig1.png')

来自:https://plot.ly/python/setting-graph-size/ 这应该让你给图像任何你想要的尺寸。

恕我直言,还有一种更简单的方法来定义保存图像的宽度和高度,即只需将值添加到方法 write_image 中的参数宽度和高度,如下所示:

import plotly.io as pio
pio.write_image(fig, 'yourImage.png', width=1980, height=1080)

您可以在保存文件时指定高度和宽度,并增加缩放比例以获得更好的质量。

import plotly.io as pio

pio.write_image(fig, file, format=png/jpeg/svg, scale=1, width=1000, height=800)

在我的例子中这有效:

dcc.Graph(
id='graph_name',
config={        
    "toImageButtonOptions": {"width": 800, "height": 600, "filename": 'graph_filename'}
})