1

I have a rather complicated two dimensional function that I represent with few levels using contourf. How do I get the array corresponding exactly to filled contours ?

For example :

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,1,100)
y = np.linspace(0,1,100)
[X,Y] = np.meshgrid(x,y)

Z = X**2 + Y**2

plt.subplot(1,2,1)
plt.imshow(Z, origin = 'lower')
plt.subplot(1,2,2)
plt.contourf(Z,[0,1,2])
plt.show()
plt.savefig('test.png')

enter image description here

I'd like to have the array from the contourplot that returns constant values between the different contours.

So far I did some thresholding:

test = Z
test[test<1] = 0
test[test>=1] = 2
plt.contourf(X,Y,test,[0,1,2])
plt.savefig('test1.png')

But contourf does a much better job at interpolating things. Furthermore, thresholding 'by hand' becomes a bit long when I have multiple contours.

enter image description here

I guess that, because contourf does all the job, there is a way to get this from the contourf object ?

P.S : why does my first piece of code produces subplots of different sizes ?

Liris
  • 1,399
  • 3
  • 11
  • 29
  • Please explain in detail what you mean by "How do I get the array corresponding exactly to filled contours ?" and "I'd like to have the 'flattened' array from the contourplot at the right hand side."? By right hand side, do you mean data points which are colored green in the subplot on the right? – Sheldore Feb 01 '19 at 10:50
  • By right hand side, I mean that in my first image, I have subplot at the left, and one at the right. The latter is the one corresponding to "right hand side'. By "I'd like to have the 'flattened' array from the contourplot at the right hand side.", i mean mostyle what I did with the thresholding by 'hand'. The question is how do I get this from `contourf`, as he does a much better job than just thresholding the array. I'll try to clarify the post. – Liris Feb 01 '19 at 10:55
  • 1
    Possible duplicate of [matplotlib - extracting data from contour lines](https://stackoverflow.com/questions/5666056/matplotlib-extracting-data-from-contour-lines) – Sheldore Feb 01 '19 at 11:01
  • I am marking it as duplicate. Look at the highest voted answer on the above link – Sheldore Feb 01 '19 at 11:01
  • I've seen this post, but from the coordinates of the lines to an array filled between these lines of the appropriate value is not that straightforward, especially is the function is not monotonic. I'll try to figure out something. – Liris Feb 01 '19 at 11:16
  • Please also have a look at the edit in [this post](https://stackoverflow.com/a/18309914/2454357). – Thomas Kühn Feb 01 '19 at 11:19
  • 1
    I'm still not sure I understand the question. The thing with `contourf` is it makes an image at much higher resolution than your input. If instead of `plt.contourf(X,Y,test,[0,1,2])` you try `plt.imshow(test[::-1], vmin=-1, vmax=3)`, you will see what the contour line really is. The actual regions cannot be smoother because the data just does not have that resolution. You can (as I assume `contourf` does) draw a smooth curve from those points (e.g. a spline or whatever), but that is visualization only. – jdehesa Feb 01 '19 at 11:28
  • `contourf` does some interpolation to represent the data at higher resolution. Basically, as `contourf` is doing this whole process of thresholding/interpolation, I just wanted to retrieve directly from its output. But I guess that, seeing all these answers, there is not simple way to do so, such that I'll do it 'by hand'. – Liris Feb 01 '19 at 12:37
  • 1
    In response to your "P.S.": they are different sizes because `imshow` forces an equal aspect ratio on the Axes, while `contourf` does not. To make them the same, add `aspect='equal'` to the second `plt.subplot()` call. – tmdavison Feb 01 '19 at 13:16
  • 2
    The input to this contouring algorithm are 2D arrays of coordinates (x,y) and values (z). The output is one or more sets of x,y coordinate pairs that define a line or polygon shape. Those output values are unrelated to the input coordinates (they may in theory not even lie on *any* grid at all). – ImportanceOfBeingErnest Feb 01 '19 at 13:58

0 Answers0