Embedding a Matplotlib Graph into HTML - python

I am trying to build a website. One part of the website needs that a graph is show based on user input. It will be built using Django.
I have checked out a few options, mpld3 proving to be at least better than the others. The graph is kind of animated(?) as the scatter plots appear after a pause. This is a feature which would be great if I could include it.
So while using mpld3, I faced two problems:
1. When I run the python script, I have an image as a background for the graph. This does not appear when I use mpld3 to render it to a webpage.
2. Only the final plots appear. Is there a way that I can show the points coming up on the graph one by one with pauses as it is supposed to?
Here is part of the code with the necessary details. In case there's any further detail, I'll be glad to provide it.
fig, ax = plt.subplots()
im = plt.imread('map_main.png')
implot = plt.imshow(im)
plt.axis([0, width, 0, height])
plt.ion()
for i in locations:
x, y = locations[i]
plt.scatter(x, y, c='b')
plt.pause(0.05)
locations contain a bunch of coordinates where the scatter points are supposed to show up.
Any help on what I should study next to achieve this would be great. Thanks.

Related

Combining plots using matplotlib

Hi im new to python here.
I am trying to plot a line and a circle on the same graph to see if they intersect based on the user's input. This is what I have currently. I am not very familiar with matplotlib and after socuring through the internet for hours I am unable to combine the graphs properly. Can anyone help me.
Thanks a lot
Code and plot
I tried using the extend function as well as combine but to no avail
To plot several items in one graph, you can simply do:
plt.figure()
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show
Both curve will appear on the same figure.
Also, please put code rather than image so it's easier to replicate.

Python: connecting close dots in 3D scatter plot

short: I want to connect dots that are close to each other in a 3D scatter plot with lines to get some "3D wired look" 3D model.
Background: I got from a microscope an image stack from a fish. I want to show specific cells in that fish. The Software gives me X, Y, Z values of these cells and I'm plotting them in a scatter 3D plot. To get an context I collected a lot of values (XYZ) from the skin too and want to plot them "around" the whole thing to get the look of an fish. If I would be able to connect these dots with lines Id get some kind of "wired" fish which would be great. It would be even greater if id be able to fill the planes in the wire (that would be a 3D fish).
#data of the cells
X = np.array(df["X"])
Y = np.array(df["Y"])
Z = np.array(df["Z"])
#data of the fishskin
XF = np.array(df["XF"])
YF = np.array(df["YF"])
ZF = np.array(df["ZF"])
ax.scatter(XF, YF, ZF, s=40, c='b', marker='o')
ax.scatter(XV, YV, ZV, s=40, c='r', marker='o')
plt.show()
that's what I did so far, very basic but hey, I'm rookie.
The only language I'm a little into is Python but I also got MatLab and toyed around with Blender a little. If you think my problem would be a lot easier to solve in one of these programs (maybe model the fish in blender end import him to Python or whatever) tell me! I was searching around the whole day but maybe I'm using the wrong keywords or something because I wasn't successful.
If I understand your question correctly, you would like to create an unstructured triangular grid. If this is the case, then I suggest you check out this tutorial on the Matplotlib website.

What is the meaning of colorbar.solids in Pyplot?

I am creating contour plots with matplotlib/pyplot and trying to print out a customized colorbar as well. I am also attempting to have the colorbar be printed in a completely separate image file from the plot.
sub_fig = plt.figure()
sub_ax = plt.axes()
sub_ax.axis("off")
#cs2 is a contourf object
sep_cb = plt.colorbar(cs2, cax=sub_ax)#, cax = new_figure.legend, ax=new_figure.legend, orientation="vertical")
sep_cb.shrink=0.5
sep_cb.fraction=.1
sep_cb.drawedges=True
# ~sep_cb.solids.set_edgecolor("white")
plt.savefig("colorbar_"+str(ii)+".png")
plt.clf()
So I am referring to the documentation here: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html but I find that changing the settings does not change the appearance of my colorbar. What I want is actually to make it pretty small, and have the sections separated by some whitespace, with the values/ labels to the right. I can't shrink it, or make the values appear. No matter what, it appears like this:
colorbar
However, if I enable that sep_cb.solids line, I get some lines between the sections. But the only reason I even know about this is because of this section:
It is known that some vector graphics viewer (svg and pdf) renders white gaps between segments of the colorbar. This is due to bugs in the viewers not matplotlib. As a workaround the colorbar can be rendered with overlapping segments:
cbar = colorbar()
cbar.solids.set_edgecolor("face")
draw()
It seems that colorbar.solids can be used to set some options on the plot, but I cannot find any documentation on that directly, I don't even see it mentioned elsewhere on the page. Is this some basic pyplot thing I have managed to overlook? Any help would be appreciated, thank you.

Polar stereographic plot

Is it possible to plot contours over a polar stereographic map with the latest version of cartopy?
I'd like to see an example of how this is done as I'm struggling to work it out myself!
The stereographic projection is causing a couple of headaches and is probably the projection which has raised the most issues for cartopy's polygon transformations code.
The following example show how one should produce a polar stereographic plot with cartopy.
Please note: even with this code, it is possible to tweak the sample data resolution and find that the plot takes ~30 minutes to actually render (that is a bug which we will need to sort sooner rather than later).
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.examples.waves import sample_data
ax = plt.axes(projection=ccrs.NorthPolarStereo())
x, y, z = sample_data((100, 200))
cs = ax.contourf(x, y, z, 50,
transform=ccrs.PlateCarree(),
cmap='gist_ncar')
ax.coastlines()
# without the set_global, currently, the plot is tiny because the limits
# are being erroneously being set (opened issue for that)
ax.set_global()
plt.show()
Hopefully that will show you how one should make a polar stereographic contour plot in cartopy. If you having problems with your data have a look at the open issues tagged "Geometry transforms" and see if you are getting something similar, if not, go ahead and open an issue and we can look into it.
Note: This answer is relating to cartopy v0.5.x (i.e. just before a v0.5 release), and many of the bugs mentioned here should hopefully be squashed in future releases.
Hope that helps,

How do I let my matplotlib plot go beyond the axes?

I have to translate an image plotting script from matlab to matplotlib/pylab, and I'm trying to achieve the same effect as the matlab image below:
As you can see, the z order of the plots seem to be higher than the z order of the grid, so the markers are not hidden by the axes. However, I can't figure out a way to do the same with my matplotlib image:
I'm wondering if it is possible to get the same display without having to increase the limits of the y axis.
To get the marker to show beyond the axes you can turn the clipping off. This can be done using the keyword argument in the plot command clip_on=False.
For example:
import matplotlib.pyplot as plt
plt.plot(range(5), range(5), 'ro', markersize=20, clip_on=False, zorder=100)
plt.show()
This is a complete example of how to use the zorder kwarg: http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html
Note that a higher z-order equates to a graph-element being more in the foreground.
For your second question, have a look at the figsize kwarg to instances of the Figure class: http://matplotlib.sourceforge.net/api/figure_api.html?highlight=figsize#matplotlib.figure.Figure
If you run into issues, please post some of your code and we'll be able to give more-detailed recommendations. Best of luck.
If you're plotting the lines one after the other, just change the order of the plotting calls and that would fix the z order.

Categories

Resources