用python绘制二进制数据

2024-05-19 08:11:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据看起来像:

data = [1,2,4,5,9](整数递增的随机模式)

我想把它画成一个二进制水平线,这样y=1表示在data中指定的每个x值,否则为零。

我有几个不同的data数组,我想堆栈,类似于这种样式(这是CCD时钟数据,但打印格式看起来很理想)

binary plot

我想我需要为我的数据数组创建一个一个列表,但是如何为数组之外的所有内容指定零值呢?

谢谢


Tags: 数据内容列表data堆栈格式二进制模式
2条回答

你明白了。您可以在数据中指定的任何位置使用1创建列表,在其他位置使用0。有了列表理解,这很容易做到

def binary_data(data):
    return [1 if x in data else 0 for x in range(data[-1] + 1)]

会像这样:

>>> data = [1, 2, 4, 5, 9]
>>> bindata = binary_data(data)
>>> bindata
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1]

现在你所要做的就是策划它。。。或者更好的步骤因为它是二进制数据,step()看起来更好:

import numpy as np
from matplotlib.pyplot import step, show

def binary_data(data):
    return [1 if x in data else 0 for x in range(data[-1] + 1)]

data = [1, 2, 4, 5, 9]
bindata = binary_data(data)
xaxis = np.arange(0, data[-1] + 1)
yaxis = np.array(bindata)
step(xaxis, yaxis)
show()


要绘制堆叠在同一图形上的多个数据数组,可以对binary_data()进行如下调整:

def binary_data(data, yshift=0):
    return [yshift+1 if x in data else yshift for x in range(data[-1] + 1)]

所以现在可以设置yshift参数来移动y轴上的数据数组。E、 g

>>> data = [1, 2, 4, 5, 9]
>>> bindata1 = binary_data(data)
>>> bindata1
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1]
>>> bindata2 = binary_data(data, 2)
>>> bindata2
[2, 3, 3, 2, 3, 3, 2, 2, 2, 3]

假设你有data1data2data3来进行叠加,你会这样:

import numpy as np
from matplotlib.pyplot import step, show

def binary_data(data, yshift=0):
    return [yshift+1 if x in data else yshift for x in range(data[-1] + 1)]

data1 = [1, 2, 4, 5, 9]
bindata1 = binary_data(data1)
x1 = np.arange(0, data1[-1] + 1)
y1 = np.array(bindata1)

data2 = [1, 4, 9]
bindata2 = binary_data(data2, 2)
x2 = np.arange(0, data2[-1] + 1)
y2 = np.array(bindata2)

data3 = [1, 2, 8, 9]
bindata3 = binary_data(data3, 4)
x3 = np.arange(0, data3[-1] + 1)
y3 = np.array(bindata3)

step(x1, y1, x2, y2, x3, y3)
show()

您可以轻松编辑以使其与任意数量的数据数组一起工作:

data = [ [1, 2, 4, 5, 9],
         [1, 4, 9],
         [1, 2, 8, 9] ]

for shift, d in enumerate(data):
    bindata = binary_data(d, 2 * shift)
    x = np.arange(0, d[-1] + 1)
    y = np.array(bindata)
    step(x, y)

show()


最后,如果处理的是不同长度的数据数组(比如[1,2][15,16]),并且您不喜欢图中间消失的图,则可以再次调整binary_data()以将其范围强制设置为数据的最大范围。

import numpy as np
from matplotlib.pyplot import step, show

def binary_data(data, limit, yshift=0):
    return [yshift+1 if x in data else yshift for x in range(limit)]


data = [  [1, 2, 4, 5, 9, 12, 13, 14],
          [1, 4, 10, 11,  20, 21, 22],
          [1, 2, 3, 4, 15, 16, 17, 18] ]

# find out the longest data to plot
limit = max( [ x[-1] + 1 for x in data] )
x = np.arange(0, limit)

for shift, d in enumerate(data):
    bindata = binary_data(d, limit, 2 * shift)
    y = np.array(bindata)
    step(x, y)

show()


编辑:如@ImportanceOfBeingErnest所建议,如果您希望执行databindata的转换,而不必定义自己的binary_data()函数,则可以使用numpy.zeros_like()。叠起来的时候要多加注意:

import numpy as np
from matplotlib.pyplot import step, show

data = [  [1, 2, 4, 5, 9, 12, 13, 14],
          [1, 4, 10, 11,  20, 21, 22],
          [1, 2, 3, 4, 15, 16, 17, 18] ]

# find out the longest data to plot
limit = max( [ x[-1] + 1 for x in data] )
x = np.arange(0, limit)

for shift, d in enumerate(data):
    y = np.zeros_like(x)
    y[d] = 1
    # don't forget to shift
    y += 2*shift
    step(x, y)

show()

可以创建一个全为零的数组,并为数据中的这些元素分配1

import numpy as np

data = [1,2,4,5,9]
t = np.arange(0,data[-1]+1)
x = np.zeros_like(t)
x[data] = 1

然后可以用step函数绘制

import matplotlib.pyplot as plt
plt.step(t,x, where="post")
plt.show()

enter image description here

或者使用where = "pre",这取决于如何解释数据

enter image description here

相关问题 更多 >

    热门问题