简体   繁体   中英

How to use pcolor (or imshow) to plot color mapped squares centered on each X,Y scatter point

I have several thousand points with X,Y,C values (in numpy arrays).

I want each X,Y point to be plotted on a 2D image plot with a colored square around it (a box of size 40x40 units). Each X,Y point should be centered in the middle of the box. The colour of the box will be mapped according to the C value. The X,Y points are fairly randomly spaced. The points are arranged so that no boxes will overlap, they may touch, or have gaps.

I'm not a Python expert so would appreciate if someone could help get me started on this with a few lines of code. I believe that something like imshow or pcolor will be needed.

Thanks,

You can simply set up the size and marker type in the scatter command.

That'd be my solution:

X = 50 * np.round(10 * np.random.rand(100))
Y = 50 * np.round(10 * np.random.rand(100))
C = np.random.rand(100)

plt.figure(figsize=(12, 12))
sc = plt.scatter(X, Y, s=40**2, c=C, marker='s', cmap='gist_rainbow')
plt.scatter(X, Y, s=11**2, c='k')
plt.colorbar(sc)
plt.axis('equal')
plt.show()

The output would be the following:

输出

Hope that helps!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM