如何在python中使串行数据成为柱状图?

如何在python中使串行数据成为柱状图?,python,numpy,matplotlib,serial-port,histogram,Python,Numpy,Matplotlib,Serial Port,Histogram,我每次得到288个数据值。x轴为350至840,料仓尺寸为1.7。下面给出的数字是y值 [175, 173, 177, 175, 175, 175, 175, 174, 174, 175, 175, 174, 175, 175, 175, 175, 174, 175, 175, 175, 175, 175, 175, 175, 174, 175, 174, 174, 175, 175, 174, 174, 174, 175, 175, 175, 175, 174, 175, 175, 175,

我每次得到288个数据值。x轴为350至840,料仓尺寸为1.7。下面给出的数字是y值

[175, 173, 177, 175, 175, 175, 175, 174, 174, 175, 175, 174, 175, 175, 175, 175, 174, 175, 175, 175, 175, 175, 175, 175, 174, 175, 174, 174, 175, 175, 174, 174, 174, 175, 175, 175, 175, 174, 175, 175, 175, 174, 175, 175, 175, 175, 175, 175, 175, 174, 174, 175, 175, 175, 174, 174, 175, 175, 174, 175, 174, 175, 174, 175, 174, 174, 175, 175, 175, 175, 175, 175, 175, 175, 174, 175, 174, 174, 174, 174, 175, 175, 176, 174, 175, 174, 177, 175, 174, 175, 176, 175, 175, 175, 175, 175, 175, 175, 175, 173, 175, 176, 176, 176, 177, 175, 175, 175, 174, 175, 175, 174, 174, 176, 179, 175, 175, 175, 175, 175, 175, 175, 175, 174, 175, 174, 175, 175, 175, 175, 175, 175, 175, 174, 173, 175, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 175, 176, 177, 175, 174, 174, 175, 175, 175, 175, 172, 176, 176, 178, 176, 175, 175, 175, 174, 174, 174, 177, 176, 176, 176, 176, 176, 175, 176, 176, 174, 175, 175, 174, 174, 174, 175, 175, 175, 175, 175, 174, 174, 174, 175, 175, 175, 175, 175, 175, 175, 175, 174, 174, 175, 175, 175, 174, 174, 174, 175, 175, 174, 174, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 174, 175, 174, 175, 175, 175, 175, 176, 176, 176, 175, 175, 176, 177, 176, 176, 176, 176, 177, 176, 175, 176, 176, 175, 175, 175, 175, 173]
下面是我一直在处理的Python代码。有些被注释掉了,所以你可以看到一些思考过程。大部分都被删除了,然后一次又一次地重新组织

import serial 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation from matplotlib 
import style import time import re

raw_buffer = ''
ydata = []
ydata_changed = False
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=None)



fig = plt.figure() 
ax1 = fig.add_subplot(1,1,1) 
while True:


  waiting = ser.inWaiting()
  if waiting > 0:


        raw_buffer = raw_buffer + \
                     ser.read(waiting).replace('\x00', '').replace('\n', ' ').replace(',', ' ')
        re.sub("[^[0-9]", "", raw_buffer)
        raw_buffer= [ int(x) for x in raw_buffer ]
        print "" + str(raw_buffer)

             #ser.read(waiting).replace('\x00', '').replace('\n', '')





       # idx = raw_buffer.rfind(',')
        #raw_data = raw_buffer[0:idx]
        #raw_buffer = raw_buffer[idx + 1:]


        #raw_list = map(lambda x: int(x), \
                   #filter(lambda x: x != '', raw_data.split(',')))

        #if len(raw_list) > 0:
            #ydata.extend(raw_list)
            #ydata_changed = True

    #if ydata_changed:
        #ydata_changed = False
        #plt(ax, raw_data)
    #print "" + str(raw_buffer)
       ##break
     def animate(i):
      graph_data = raw_list.read()
      lines = graph_data.split('\n')
      xs = []
      ys = []
      for line in lines:
            if len(line) > 1:
                x = (0,288,1)
                y = lines
                xs.append(x)
                ys.append(y)
      ax1.clear()
      ax1.plot(xs, ys)

ani = animation.FuncAnimation(fig, animate, interval=1000) time.sleep(1)

