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.
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
According to documentation for offline usage https://plot.ly/python/offline/ you can export images with:
plotly.plotly.image.save_as(figure, 'my_plot.png')
But to export pdf you have to have a paid account. Using free account you can export only png or jpg formats (100 API calls a day). But not pdfs.
In case if you stack with the error like:
exceptions.PlotlyError(return_data['error'])
than there is a chance that you have some authentication problems to access Plotly API. Make sure you have an account and configure you ~/.plotly/.credentials with correct user name and api_key (you can find it in profile settings).
To control the size of the graph you need to setup your layout accordingly.
Related
Is there a method that will allow me to save my seaborn or matplotlib chart as an html string? I'm trying to embed multiple (about 20) charts into an automated report that will go out daily via email. The other aspects of my report are grounded in html. Just want to confirm that I have to rely on image only objects from seaborn/matplotlib.
I orgianlly was using altair and embedding the json into my email, however this fails to render in any email app as most email applications have javascript/css disabled.
HTML allows you to show any image encoded using base64, consider following example of small red dot
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
taken from How to Display Base64 Images in HTML. In your case you need: base64-encoded PNG image generated using seaborn, you can use base64 for that following way
import base64
import seaborn as sns
fig = sns.heatmap([[1,2],[3,4]]).get_figure()
fig.savefig('heatmap.png')
with open('heatmap.png','rb') as f:
b64data = base64.b64encode(f.read()) # b64data is bytes, use b64data.decode() if you need str
I am using Google Colab to work create maps with Python 3.0 and I have done so using Basemaps. I am next trying to create similar maps using Google Maps. There are two python packages that I have located namely gmaps and gmplot. It seems as if gmaps requires a google API but gmplot does not hence I am using gmplot.
With gmplot, I am able to create a file 'my_map.html' which if I download to my local desktop, I can open in a browser and see the map correctly.
However I would like to see the map in the notebook output cell without downloading to the local machine. The following image is a screenshot of what I have tried ... there is no error, but no display either.
is there some command like %matplotlib inline that i need execute to be able to display the contents of the file in the output cell? or is there a better solution
Try full path to your file: /content/my_map.html
The code should be:
import IPython
IPython.display.HTML(filename='/path/to/your/filename')
Please Try Following code:
from IPython.display import IFrame
IFrame(src='path/to/your/filename.html', width=900, height=600)
It did work for me. :)
This solution is not working for me if the html contains images located on colab or in your (mapped) google-drive:
IPython.display.HTML('<img src="/content/elmo_1.jpg">') # can't find image
IPython.display.HTML('<img src="/content/gdrive/My Drive/elmo_1.jpg">') # can't find image
IPython.display.HTML('<img src="/content/gdrive/My%20Drive/elmo_1.jpg">') # can't find image
It works, however, with standard url:
IPython.display.HTML('<img src="https://github.com/blablabla/elmo_1.jpg?raw=1">')
Colab won't display local file images in html. You need to convert a png file to base64
encoded = base64.b64encode(open(png_file_name, "rb").read())
And show image using
<img src=f"data:image/png;base64,{encoded.decode('utf-8')}" alt="" width="600" height="400">
Following the plotly tutorial on getting static images through the python api.
When I execute:
import plotly as py
py.offline.plot(data,
show_link=False,
image_filename='pandas-bar-chart-binned',
image='png')
My browser auto opens and I get both an HTML file in my parent directory and an image of the chart downloads from the browser, which is to be expected from the docs.
But if I execute:
import plotly as py
py.offline.plot(data,
show_link=False,
image_filename='pandas-bar-chart-binned',
image='png',
auto_open=False)
The HTML file is generated in my parent directory, but no image is produced. If I store it as a variable, I get a string that represents the location of the HTML file.
Is there a save_as function for offline, as there is for image?
import plotly as py
py.plotly.image.save_as(data, filename='pandas-bar-chart-binned.png')
I have found a handful of suggestions, but they all seem to point back to opening up a browser rather than saving directly through the file system.
I want to start doing data visualisation with Bokeh and upload it to my wordpress, and so I generated some html like so
from bokeh.plotting import figure,output_file,save
from bokeh.embed import file_html
output_file("line.html")
p=figure(plot_width=400,plot_height=400)
p.circle([1,2,3,4,5],[6,7,2,5,4],size=20,color='navy',alpha=0.5)
save(p)
I then attempted to embed the plot by pasting the saved html into wordpress.com's "HTML" tab when making a new post, and it simply erases it.
This same method works perfectly on blogger.
Is there any way to get past this for wordpress?
Thanks!
You might try the EmbedIt plugin: http://www.matteoionescu.com/wordpress/embed-html/. Just copy the entirety of your Bokeh-generated html file to a "snippet", and embed the snippet in your wordpress post.
I got it to work on my service-hosted wordpress site (not wordpress.com). The plot and tools work fine, but the left edge of the plot always appears in the center of the page (so the plot itself is not centered).
Worth a shot, but as Big red said, javascript might not be allowed at all.
My app is in python using the flask framework. I have a chart that I've generated with chartlink. I'd like to include it on a pdf that I'm outputting with pisa. Is this possible? Is there some way to save the chartkick chart as an image? Or should I look into another chart-generating option like pygal or matplotlib?
According to the chartkick documentation you can give users the option to download an image of the chart.
You will have to include canvg before chartkick.js.
Another option would be to generate the image on a PhantomJS instance and save the screenshot/image.