How do I plot the surface of a 4D plot? - python

I am trying to plot the wave function for a particle in a 3D box. This requires me to plot 4 variables: x, y, z axes and the probability density function.
The probability density function is:
abs((np.sin((p*np.pi*X)/a))*(np.sin((q*np.pi*Y)/b))*(np.sin((r*np.pi*Z)/c)))**2
I am using np.arange() for the X, Y and Z.
I have read that to do this you need to plot the surface of a 4D plot.
Here is what it is supposed to look like:

You want to plot a 3D scalar field f(x,y,z) against all three spatial coordinates.
I am not sure what you're precisely willing to do: which surfaces are you talking about ? You may want to plot iso-density surfaces, which allow for clear visualization of the field.
I don't know the Matplotlib formulation for this, but with Mayavi2 (a great 3D-plotting Python library) you can use "contour3d" :
http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mayavi.mlab.contour3d
Anyway if you're willing to do advanced 3D stuff Mayavi is way better than Matplotlib.
See the gallery for examples :
http://docs.enthought.com/mayavi/mayavi/auto/examples.html
Hope that helps !

Are these plots essentially plots allowing three of the variables to vary freely while fixing one of the variables and then cycling through all four? If so then these plots could be achieved using matplotlib.Axes3d or the like?

Related

Plotting 3D Electric Potential and Field Lines in Python

My goal is to create a 3d plot of the field inside a Paul trap. I've been trying to do this 3D plot without any success. My problem is that I have a 3D matrix that represents the potential everywhere in space, but I haven't found a way to plot it as a contour or color map.
All the options I saw required me to create a meshgrid of x and y and then plot z as a function of x and y, which isn't what I need because the potential depends on all 3 coordinates. Does anyone know of a function to plot in such a way, or at least a way to trick other functions into doing what I need?
Thank you in advance!

In python, is it possible to plot colour map by giving coordinates and function value?

On the web, the only solution I find to plot the values of a two-dimensional function is to have a file with the matrix A(nxm) of the function's values and use matplotlib imshow. This will produce a map, whose extent (dimension along axes) has to be known and explicitly indicated.
Now, my concern arises from the fact that I need to plot two maps on the same axis, but one is slanted with respect to the other. So to say, I have a large main rectangular map and a smaller one that is oblique and superimposed.
In the end, the question is: is it possible to plot 2d maps starting from a file that is written as x, y, f(x,y) rather than just using f(x,y) so the plotting tool knows exactly where to draw?
I luckily found what I was looking for in pcolormesh that can draw a map given the coordinates of each point and its "colour"-value.

How to make data points in a 3D python scatter plot look like "discs" instead of "spheres"

In a standard 3D python plot, each data point is, by default, represented as a sphere in 3D. For the data I'm plotting, the z-axis is very sensitive, while the x and y axes are very general, so is there a way to make each point on the scatter plot spread out over the x and y direction as it normally would with, for example, s=500, but not spread at all along the z-axis? Ideally this would look like a set of stacked discs, rather than overlapping spheres.
Any ideas? I'm relatively new to python and I don't know if there's a way to make custom data points like this with a scatter plot.
I actually was able to do this using the matplotlib.patches library, creating a patch for every data point, and then making it whatever shape I wanted with the help of mpl_toolkits.mplot3d.art3d.
You might look for something called "jittering". Take a look at
Matplotlib: avoiding overlapping datapoints in a "scatter/dot/beeswarm" plot
It works by adding random noise to your data.
Another way might be to reduce the variance of the data on your z-axis (e.g. applying a log-function) or adjusting the scale. You could do that with ax.set_zscale("log"). It is documented here http://matplotlib.org/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.set_zscale

Pyplot - Drawing a log-log heat map under a curve

I've been struggling with this problem for days, but haven't yet found an answer on the site, so here goes!
I've made a simple straight line plot made in python, using matplotlib.pyplot - It's essentially a triangle, bounded by two straight lines and the y-axis, with a log-log scale. (I can upload the plot if this isn't a clear description, but I've not enough reputation to do so in this post - Sorry!)
The difficult part is, I now need to fill that triangle (and only that area ideally) with a heat map to show the values of a 3rd parameter, which depends on x and y in an extremely complex way. There's no simple function to describe z(x,y), but I have numerical tables giving the values of z at a range of discrete x and y values.
Is it at all possible to create such a graph (especially bearing in mind that the basic plot also has logarithmic axes)?
Thanks for reading!

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

Categories