4 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
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的示例测试:
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()
Run Code Online (Sandbox Code Playgroud)
我敢肯定这是愚蠢的,我做错了,但我似乎无法弄清楚。
谢谢您的帮助。
尝试使用a meshgrid定义您的点位置,并且不要忘记正确地索引NumPy数组(使用[x,y]而不是[x][y]):
x, y = numpy.meshgrid(x,y)
plt.scatter(x,y,c=test_data[x,y])
plt.show()
Run Code Online (Sandbox Code Playgroud)
