gpt4 book ai didi

Python散点图二维数组

转载 作者:太空狗 更新时间:2023-10-30 02:44:14 29 4
gpt4 key购买 nike

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

我正在尝试绘制随时间测量的 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()

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

感谢您的帮助。

最佳答案

尝试使用 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

关于Python散点图二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30354788/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com