简体   繁体   English

如何在matplotlib中绘制像素而不是点?

[英]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,我有一组点{(x1,y1),(x2,y2),....,(xn,yn)} whit x_i和y_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/2,到右边的1/2结束......例如,点(1,1)应该是: 点(1,1)

What you need is imshow . 你需要的是imshow A small example, where each square is a pixel (a very small image of 3x3). 一个小例子,其中每个正方形是一个像素(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. 如果数据只有0和1,则只有2种颜色。

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. 注意interpolation='nearest'导致它不插值点,而是显示正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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