1

I need to contour x, y , z data. Below is a minimal working example:

x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] z = [20, 40, 60, 80, 100]

So, z = 20 is at the coordinate (1, 2), z = 40 is at the coordinate (2, 4) and so on. In reality, I have longitude and latitude coordinates with soil conductivity values at each point in space.

I would like to contour using plt.contour() or something similar to this. However, z has to be a 2d array. I am fairly fluent in python. But, I don't fully grasp how to create meshes.

Every example of contour example I have seen, z is calculated from a function of x and y.

This is my first question in the community, so please advise if my question needs to be more thorough.

Things I tried: enter image description here enter code here

This raises an error that z must be a 2d array. When I try using:z = np.reshape(z, (-1, 2), then, x isn't the same shape. Other people have used meshing techniques as well?

How can I make z a 2d array for contouring? Should I reshape all of the data?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
jt_geophys
  • 11
  • 2
  • If the Z data is in a 2D array, [`plt.contour`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.contour.html) can be called without the need of X and Y. X and Y are either 2D with the same shape as Z. Or they can be 1D, to fit each of the 2 directions of Z. Test it with your data, with the official docs as reference. – JohanC Apr 23 '20 at 23:44
  • Hi, thank you for your response. I have updated my question to be more specific. I just don't know the proper way to make z a 2d array. – jt_geophys Apr 25 '20 at 11:52
  • You can use numpy to convert a 1D array to 2D if you know the dimensions: `Z = np.array(Z).reshape(n,m)`. – JohanC Apr 25 '20 at 12:00
  • Thanks JohanC. I have tried a version of what you are suggesting and it then said that x and y are not the same shape as z. I will try your suggestion. – jt_geophys Apr 26 '20 at 16:27

1 Answers1

0

What you posted is not a count our plot; it's a few points on a single line. Your description does imply that z is a 2D array: for each pair in X x Y, you have a Z value.

Simply follow the examples you already found, or see the matplotlib documentation. Where they compute Z values, you simply hard-code or read in your 2D z array. Feed that to the plot function; that function doesn't care where you get the values, just the you supply them.

Prune
  • 76,765
  • 14
  • 60
  • 81