1

I have an array A of shape (N,2) containing coordinate positions (in my case they are nodes coordinates positions x,y) and another array B of shape (N,1) containing the corresponding field values at the nodes in array A (in my case they are velocities). I would like to convert array B to a matrix in order to have an imshow() representation of the field at the nodes.

I have tried an alternative method (below) to plot my field using an interpolation on a meshgrid but the result isn't what am expecting but it the codes take too much time to compute the interpolations.

I think it would be easier to use imshow() method but i can't convert the field vector to a matrice

def plotField(field,coord):
    xcoord = coord[:,0] #x coordinate
    zcoord = coord[:,1] #z coordinate
    x,z=np.meshgrid(xcoord,zcoord)
    y = gd((xcoord,zcoord),field,(x,z),method='nearest') #field interpolation
    plt.pcolormesh(x,z,field)
    plt.show()

NB : In matlab one can do this with the patch command but i don't know it's equivalent in python. I found this question while searching which i think advanced python users should take a look to provide an answer ploting filled polygons in python

lefloxy
  • 181
  • 7
  • Given your _Nota Bene_ , it is not clear whether you want to plot your field as a grid or as polygons. – Delforge Dec 23 '17 at 12:23
  • @Delforge I want it as a grid because I think its more general. I added the NB because it is more specific to when your coordinates form polygons with field values for each polygon face. – lefloxy Dec 23 '17 at 12:48
  • Sorry for my previous comment. The approach is not correct. You need to create a meshgrid of the coordinates you want to interpolate on, not the ones you have in your array. There are by the way enough questions showing how to do that. If you are not interested in plotting a rectangular grid, you may use `tricolor`. – ImportanceOfBeingErnest Dec 23 '17 at 12:52
  • @ImportanceOfBeingErnest I don't understand what you mean by "the coordinates you want to interpolate on". what is the difference between those coordinates and the coordinates in my arrays? – lefloxy Dec 23 '17 at 13:00
  • You are right. I have no idea what you have in your array. In that sense the question is too unclear to be answered anyways. – ImportanceOfBeingErnest Dec 23 '17 at 13:02
  • @ImportanceOfBeingErnest I think I said in my questions that I have 2 arrays 1) has node coordinates (x,y) and the other 2) has velocity values at the nodes. Please can you tell if there something you don't understand in my question i would try to make more clear. – lefloxy Dec 23 '17 at 13:07
  • If you intend to just plot your points (x, y) as a single filled Polygon, the example you provided is already clear about that, but we shall break it down for you. As for the `velocity values at the nodes`, it is unclear to me. – IMCoins Dec 23 '17 at 13:09
  • Look at how other question's answers did it. In addition to the marked duplicate, you may look at [this question](https://stackoverflow.com/questions/43155211/plotting-interpolated-3d-data-as-a-2d-image-using-matplotlib), or [this one](https://stackoverflow.com/questions/18764814/make-contour-of-scatter) (just use pcolormesh or imshow instead of contour). Again, you may also consider using `tripcolor` for a general non-gridded output, i.e. triangles). If you need help with your special case and none of those links help, provide a [mcve]. Also see [ask]. – ImportanceOfBeingErnest Dec 23 '17 at 13:10
  • @IMCoins I think shouldn't have posted the example because it draws your attention. what I want is an algorithm to convert to convert my field (velocity) array in to a matrix in order to be able to use the matplotlib imshow method. – lefloxy Dec 23 '17 at 13:28

0 Answers0