I am using matplotlib.pyplot.pcolor
to plot a 2D grid like the following:
PL.pcolor(array, cmap = PL.cm.YlOrRd, vmin = 0, vmax = 3)
Where array
is just a simple 2D array. That part works fine. But next I try:
PL.hold(True)
PL.scatter(x, y, 'blue')
PL.hold(False)
Where x
and y
are the coordinates where I want a given dot to be plotted. However, instead of getting plotted in the center of the cells of the grid that is plotted with pcolor
, the dot gets to the corner of the cells (no matter which cell I choose).
You can use meshgrid
to generate the mesh of points. If your sampling x and y are uniform, then just add the separation divided by two in each direction. ie if separation is 1, then add 0.5 and subtract one point:
import numpy as np
import matplotlib.pyplot as plt
r = np.arange(10)
p = np.arange(10)
R,P = np.meshgrid(r,p)
data = np.random.random((10,10))
plt.pcolor(R,P,data)
plt.scatter(R[:-1,:-1]+0.5,P[:-1,:-1]+0.5, color = 'blue')
This is in case you use tripcolor
: You can calculate the centroid of your points: triang.triangles
gives you the three point indexes of each triangle, then you calculate the centroid using those three points. ie centroidX = (x1+x2+x3)/3.; centroidY = (y1+y2+y3)/3.;
centroidX = (x1+x2+x3)/3.; centroidY = (y1+y2+y3)/3.;
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
x = np.random.random(10)
y = np.random.random(10)
z = np.random.random(10)
triang = tri.Triangulation(x, y)
plt.tripcolor(triang,z)
centroidX = [x[i].sum()/3. for i in triang.triangles]
centroidY = [y[i].sum()/3. for i in triang.triangles]
plt.scatter(centroidX,centroidY, color = 'blue')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.