Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export pdf using plotly python

I generate a plotly graph using this:

import plotly.plotly as py
import plotly.graph_objs as go

fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='stacked-bar')

I tried generating a pdf using:

from xhtml2pdf import pisa

result_file = open('report.pdf', "w+b")

pisa_status = pisa.CreatePDF(
url,                 
dest=result_file)            

result_file.close()       

But I end up getting a pdf file with the url on it instead of the graph.

How can I export the plotly graph into a pdf? One the webpage, when I click the export button I am able to generate a jpeg and png but not a pdf. Also, I'm unable to control the size of the graph when exporting the, how can I control the size? Stretch out the graph making it a little wider.

Thanks.

like image 263
MTA Avatar asked Sep 01 '25 16:09

MTA


1 Answers

The APIs have now changed, so I thought I'd post an update..

The new way of doing it is to use plotly.io.write_image

Here is an example:

plotly.io.write_image(fig, 'output_file.pdf', format='pdf')

The following formats are supported: png, jpg (or jpeg), webp, svg, pdf

like image 80
jmg Avatar answered Sep 04 '25 05:09

jmg