简体   繁体   中英

How to plot pixels instead of points in matplotlib?

I have a set of points {(x1,y1),(x2,y2),....,(xn,yn)} whit x_i and y_i integers for all i. I wish to plot them, but instead of points I'd like them to be square pixels with center in the point and side length 1, starting 1/2 to the left of the point and ending 1/2 to the right...For example, with the point (1,1) should be: 点(1,1)

What you need is imshow . A small example, where each square is a pixel (a very small image of 3x3). The color is given by the data values of the array. If data has only 0 and 1, it will be 2 colors only.

import matplotlib.pyplot as plt

data = np.random.random(size=(3, 3))

plt.imshow(data, interpolation='nearest')
plt.xticks(np.arange(0.0, 2.5, 1), np.arange(0.5, 2, 0.5))
plt.yticks(np.arange(2, -0.5, -1), np.arange(0.5, 2, 0.5))

现在的例子

Notice the interpolation='nearest' that causes it does not interpolate the points, but rather shows us squares.

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