How to make a contour plot with three variables in a dataset? - python

I am trying to generate a contour graph in terms of three parameters (say x, y, z). These parameters come from a data table of more than 5000 values.I need the graphics to look like the figures shown below.

Contour plots are most easily made using matplotlib's contour.
There's also a corresponding contourf function that provides filled contours. Anyway, what you uploaded looks more like matplotlib's pcolor or pcolormesh, as they draw colored pixels instead of isovalue lines.
Here's a nice comparison of both if you need to choose.
Edit: For (x,y,z) points that are not distributed on a grid (i.e. come from random samples), a working solution seems to be a combination of binned_statistic_2d and then either plt.pcolor or plt.contour.

Related

Convert multiple 2D lines in heatmap to represent density in each cell

I have numerous 2D lines (x1,y1;x2,y2) and a bunch of lines crossed each other in a specific area of the map. I have plotted as below, but it may not be easy to interpret.
Is it possible to use these 2D lines in the form of heatmap, and represent the crossing density in each cell (like this flights path).
In other ways, each 2D line has a Z value, so we may use it as a color bar for that heatmap.
I have seen multiple heatmap plots using scatter data points but could not find any way for 2D lines. Example
Any suggestions will be appreciated.

How to set camera angle of a 2D plot in matplotlib?

I have generated a series of 2D plots using matplotlib.pyplot. I want to change the perspective of each 2D plot to make them look more "3D" (from the rectangular shape to parallelogram shape) and stack them together by hand, which will look something like this:
If there are texts present in the 2D plot (e.g. labels, title, legend), I want them to be rotated together with the plot. The reason I don't want to use mplot3d is that it doesn't support some advanced functions that is used in my 2D plots.
This has already been asked before for 3D plots: how to set "camera position" for 3d plots using python/matplotlib?, but the ax.view_init is only implemented for 3D plots. I wonder if there is a way to also change the camera angle for a 2D plot. If not, are there any tools that can do this task?

Python - How to plot 'boundary edge' onto a 2D plot

The program pastebinned below generates a plot that looks like:
Pastebin: http://pastebin.com/wNgAG6K9
Basically, the program solves an equation for AA, and plots the values provided AA>0 and AA=/=0. The data is plotted using pcolormesh from 3 arrays called x, y and z (lines 57 - 59).
What I want to do:
I would like to plot a line around the boundary where the solutions go from zero (black) to non-zero (yellow/green), see plot below. What is the most sensible way to go about this?
I.e. lines in red (done crudely in MS paint)
Further info: I need to be able to store the red dashed boundary values so that I can plot the red dashed boundary condition to another 2d plot made from real/measured/non-theoretical data.
Feel free to ask for further information.
Without seeing your data, I would suggest first trying to work with matplotlib's internal algorithm to plot the contour line corresponding to the zero level. This is simple, but it might happen that the interpolation that is used for this doesn't look good enough (I don't know if it can find that sharp peak in the contour line). The proof of the pudding is in the eating:
plt.contour(x,y,z,[0],colors='r',linewidths=2,linestyles='dashed')
If that doesn't suffice, you might have to resort to image processing methods to find the boundaries of your data (after turning it into binary).

matplotlib surface plot limited by the boundaries

Is there any kind of chance to "cut" the surface plot (x,y,z) made by use of the matplotlib by some well defined boundaries, so that I can draw any kind of shape in 3D. Now I can do that but x,y are 2D arrays (meshgrid) and the shape is always rectangular.
Example:
Here, the plate has a base-shape of rectangular (2d-array are used). The z coordinates are derived by some function f=f(x,y).
What I would like achieve is shown in the picture below (made by hand ;)). One idea is to turn-off a single cell. But how to make the cells transparent?
What you'd like is to mask some regions in the surface. Unfortunately, matplotlib does not support masked arrays yet for plot_surface, but you could circumvent it by using np.nan for those masked regions.
It is also detailed in plotting-a-masked-surface-plot-using-python-numpy-and-matplotlib.

When to use imshow over pcolormesh?

I often find myself needing to create heatmap-style visualizations in Python with matplotlib. Matplotlib provides several functions which apparently do the same thing. pcolormesh is recommended instead of pcolor but what is the difference (from a practical point of view as a data plotter) between imshow and pcolormesh? What are the pros/cons of using one over the other? In what scenarios would one or the other be a clear winner?
Fundamentally, imshow assumes that all data elements in your array are to be rendered at the same size, whereas pcolormesh/pcolor associates elements of the data array with rectangular elements whose size may vary over the rectangular grid.
If your mesh elements are uniform, then imshow with interpolation set to "nearest" will look very similar to the default pcolormesh display (without the optional X and Y args). The obvious differences are that the imshow y-axis will be inverted (w.r.t. pcolormesh) and the aspect ratio is maintained, although those characteristics can be altered to look like the pcolormesh output as well.
From a practical point of view, pcolormesh is more convenient if you want to visualize the data array as cells, particularly when the rectangular mesh is non-uniform or when you want to plot the boundaries/edges of the cells. Otherwise, imshow is more convenient if you have a fixed cell size, want to maintain aspect ratio, want control over pixel interpolation, or want to specify RGB values directly.

Categories

Resources