1

I would like to create a contourf plot from matrix 24x20 from dataframe like below scenario:

img

The snippet of my code is:

fig, ax = plt.subplots(nrows=1, ncols=3 , figsize=(20,10))
plt.subplot(131)
xlist = np.linspace(0, 20, 20)
ylist = np.linspace(0, 24, 24)
X, Y = np.meshgrid(xlist, ylist)
plt.contourf(X, Y, df3, cmap="coolwarm" )
plt.gca().invert_yaxis()
plt.axis('off')

Problem1 is I'm not sure why plot of 'A' printed inversely and I have to use plt.gca().invert_yaxis() based on this answer in order to invert it. I noticed that In 7 is mentioned: plt.imshow() by default follows the standard image array definition where the origin is in the upper left, not in the lower left as in most contour plots. Is it the reason behind of that or am I doing something wrong?

Problem2 I would like to divide matrix into 8 small ones by drawing 4 white lines but wasn't possible by using ax.axvline(x=10, color='w',linewidth=1.5). Based on In 7 it seems likewise plt.imshow() doesn't accept an x and y grid, so it must manually specify the extent [xmin, xmax, ymin, ymax] of the image on the plot. Is there any idea to accomplish that?

Problem3 What's the benefit of Interpolation using scipy.interpolate.griddata or Non-gridded contour using tricontour() or this approach in this answer in my case?

Problem4 when interpolation='nearest' is useful? I noticed the here it's used for finding contour

Note The matrix which has 480 values is available in .csvfile : here

Expected result will be like bellow picture: img

Mario
  • 1,631
  • 2
  • 21
  • 51
  • I'm a bit lost here on what the actual problem is (despite the nice graphics). So maybe you can split your problem into smaller parts. It might e.g. help here to look up and understand `numpy.meshgrid` and `numpy.reshape`. Come up with a toy example of some 12 value table, reshape the columns as 3x4 grid each and see if you can plot a contour of it; until then, don't worry about the rest. – ImportanceOfBeingErnest Jan 31 '19 at 23:15
  • @ImportanceOfBeingErnest Actually I followed your hints and made it simple and after I plotted it , I faced the few problems which I updated in question. I would like to hear from your side about them. – Mario Feb 01 '19 at 23:16
  • 1
    (1) Matplotlib y axes have extend vertically from the lower left corner towards the upper left corner. Hence, if you want to have the lowest value in the upper left corner, you need to invert the axis. This is different for imshow, but you don't have an imshow plot here. (2) `ax.axvline(x=10)` should work. Again, it's not clear what that has to do with an imshow plot, if a contourf plot is desired. (3) You have a rectilinear grid defined by the indices of your matrix. You do not have to interpolate anything. – ImportanceOfBeingErnest Feb 01 '19 at 23:35
  • 1
    (4) In that link the `imshow` is only used to plot the original data as a background to the contour plot. It is irrelevant for the contour finding. – ImportanceOfBeingErnest Feb 01 '19 at 23:35
  • @ImportanceOfBeingErnest Thanks for your short answer just regarding **Problem2** when I use `ax = plt.contourf(X, Y, df3)` and then `ax.axvline(x=10)` I get this error `AttributeError: 'QuadContourSet' object has no attribute 'axhline'` – Mario Feb 01 '19 at 23:48
  • `ax` should be the axes, as in `fig, ax = plt.subplots(..)` If you later reassign something different to that variable `ax` it will not be the same any more. – ImportanceOfBeingErnest Feb 01 '19 at 23:50

0 Answers0