plt.show()
我有“处理”的代码,工作!这不是什么花哨的东西。如果有人愿意帮忙并想看到代码,我可以编辑我的帖子并把它贴上去

在Stackoverflow上似乎没有一个“问题”似乎完全符合我想要做的,只是零碎的。基本上,我希望每一组新数据(大约每秒)都形成一个直方图。如果这不是一条好的途径,我会尝试其他途径。非常感谢


要绘制直方图:

import matplotlib.pyplot as plt 

##here are datas##

sample  =[1,31,52,72,9,53,6,8,54,32,9,7,6,35,7,43,23,31,4,1,0,9,8,7,9,11,12,15,16,123,23,25,26,26,35,44,12,54]

##Here is the Histogram plot##
bins=[0,10 ,20,30,40,50,60]
plt.hist(sample, bins, histtype='bar', rwidth='0.8', color='m',label="sample hist")

plt.xlabel('x values')
plt.ylabel('y values')
plt.title('title line 1\ntitle line 2')
plt.legend()
plt.show()
要从序列号读取:

import serial

ser = serial.Serial('COM5', 9600, timeout=None)

while True:
    data = ser.readline()
这里有一个更新直方图的链接:

要绘制直方图:

import matplotlib.pyplot as plt 

##here are datas##

sample  =[1,31,52,72,9,53,6,8,54,32,9,7,6,35,7,43,23,31,4,1,0,9,8,7,9,11,12,15,16,123,23,25,26,26,35,44,12,54]

##Here is the Histogram plot##
bins=[0,10 ,20,30,40,50,60]
plt.hist(sample, bins, histtype='bar', rwidth='0.8', color='m',label="sample hist")

plt.xlabel('x values')
plt.ylabel('y values')
plt.title('title line 1\ntitle line 2')
plt.legend()
plt.show()
要从序列号读取:

import serial

ser = serial.Serial('COM5', 9600, timeout=None)

while True:
    data = ser.readline()
这里有一个更新直方图的链接:

我不太清楚,但是如果您想为每个新的值输入更新图表,您会怎么做?您可以使用
drawnow
。。。或者看看这篇文章:orI修改了我的代码以适应他们的,并且遇到了麻烦。所以,我决定试试他们的代码,amd甚至在那里也遇到了麻烦。我已经修复了一对,但一个错误示例是:“操作数无法与形状(0、)(10、)一起广播。”我也尝试了drawnow,但仍然没有得到任何与弹出图形的图形接近的结果:(您能否确认您的代码正在工作,并且问题是在新数据到来时更新绘制的直方图?我不知道这样做是否合适,但屏幕截图显示了发生了什么。如果取消对打印的注释,我会得到[175、174、167….]东西。所以我认为它是有效的,它只是没有开发一个图表。接下来我将尝试matplotlib.animation…好的,这里有一个很好的例子:我不确定是否理解得很好,但是如果你想为每个新的值输入更新你的图表?你可以使用
drawnow
…或者看这篇文章:orI已经修改了我的代码以适合他们的所以,我决定试试他们的代码,amd甚至在那里也遇到了问题。我已经修复了一对,但一个错误的例子是:“操作数不能与形状(0,)(10,)”我也尝试了drawnow,但仍然没有得到任何与弹出图形的图形接近的结果(您能否确认您的代码正在工作,并且问题是在新数据到来时更新绘制的直方图?我不知道这样做是否合适,但屏幕截图显示了发生了什么。如果取消对打印的注释,我会得到[175、174、167….]东西。所以我认为它是有效的,它只是没有开发一个图表。接下来我将尝试matplotlib.animation…好的,这里有一个很好的例子:我相信带有示例的直方图是好的。我认为主要的问题是调整串行数据以适合适当的“格式”一个直方图,甚至是一个普通的图形。我将改变我的代码,以显示我迄今为止所做的工作。它还不是一个图形。数据只是以字符串的形式输入的y值。x值只是与输入的数据配对。我相信带样本的直方图是好的。我认为主要问题是调整串行数据以适合直方图或常规图形的正确“格式”。我将更改代码以显示到目前为止我所做的工作。它还不是一个图形。数据以字符串形式仅以y值的形式输入。x值只是与输入的数据配对。