Hi I'm new to programming and I'm trying to do something that's probably really obvious but for the life of me I can't figure it out. I have a series of x, y, z data (in my case, corresponding to distance, depth, and pH). I would like to plot isolines of the z data (pH) on an xy (distance, depth) grid using matplotlib. Is there any way to do this? Thanks.
Asked
Active
Viewed 8,445 times
1 Answers
3
The solution will depend on how the data is organized.
Data on regular grid
If the x
and y
data already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.
#x y z
4 1 3
6 1 8
8 1 -9
4 2 10
6 2 -1
8 2 -8
4 3 8
6 3 -9
8 3 0
4 4 -1
6 4 -8
8 4 8
can plotted as a contour
using
import matplotlib.pyplot as plt
import numpy as np
x,y,z = np.loadtxt("data.txt", unpack=True)
plt.contour(x.reshape(4,3), y.reshape(4,3), z.reshape(4,3))
Arbitrary data
(a) In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid. One method to do so is provided by matplotlib itself, using matplotlib.mlab.griddata
.
import matplotlib.mlab
xi = np.linspace(4, 8, 10)
yi = np.linspace(1, 4, 10)
zi = matplotlib.mlab.griddata(x, y, z, xi, yi, interp='linear')
plt.contour(xi, yi, zi)
(b) Finally, one can plot a contour completely without the use of a quadrilateral grid. This can be done using tricontour
.
plt.tricontour(x,y,z)
An example comparing the latter two methods is found on the matplotlib page.

ImportanceOfBeingErnest
- 321,279
- 53
- 665
- 712
-
Thank you! I also found a way to do it in matlab (not my preference but I was desperate) using tricontour from here: [link](https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data/content/tricontour.m)_italic_ **bold** `code` – Hg1 Mar 13 '17 at 14:44
-
dataset = xlsread (dataset.xlsx','Sheet2','B1:D10'); #each new '#' is a new line in matlab bc I can't get this comment to format# #x = dataset(:,1); #y = dataset(:,2); #z = dataset(:,3); #[xx,yy]=meshgrid(x,y); #zz=griddata(x,y,z,xx,yy); %relating z scatter data to the xy grid #v=3:0.5:7; % contour levels: lowest:step interval:highest #idx=randperm(numel(zz)); #n=idx(1:ceil(numel(zz)/2))'; #x=xx(n); #y=yy(n); #z=zz(n); #tri=delaunay(x,y); #subplot(1,1,1) #[C,h]=tricontour(tri,x,y,z,v); #clabel(C) #title 'My Figure Title' – Hg1 Mar 13 '17 at 14:52
-
You are not meant to post extended code into a comment, which by the way is completely unrelated to the question (which is about matplotlib, not matlab). If the above answer solves your problem, you should [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it. If it doesn't, you should edit your question and make clear which further information you are lacking. – ImportanceOfBeingErnest Mar 13 '17 at 15:10
-
Sorry I didn't know about not posting code in the comments. I asked the original question and chose to pick one program to ask about to avoid confusion. However, since I had so much trouble trying to do something so basic, I figured others might have the same question, whether in matlab or matplotlib, so in case someone found this thread, they might appreciate two ways to do the same thing, depending on the program they're more comfortable with. I am very appreciative of the answer! I'm going to compare the two to see which one better represents the system. – Hg1 Mar 13 '17 at 19:04