7

I know that savefig has an option dpi. but if I make plots with plotly and write image I don't have an option to set dpi.

How can I set dpi with plotly?

TypeError: write_image() got an unexpected keyword argument 'dpi'
Noob Programmer
  • 698
  • 2
  • 6
  • 22
  • 2
    [How can I save plotly graphs in high quality?](https://stackoverflow.com/questions/43355444/how-can-i-save-plotly-graphs-in-high-quality)I also researched, but 'plotly' did not have the ability to adjust the `dpi`. I hope you find this answer helpful. – r-beginners May 22 '20 at 13:07

2 Answers2

11

There is an option of passing scale argument to write_image method, which helps in increasing the resolution (by putting the value greater than 1) -- the same purpose intended by dpi.

So, the code would be like:

fig.write_image('figure.png', scale=2)

Here, the scale is put 2.

If the scale is less than 1, that would decrease the dimension instead.

Suraj Regmi
  • 193
  • 2
  • 10
2

As Suraj Regmi already wrote, use the scale parameter:

In doing so, you will not violate the sizes of fonts and lines. To understand what parameter you need keep in mind that with scale = 1 the image will be 700x500 pixels in size.

Thus, if you want an 130 mm image with a 300 dpi resolution, then

scale = (width_in_mm / 25.4) / (width_default_px / dpi) = (130 / 25.4) / (700 / 300) = 2.1934758155230596

or

scale = width_in_mm * 17780.0 # dpi=300