Python散点图 二维数组

15

我试图做一些看起来很简单的事情,但似乎无法使其正常工作。

我想绘制16个字节值随时间变化的曲线。我尝试使用散点图: x轴为测量指数 y轴为字节索引 颜色表示字节的值。

我已经将数据存储在一个numpy数组中,其中data [2] [14]会给我第二次测量中第14个字节的值。

每次我尝试绘制它,要么出现:

ValueError: x and y must be the same size
IndexError: index 10 is out of bounds for axis 0 with size 10

这是我正在使用的样本测试:

import numpy
import numpy.random as nprnd
import matplotlib.pyplot as plt

#generate random measurements
# 10 measurements of 16 byte values
x = numpy.arange(10)
y = numpy.arange(16)
test_data = nprnd.randint(low=0,high=65535, size=(10, 16))

#scatter plot the measurements with
# x - measurement index (0-9 in this case)
# y - byte value index (0-15 in this case) 
# c = test_data[x,y]

plt.scatter(x,y,c=test_data[x][y])
plt.show()

我确定是我做错了一些愚蠢的事情,但似乎无法弄清楚是什么。

感谢您的帮助。

1个回答

13

尝试使用meshgrid来定义你的点位置,并且不要忘记正确地对NumPy数组进行索引(使用[x,y]而不是[x][y]):

x, y = numpy.meshgrid(x,y)
plt.scatter(x,y,c=test_data[x,y])
plt.show()

在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接