Save interactive matplotlib figures to html - python

I realize that there have been quite a few questions asked on this over the years, but the answers I have found thus far have been fairly unsatisfactory, and that may be just because there is no way to easily accomplish this. I have a notebook that generates interactive figures with the % matplotlib notebook magic command, but when I use nbconvert to get the HTML for the notebook, the interactivity of the figures is lost. Is there a way to save the javascript and raw data of each figure directly into the HTML output to maintain interactivity? If not, can someone outline a few possible alternative solutions that I can look into?

Related

Why does my plotly heatmap render a line chart instead?

I'm using plotly 3.6.1, because that's what Debian 10 ("buster") has (the current "stable" version of Debian).
The documentation suggests creating a heatmap should be as simple as this:
#!/usr/bin/env python3
import plotly
import plotly.figure_factory
z=[
[0.0,1.0,2.0],
[1.0,1.0,1.0],
[2.0,1.0,0.0]
]
a=[
['AD','BD','CD'],
['AE','BE','CE'],
['AF','BF','CF']
]
x=['A','B','C']
y=['D','E','F']
fig=plotly.figure_factory.create_annotated_heatmap(z,x=x,y=y,annotation_text=a)
plotly.offline.plot(fig,filename='heatmap.html',auto_open=False)
However, what that actually gets me when the html file is displayed is:
Which, while it has a grid of the supplied cell annotations and labelled the axes, seems to be under the mistaken impression it's a line chart of some sort.
How can I fix this?
I'm successfully using Plotly for other chart types (Scatter and Sunburst) without any issues. This is the first time I've tried using a figure_factory though, because that generally seems to be described as the easiest way to get a heatmap with cell annotations (which is what I want). I've only ever used the offline style of rendering to an HTML file.
The above code is just in an executable heatmap.py file, executed by ./heatmap.py and then the output viewed in whatever version of Firefox is standard in that Debian release.
I've copied into a script and tried your code and it provides an adequate heatmap for the data provided!
I would check that you were definitely using a fresh session in a fresh environment. Did you copy your code from a notebook kernel for example?
Certainly it should work, so if you copy that code directly into an executable file, give it a run and let me know how you get on, I hope you'll be happily surprised :-)
Best of luck!

Building reports and articles using Jupyter

I've been reading on how to build reports and sort of publication ready articles using Jupyter and markdown and am sort of confused at this point. I have followed several posts and guides including:
This blog post
Making publication ready Python Notebooks
Writing academic papers in plain text with Markdown and Jupyter notebook
Right now, I am using the following plugins to have a more readable format:
collapsible headings
python-markdown (breaks when outputting to pdf)
hide code
cite2c
This was all relatively easy to implement, though I am still having issues with python markdown breaking when outputting to pdf. I am wondering:
Especially considering most of the blog posts and tutorials I found are about 4 years or older, are there easier approaches or guidelines for outputting markdown in a jupyter notebook to PDF (or word?).
Crossreferences seem to not be possible yet. For instance, I can reference any figure that I label manually, but numbers are not automatically updated.
Any suggestions are welcomed!

saving a python figure that later can be viewed interactively

I am using Spyder to edit python code. I plotted a figure like following on my screen:
As you can see I have zoom-in on my menu by playing which I can magnify a ROI like following:
Now I need to send this figure to another person who'd like to view this figure interactively, say do zoom-in/zoom-out as well.So my question is, is there anyway to send this figure in certain format the other person can play with such that me without sending my entire python code?
I tried to the save icon on the menu bar, but I did not see a format that can do what I want. I am new to python, please advice. Thank you.
PS: I think in MATLAB you can do that by saving the figure in certain format, so that as long as the other person has MATLAB installed, he/she does not need the data to see the figure interactively
You need to use the pickle module.
MATLAB saves the figure in a .fig format. This is really just a .mat MATLAB data file with a different extension so MATLAB knows it stores image data. If you change the extension to .mat, you can open it is a refular MATLAB data file and see that it just contains variables storing image information.
The equivalent thing to do in matplotlib is to use the pickle.dump function to save the matplotlib figure object to a file. Someone else can then just load the figure from the file and show it. Although the other person may need to have the same matplotlib version installed.
A better option would be to use something like bokeh to save an interactive HTML plot.
I believe its too late for an answer whereas I think it will help others.
In python, if an interactive figure is plotted in plotly. Then it can be exported as a .html.
For reference please follow official documentation Interactive HTML Export in Python
.
I hope it helps.

Regarding data using in Bokeh

Don't know if this sort of question belongs to this forum, but I will give it a shot:
I have started using bokeh as an alternative to be able to share Python-generated data and plots in between my colleagues (who don't necessarily have Python, or any knowledge of the language for that matter).
Since some of the data I am using could potentially be proprietary, I would like to now how the data is actually stored when outputted from Python as a html-file?
Moreover, will anyone outside of my organization be able to get a hold of the data that the file is built up on? Meaning, is the person double-clicking on the .html-file, running it through her Chrome/Explorer the only one that can access the data that is generated in the plots etc., or could someone potentailly snoop the data externally?
Using Python code as in:
from bokeh.io import output_file
output_file('data.html', title="test data")
Thank you, and hope it's not a too a lame/bad question for this forum.
When you set the output_file and save the HTML file it is all done so locally; I don't believe it is shared to a cloud service (like plotly).
The HTML file will contain the original data used to produce the plots. If that concerns you, you can save snapshots of the plots with the save button on the toolbar.

Jinja rendering matplotlib plots?

I am looking at combining matplotlib and jinja2 to produce html pages.
What I do now is just including an image previously produced by matplotlib as a reference in my html page. The result is really static.
I've seen related questions like here or here, but none related to matplotlib/jinja integration (ultimately I'd like interactivity but It does not seem to be simple enough for me).
Is there any alternative to what I do ?
Ipython notebook from v1.x uses jinja2.
You can create inline images in the notebook and then convert it to a static html with nbconvert. You can provide custom css and you can customise the output html to hide code cells, cell numbers, ...

Categories