Why does my plotly heatmap render a line chart instead? - python

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!

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.

Can Python subprocess lose track of what is in my PATH after I import pygmsh module?

Lately I have wanted to use pygmsh to help with resampling 3d geometry. Ideally I would like to understand what I'm doing by routinely drawing the output of pygmsh calls using itikz. However something in pygmsh seams to be altering the operating system PATH after I call code of the form:
import pygmsh
with pygmsh.geo.Geometry() as geom:
geom.add_circle([0.0, 0.0], 1.0, mesh_size=0.2)
mesh = geom.generate_mesh()
If I run the following code before the pygmsh cell above I get typical pdflatex output, and a pdf:
from subprocess import check_output
subprocess.check_output(['pdflatex', 'src.tex'])
If I run that same cell after the pygmsh cell, I get a FileNotFoundError.
After playing around with this last night I made the following observations:
The PATH returned by the ipython shell command !PATH is being shortened. I have my full path available before calling the pygmsh cell, but after it has been substantially reduced.
However the output of os.environ['PATH'] seams not to change.
I am trying to use itikz to draw a diagram of a 3d mesh, generated by pygmsh in a jupyter-lab notebook. Ordinarily I can use itikz in my notebooks with out issues.
Under the hood the itikz cell magic simply saves the contents of a cell to a .tex file (ie 'src'tex'), then calls pdflatex on the file using subprocess.check_output(['pdflatex', 'src.tex']). After this it converts the resulting src.pdf to an .svg image and renders that as the cells output. It seams that the important point though is that itikz calls an external program found on the operating system PATH.
This generally works well enough, on it's own.
So I guess I have two questions.
What is the best way to find the offending code in the pygmsh repository to stamp this bug?
Why am I getting different results from the ipython shell command and from os.environ['PATH']?
I'm running python version 3.8.6 64bit, on windows 64bit 10.
Additionally
Scanning the repository for pygmsh hasn't yielded any footholds yet.
https://github.com/nschloe/pygmsh/search?q=path
If they modify the path maybe they do it by accident. Or they just don't use convenient names.
I have narrowed down the culprit to the with statement. Removing that idiom doesn't seam to mess with my path. Not a fix, seams I need to __enter__() the geom object and __exit__() for it to function, and somewhere between these two calls is my culprit!
found it... it's a known issue with gmsh [https://gitlab.onelab.info/gmsh/gmsh/-/issues/1142]
The environment of you current process can be modified by a statement in the imported module causing executable resolution relying on search PATH to fail, e.g.:
subprocess.run("ls")
os.environ["PATH"] = "bogus"
subprocess.run("ls")
This will run the first ls and fail on the second one.
Manipulating environment of running process is a problematic action and can lead to confusion. But you could also in this case isolate yourself form it by using absolute paths.
But to your edit, to find the offending location, be on a lookout for os.environ or os.putenv.
So it turns out I was encountering a known issue in gmsh which is what is doing the grunt work under the hood of pygmsh.
Unfortunatly there isn't really a work around because the fix for this involves passing arguments to a function that pygmsh does not fully expose.
In the short term I have submitted a pull request to that repo trying to expose the fix.

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?

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.

Can I output a mayavi plot as .vtk file to embed in a web app?

Okay, a little background here. I made a script that plots a dataset as 3d line plots using mlab.plot3d(), multiple times in Mayavi, after reading it from an xlsx file.
Now, I am trying to make a web app to see the created 3d models in a web browser interactively.
After some search, I found xtk and thought about saving the Mayavi output as a .vtk file, and viewing it online using xtk.
Question 1: is it a stupid plan? are there easier ways?
Question 2: if it's feasible, how do I output all the components of a mlab scene into a single vtk file?
I searched and found the save_output() function from the mayavi pipeline children using record, but that only seems to work for single objects. Any idea how to do it for the whole scene?
If you have installed mayavi, then you'll have included the tvtk python library, which is one of its dependencies. The documentation gives some examples how you can write your structures to a vtk file using the write_data function.

Categories