Python散点图2维数组

时间:2015-05-20 15:59:59

标签: python arrays numpy matplotlib

我正在尝试做一些我认为应该非常直接的事情,但我似乎无法让它发挥作用。

我正在尝试绘制随时间测量的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 个答案:

答案 0 :(得分:4)

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

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

enter image description here

相关问题