Plotting multivalued surface in mayavi - python

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?

Related

How to slice 2D grid from 3D irrigular data?

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.

surfaceplot with matplotlib over points xyz

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.

Best way to create a 2D Contour Map with Python

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

Need to add a "legend" to an arrow/contour plot

I am plotting some scalar data as a contour plot with matplotlib.contourf. On top of it, I am plotting some vector data with matplotlib.arrow. The basic plot has come along OK, but now I need to put a box on the plot with a default-size arrow plus the data value to which it corresponds, so the viewer will know what kind of scale he is looking at. For instance, I need a box with a horizontal arrow of some length and, below that, some text like "10 cm/sec".
First, if anyone can give me a simple approach to this, I would be grateful.
Second, the approach I have tried is to do the contour plot, then plot the arrows, then add a rectangle to the plot like so:
rect=pl.Rectangle((300,70),15,15,fc='white')
pl.gca().add_patch(rect)
and then, finally, put my scale arrow and text on top of this rectangle.
This isn't working because the rectangle patch covers up the contour, but it doesn't cover up the arrows in the plot. Is there a way to move the patch completely "to the front" of everything else?
Got it. Using pylab.quiver and pylab.quiverkey functions. quiver produces a nice vector field with just a few lines of code, and quiverkey makes it easy to produce a scaling vector with text. And, for some reason, the arrows plotted with quiver are indeed covered by my rectangle, so it is easy to make the scaling arrow very visible. There are still some mysteries in all of this for me. If anyone wants to try to clear them up, would be much obliged. But I have a way now to do what I need in this instance.

transforming coordinates in matplotlib

I'm trying to plot a series of rectangles and lines based on a tab delimited text file in matplotlib. The coordinates are quite large in the data and shown be drawn to scale -- except scaled down by some factor X -- in matplotlib.
What's the easiest way to do this in matplotlib? I know that there are transformations, but I am not sure how to define my own transformation (i.e. where the origin is and what the scale factor is) in matplotlib and have it easily convert between "data space" and "plot space". Can someone please show a quick example or point me to the right place?
If you simply use matplotlib's plot function, the plot will fit into one online window, so you don't really need to 'rescale' explicitly. Linearly rescaling is pretty easy, if you include some code sample to show your formatting of the data, somebody can help you in translating the origin and scaling the coordinates.

Categories