I am trying to create a 2D Contour Map in Python that looks like this:
In this case, it is a map of chemical concentration for a number of points on the map. But for the sake of simplicity, we could just say it's elevation.
I am given the map, in this case 562 by 404px. I am given a number of X & Y coordinates with the given value at that point. I am not given enough points to smoothly connect the line, and sometimes very few data points to draw from. It's my understanding that Spline plots should be used to smoothly connect the points.
I see that there are a number of libraries out there for Python which assist in creation of the contour maps similar to this.
Matplotlib's Pyplot Contour looks promising.
Numpy also looks to have some potential
But to me, I don't see a clear winner. I'm not really sure where to start, being new to this programming graphical data such as this.
So my question really is, what's the best library to use? Simpler would be preferred. Any insight you could provide that would help get me started the proper way would be fantastic.
Thank you.
In the numpy example that you show, the author is actually using Matplotlib. While there are several plotting libraries, Matplotlib is the most popular for simple 2D plots like this. I'd probably use that unless there is a compelling reason not to.
A general strategy would be to try to find something that looks like what you want in the Matplotlib example gallery and then modify the source code. Another good source of high quality Matplotlib examples that I like is:
http://astroml.github.com/book_figures/
Numpy is actually a N-dimensional array object, not a plotting package.
You don't need every pixel with data. Simply mask your data array. Matplotlib will automatically plot the area that it can and leave other area blank.
I was having this same question. I found that matplotlib has interpolation which can be used to smoothly connect discrete X-Y points.
See the following docs for what helped me through:
Matplotlib's matplotlib.tri.LinearTriInterpolator docs.
Matplotlib's Contour Plot of Irregularly Spaced Data example
How I used the above resources loading x, y, z points in from a CSV to make a topomap end-to-end
Related
I have some data of soil's moisture content (Theta) in the form of 3D-domain points (CSV file of the columns x, y, z, Theta). I want to take cross sections from the 3D domain in some specific positions (section ABCD in the figure). I want to calculate the value of Theta in a 5*5 grid in the cross-section, but the points around each node of the grid are not coplanar with the unknown point. I did this before for 2D domains in python, but the 3D domains seem more complicated for me. I found that plotly can make something like that in its virtual environment but I want this to output a numpy array or pandas DataFrame to draw it as a contour in the jupyter notebook.
I know that finding the grid involves finding the value of each point like P0 in the figure by interpolation or gridding from its neighbors, then to draw the cross section using matplotlib, but I don' know how to do it.
Related question, Is slicing 2D grids from 3D grids available in matplotlib or similar libraries?
Thanks for all help.
The underlying problem is 3D interpolation. There are numerous packages which can do this type of thing, or you can write your own (using, e.g. KDE, which is basically just a type of smoothing/binning). There is a lot of material on the topic, like
This answer https://stackoverflow.com/a/15753011/230468
The scipy docs
This extensive set of option on scicomp.stack
And this blog post (with some good examples)
Have you tried playing with pyugrid? It's a library specifically for manipulating unstructured grids, so it sounds like it might be of some use to you. Check out these example notebooks.
I'm having some trouble visualizing a certain dataset that I have in a contour plot. The issue is that I have a bunch of datapoints (X,Y,Z) for which the Z values range from about 2 to 0, where a lot of the interesting features are located in the 0 to 0.3 range. Using a normal scaling, they are very difficult to see, as illustrated in this image:
Now, I have thought about what else to do. Of course there is logarithmic scaling, but then I first need to think about some sort of mapping, and I am not 100% sure how one would do that. Inspired by this question one could think of a mapping of the type scaling(x) = Log(x/min)/Log(max/min) which worked reasonably well in that question.
Also interesting was the followup discussed here.
where they used some sort of ArcSinh scaling function. That seemed to enlarge the small features quite well, proportionally to the whole.
So my question is two fold in a way I suppose.
How would one scale the data in my contour plot in such a way that the small amplitude features do not get blown away by the outliers?
Would you do it using either of the methods mentioned above, or using something completely different?
I am rather new to python and I am constantly amazed by all the things that are already out there, so I am sure there might be a built in way that is better than anything I mentioned above.
For completeness I uploaded the datafile here (the upload site is robustfiles.com, which a quick google search told me is a trustworthy website to share things like these)
I plotted the above with
data = np.load("D:\SavedData\ThreeQubitRess44SpecHighResNormalFreqs.npy")
fig, (ax1) = plt.subplots(1,figsize=(16,16))
cs = ax1.contourf(X, Y, data, 210, alpha=1,cmap='jet')
fig.colorbar(cs, ax=ax1, shrink=0.9)
ax1.set_title("Freq vs B")
ax1.set_ylabel('Frequency (GHz)'); ax1.set_xlabel('B (arb.)')
Excellent question.
Don't scale the data. You'll be looking for compromises in ranges with many scaling functions.
Instead, use a custom colormap. That way, you won't have to remap your actual data and can easily customize the visualization of the regions you'd like to highlight. Another example can be found in the scipy cookbook and there's quite a few more on the internet.
Another option is to break the plot into 2 separate regions by breaking the axis like so
I'm trying to plot a surface over several points it should look like a deformed sphere.
I used the scatter function and plotted the points, but the surface function is not working (the window is empty).
To plot the surface, I think i need a mesh function. I try ed to mash x,y,z but it was not working. How i can generate the code, to put a surface over my points?.
Thanks for helping me.
I have the points xyz stored in a list. They are describing a deformed sphere and i have to plot somthing like this
(source: iop.org)
This question is hard to answer without any sample code of what you're doing, you might want to edit it to include a working example.
I suppose you are using the mplot3d class, have you checked the examples that are provided online here, here, and here? These to me look like what you're trying to produce.
mayavi
I have some data which is on a structured grid in the X and Y directions and is unstructured in the Z direction. This is in the form of a list of data points, e.g [[x,y,z], [x2,y2,z2], ...]. There are 2 points corresponding to most x,y coordinates, and the data is double valued in the z dimension. I would like to plot this shape as an enclosed surface, and if possible remove one of the walls.
I have tried the advice here: http://docs.enthought.com/mayavi/mayavi/auto/example_surface_from_irregular_data.html#example-surface-from-irregular-data
When I try this only the bottom half of the plot is covered by the surface. I also get this message which I don' understand: No handlers could be found for logger "mayavi.core.common". I would love to know why this is.
I have tried plotting the top and bottom surfaces separately, but this looks a bit ugly. Here is what that looks like:
matplotlib
I have also tried to grid my data and follow the advice using the matplotlib demos. I can't post the link to this because I don't have the reputation, but if you google matplotlib plot3D demos it is in the first result.
I can't get this to produce anything reasonable. I think this is because I don't really understand how the sphere example on that web page could be adapted to work with data rather than a function.
Question
how can I adapt the code I have from the link I provided to produce a plot of an enclosed surface?
or, how can I use matplotlib to make the enclosed surface?
Or is there some other program/function I ought to be using for this kind of problem?
for a while I've been trying to come up with a good way to graphically represent a data series along with its estimated error.
Recently I saw some graphs where the data was plotted as a line, with a background 'ribbon' filling the area between the lines plotting data +/- sigma.
Is there a name for this type of graph, and is there any python toolkit which has the capability to make such plots?
A simple way to fake it with matplotlib would also be useful - right now I'm just plotting three lines, but I don't know how to fill the area between them.
I would use the fill_between method. Look at the Our Favorite Recipes section of the manual for matplotlib for some good examples. They have one that looks like this:
and another that looks like this: