Python can i present bokeh plotting outside jupyter notebook? - python

I've been using bokeh to plot data on a map using bokeh.
Is there a way to work with this library outside jupyter notebook?
Something like in Pychram and saving the plot to HTML file.

Bokeh can generate complete HTML pages for Bokeh documents using the file_html() function. you can refer Embedding Plots and Apps for more detail.
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
plot = figure()
plot.circle([1,2], [3,4])
html = file_html(plot, CDN, "my plot")

Related

Doubts about adding matplotlib graphs to a webpage

So, I'm learning how to use matplotlib and stumbled into MPLD3 as the most used way to get these plots in a webpage. However, MPLD3 doesn't support tick formatting and that's something critical for the project I'm in right now. I'd like to know if there is another way to add a matplotlib graph to a webpage while keeping the tick formatting and also having tooltips to display data on hover.
Thanks!
For a web application I would consider bokeh. You can output html with interactive charts or run another backend server. If you go to the ipython notebook tutorial section 10 has demos for embedding.
Alternatively you can just save images from matplotlib and use them as static assets in your page.

Exporting jupyter notebook to pdf with offline plotly graph; missing graphs

I am trying to create pdf export of my lesson plans and I use plotly offline for the graphs. In a MWE below, the plot will display in the Jupyter Notebook but will not show up when I export to pdf. I export using File-->Download as-->PDF via Latex (.pdf).
I'd like to make a pdf instead of using html. I understand it might take an extra step to convert an html export to pdf, but I was just wondering if there was a more direct route (a code modification?) that would allow me to export directly through File-->Download as-->PDF via Latex (.pdf)
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
data = [go.Scatter(
x=[1, 2, 3],
y=[3, 2, 1])
]
iplot(data)
You need to specify the appropriate Default Renderer (or Renderers, if you want to visualize it in the Notebook and also when exporting to PDF using the File-->Download as-->PDF via Latex (.pdf) option you mentioned).
I have been struggling with this myself for some hours too, but the setup that ended up working for me is the following:
import plotly.io as pio
pio.renderers.default = "notebook+pdf" # Renderer for Notebook and HTML exports + Renderer for PDF exports
# init_notebook_mode(connected=True) # Do not include this line because it may not work
Note that you can concatenate as many Renderers as you want using the + symbol, and Plotly will magically know when to use each of them.
I think because plotly graphs are svg objects and generated by javascript, I dont have export to PDF working in my jupyter notebook, so I was unable to check and confirm my answer.
Plotly offline does not have show as image, you can use plotly online to do this, its free to generate graphs,
You need to create an online account, also you need to paste the username and API key from plotly website (API key can be found in settings).
Note: please check in plotly if the plots are shared in public or private, I am not responsible for your plots becoming public.
Anyway this will give you an image output of the graph, and you can export it to PDF
Code:
import plotly
import plotly.graph_objs as go
plotly.plotly.sign_in('<<username goes here>>', '<<api key goes here>>')
trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15])
data = [trace]
layout = go.Layout(title='A Simple Plot', width=800, height=640)
fig = go.Figure(data=data, layout=layout)
plotly.plotly.image.save_as(fig, filename='a-simple-plot.png')
from IPython.display import Image
Image('a-simple-plot.png')
I don't have a solution for the exact original problem above. However, if you wish to consider the .html format, here is a nice solution that worked for me. After all, my goal was just to share the notebook with people who didn't have jupyter installed.
The conversion to .html is performed with plotlyhtmlexporter.
Here is a piece of code you can copy-paste:
pip install plotlyhtmlexporter
jupyter nbconvert --to plotlyhtml mynotebook.ipynb
You might then want to try and save (print) the .html from your browser as a .pdf, but I think it would be equivalent to just printing the original notebook to .pdf. As I say, in my case, having a jupyter-independent .html file was enough.
A bit easier solution is to use image_bytes = fig.to_image(format='png') and than display using Image(image_bytes), but firstly you should install orca, and pip3 install psutil requests
example:
fig = go.Figure()
""" here goes some code to draw """
image_bytes = fig.to_image(format='png', , width=1200, height=700, scale=1) # you can use other formats as well (like 'svg','jpeg','pdf')
#instead of using fig.show()
from IPython.display import Image
Image(img_bytes)
You can read more here
Than you can convert it with File -> Download as -> PDF or using terminal jupyter nbconvert --to pdf <notebook_name>.ipynb
Just use this and download the notebook as PDF via HTML. Also you need to pip install kaleido first.
import plotly.io as pio
pio.kaleido.scope.default_format = "png"

How to export Bokeh view to Matplotlib?

I have a Flask web app that uses Bokeh to deliver interactive charts. My end goal is to export whatever the current Bokeh view is to Matplotlib (so that I can create a printable pdf file after that). This would include how the current axes look like after the user zooms and pans. Is there a way to export that data so that I can create those Matplotlib charts behind the scenes? (Printing the page directly or printing to pdf results in low-quality and blurred charts.)
Thanks!
No, currently there is no way to export bokeh to matplotlib. Actually you can do it otherway. You can create matplotlib plot, save, and after that you can export matplotlib to bokeh. I think this is the best option. Eventually you can export bokeh plot as png but it still would not solve problem with quality

plot.ly python graph url

I am using plot.ly to plot some graphs on some data. The graph is shown correctly but I would like to get a url to the graph that is hosted on plot.ly/username/xxx. Is there a way to get the url programmatically? I checked the plot.ly documentation but did not come across this option.
By default when creating plot. The URL are passed by the function
import plotly.plotly as py
url = py.plot(fig, filename='stacked-bar')

Bokeh: how to save a file as svg?

I would like to embed a Bokeh plot (from IPython notebook) into my blog as an svg file. Is there currently a way to save the plots as svg's?
I tried to look in the documentation, but it's a little unclear.
This is now possible! From the doc:
from bokeh.io import export_svgs
plot.output_backend = "svg"
export_svgs(plot, filename="plot.svg")

Categories

Resources