saving a python figure that later can be viewed interactively - python

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.

Related

How to create a paraview animation from python script?

So, I have a set of vtk files named file_0.vtk, file_1.vtk ... In paraview I can read all these files, choose a variable ( for example 'V'), create slices in the Z direction then save a temporal animation. I get a nice movie.
Now, I wonder if it is possible to get the same result automatically in python without using the paraview interface, i.e. just by doing :
python3 ./animation.py
I know that it is possible to save python scripts in python from the commands we do through paraview or even use python from paraview, but I don't know how to create the animation without using the software.
Can you help me?
Just use the SaveAnimation() method at the end of the python script for your pipeline. See this doc (python part at the end of the section).
If you want to see the animation at runtime, then add the AnimateReader(reader, view) method instead. Here is an example.

Creating a PPTX template using python

I have a requirement involving making a large pptx file with loads of charts, images and tables dynamic. This pptx has default styles. Is there any library or method to make a template so that I can insert the dynamic parts. Like docxtpl library allows us to input a dict and generate a docx file.
Thanks.
You can try the following python modules:
https://pypi.org/project/template-pptx-jinja/
https://pypi.org/project/pptx-template-simple/
https://pypi.org/project/python-pptx-templater/
With the first one, the example they show, it works fine, but im having trouble applying it in another custom ppt.. keep getting "Unepexpected end of template".
The third one didint work for me though, maybe you have more luck.. and now i will try the second one.
I'm not sure I understand your problem...
python-pptx (a tag you've used) is the fundamental programmable way to build a presentation - whether from a "template presentation or not.)
I hope nobody will mind me advertising my md2pptx open source project for taking Markdown and images and making a presentation.
However, I think md2pptx doesn't help you unless you have a way of turning graphs into eg PNG files - and I suspect that's not what you want.

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!

Save interactive matplotlib figures to html

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?

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.

Categories