【问题标题】:Quick and dirty stripchart in python?python中快速而肮脏的条形图?
【发布时间】:2010-10-08 02:37:40
【问题描述】:

我有一些 python 代码,它经常接收包含时间戳和边缘转换的消息,无论是从低到高还是从高到低。我想在条形图上绘制每个转换,以便以最少的努力快速而肮脏地可视化数字波形。

您能推荐任何可以简化此操作的方法或软件包吗?

我也不反对以例如 csv 格式导出数据并将其加载到另一个程序中,如果这样更容易的话。

编辑:

尝试过 CairoPlot:

>>> data = [(10, 0), (11, 1), (12.5, 0), (15, 1)]
>>> def fn(t):
...     for d in data:
...             if t > d[0]:
...                     return d[1]
...     return data[-1][1]
...
>>> CairoPlot.function_plot( 'tester.png', data, 500, 300, discrete = True, h_bounds=( data[0][0],data[-1][0]), step = 1 )

这将我的 CPU 固定在 100% 的状态超过 10 分钟,并且一直在消耗内存。我在它用完所有交换之前杀死了它。是我做错了什么还是 CairoPlot 刚刚坏了?

进一步编辑:

我现在有了使用 CairoPlot 的更可行的东西,大致基于上面的代码。然而,由于分辨率的原因,它并不完美:我可能需要高达数十纳秒 (1e-8) 的分辨率才能捕捉到一些较短的脉冲。对于多秒图,使用此方法需要 非常 很长时间。

【问题讨论】:

  • 好的,编辑了我的答案以尝试使用您的数据,希望现在可以使用!
  • 那么,您需要精确到纳秒并且想要绘制几秒钟的数据?这肯定是个问题,至于 1 分,您将获得 1e9 分...我想我很难理解您要在此图形上显示的内容...
  • 我关心的不是完全准确、视觉上可辨别的纳秒分辨率,而是图形算法需要在该分辨率下检查新边缘,以免错过短脉冲,如下所示: [(10.0, 0), (10.00000001, 1), (10.00000002, 0), (11.0, 1), (12.0, 0)]
  • 对于我上次评论中的数据,我希望看到一个如此短的脉冲,以至于它实际上只是 10 处的 1 像素垂直线。

标签: python graphing stripchart


【解决方案1】:

我自己没用过,不过Cairo Plot或许值得一看。

【讨论】:

    【解决方案2】:

    Matplotlib 可能会起作用。看看这个strip chart demo

    【讨论】:

      【解决方案3】:

      您可以尝试使用 CairoPlot:

      import CairoPlot
      
      #the data list stands for your low-to-high (1) and high-to-low (0) data
      data = lambda x : [0,0,1,1,0,0,1][x]
      CairoPlot.function_plot( 'Up_and_Down', data, 500, 300, discrete = True, x_bounds=( 0,len(data) - 1 ), step = 1 )
      

      欲了解更多信息,请查看CairoPlot

      编辑:

      我不明白你的函数 fn(t) 在这里。 function_plot 的想法是绘制一个函数而不是一个向量。

      要绘制这些点,您可以这样使用 function_plot:

      #notice I have split your data into two different vectors,
      #one for x axis and the other one for y axis
      x_data = [10, 11, 12.5, 15]
      y_data = [0, 1, 0, 1]
      
      def get_data( i ):
          if i in x_data :
              return y_data[x_data.index(i)]
          else :
              return 0
      
      CairoPlot.function_plot( 'Up_and_Down', get_data, 500, 300, discrete = True, x_bounds=( 0,20 ), step = 0.5 )
      

      我想这会起作用

      对于 100% 固定 CPU,不应该发生这种情况……我今天晚些时候再看看。谢谢指点 \o_

      【讨论】:

      • 顺便说一句,我注意到 function_plot 的文档字符串声称您可以将 {"label":fn()} 的字典传递给它以获取多个标记图。但是,当我这样做时,它会抱怨我没有给它一个可调用对象。
      • 另外,看起来你是 CairoPlot 的维护者?您可能有兴趣知道它在 python 2.4 中中断,因为 max 仅在 2.5 中添加了 kwargs 支持。
      • @Jorenko man,你用的是trunk版还是1.1版?如果是主干,函数字典应该可以工作(我得看看)。如果是v1.1,文档字符串肯定是错误的。
      • @Jorenko 哦,是的,我是维护者。而且我知道那个 2.4 错误。必须找到一种方法来修复它......
      【解决方案4】:
      #simple stripchart using pygame
      import pygame, math, random                   
      white=(255,255,255)                             #define colors                      
      red=(255,0,0)
      pygame.init()                                   #initialize pygame
      width=1000
      height=200
      size=[width,height]                             #select window size
      screen=pygame.display.set_mode(size)            #create screen
      pygame.display.set_caption("Python Strip Chart")
      clock=pygame.time.Clock()
      data=[]
      for x in range (0,width+1):
              data.append([x,100])
      
      # -------- Event Loop -----------
      while (not pygame.event.peek(pygame.QUIT)):     #user closed window
              for x in range (0,width):
                      data[x][1]=data[x+1][1]       #move points to the left
              t=pygame.time.get_ticks()            #run time in milliseconds
              noise=random.randint(-10,10)         #create random noise
              data[width][1]=100.-50.*math.sin(t/200.) +noise #new value
              screen.fill(white)                         #erase the old line
              pygame.draw.lines(screen, red, 0,data,3)   #draw a new line
              clock.tick(150)                            #regulate speed
              pygame.display.flip()                  #display the new screen
      pygame.quit ()                                #exit if event loop ends
      

      【讨论】:

        【解决方案5】:

        http://bitworking.org/projects/sparklines/ 为您提供了一个小图表。

        【讨论】:

          【解决方案6】:

          GnuPlot 是一个古老的可靠答案,可以通过很多选项轻松绘制图表。我相信有 python 绑定,但导出数据并通过常规 gnuplot 运行它可能更容易。这是一个古老的quick start doc

          我还在使用matplotlib,在处理更大的数据量方面取得了巨大成功。

          【讨论】:

            【解决方案7】:

            对于仅使用 tkinter(不需要外部包)的实时条形图应用程序,请参阅 What is the best real time plotting widget for wxPython?

            如果我理解您的问题,您正在实时接收具有纳秒分辨率时间戳的消息,但您不希望每秒看到 10^9 条消息。如果平均消息速率较低(每秒 100 条消息或更少),我将忽略时间戳并一次绘制一条消息的转换。如果图形时间尺度是每像素 10 毫秒,则将在 40 毫秒内绘制 4 次转换,但至少您不会错过看到发生的事情。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-05
              • 1970-01-01
              • 1970-01-01
              • 2010-12-02
              • 1970-01-01
              • 1970-01-01
              • 2010-09-11
              相关资源
              最近更新 更多