2

I have data files that have the following x, y, z structure:

   -0.2385905666       -0.0131579757       -0.0000000035
   -0.2385905666       -0.0114839591       -0.0000000096
   -0.2385905666       -0.0098099425       -0.0000000249
   -0.2385905666       -0.0081359259       -0.0000000613
   -0.2385905666       -0.0064619093       -0.0000001440

I currently use gnuplot to plot the data:

splot [0:4] [0:7] "xyzData.dat" using ($1):($2):($3) with lines

The resulting plots look like this example.

I want to use Python 3.7/Matplotlib instead. I have tried various surface and contour plotting methods and keep running into a problem with my Z data. For example, I tried this:

plt.contourf(x, y, z, 100)

I get the error below and don't understand why based on similar posts here at Stackoverflow, although I'm guessing it may have something to do with a grid definition:

Input z must be a 2D array.

At any rate, I am hoping that there is a different plotting method that would be more appropriate. If anyone has a newbie-level suggestion about where to start, I would be grateful.


Update: I came across this post: How to do a contour plot from x,y,z coordinates in matplotlib? (plt.contourf or plt.contour)

It almost works, as shown below. However, I can't figure out how to modify the colors or contour spacing to be more similar to my gnuplot example above. If anyone knows, I would be very grateful for any hints! Example

Ants
  • 21
  • 2
  • You need `tricontourf` (or `tricontour`) if the x-y data aren't organized as a grid. The proposed solution via `griddata` is an unnecessary workaround. For the spacing you can use `levels=30` in `tricontourf`. Colors can be supplied via the `cmap=` parameter. The colors of the `gnuplot` example seem to be just a repetition of standard colors, and not very optimal for the given plot. See [here](https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html) for color maps to choose from. – JohanC Jun 13 '20 at 14:48
  • Thank you, @JohanC. I'm beginning to understand. I think my data are indeed in a grid in the context you're referring to. I removed the "f" from "contourf" and now things are looking a bit clearer. I still have not figured out how to change the number of contours/their spacing and I'm working on that now. However, I'm pretty sure that "contour" will do it from my experimentation just now with your suggestion (tricontour and tricontourf generated the error I mentioned earlier, where Z needs to be in 2-D form). Thanks for your help. – Ants Jun 13 '20 at 15:21
  • Well, it is `contour` which complains when x, y and z are not in 2D format. `tricontour` expects three 1D arrays. If your data already is in a grid, but only available as 1D arrays, you can use `x = np.array(x).reshape(height, width)` (similar for y and z). Then you can use `contour(x, y, z)`. – JohanC Jun 13 '20 at 15:37
  • Thanks, Johan. I tried that (I came across a post where someone used it). I got an error and unfortunately, have forgotten what it was because I have tried other things since; however, I managed to get "contour" to work for me. I have no idea why it works, but I'm very happy it does. I'll post an update once I have finished. Thanks very much for your help. – Ants Jun 13 '20 at 15:43
  • `reshape` will complain when the `size` isn't equal to `width*height`. And if you try `reshape` without `np.array(x)` there would be a problem in case `x` wouldn't be already be a numpy array. – JohanC Jun 13 '20 at 15:49

0 Answers0