python 中的快速而肮脏的条形图?

发布于 2024-07-13 22:53:48 字数 825 浏览 9 评论 0原文

我有一些 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) 的分辨率才能捕获一些较短的脉冲。 对于多秒图表,使用此方法需要非常很长时间。

I have some python code that receives a message every so often containing a timestamp and an edge transition, either low-to-high, or high-to-low. I'd like to graph each transition on a stripchart for a quick and dirty visualization of the digital waveform with the minimal amount of effort.

Can you recommend any methods or packages that would make this easy?

I'm also not opposed to exporting the data in, for instance, csv format and loading it into another program if that would be easier.

Edit:

Tried 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 )

This pinned my CPU at 100% for more than 10 minutes and was steadily eating memory. I killed it before it used up all of swap. Am I doing something wrong or is CairoPlot just broken?

Further edit:

I now have something more workable using CairoPlot, based loosely on the above code. However, it's not perfect because of the resolution: I may need up to tens of nanoseconds (1e-8) resolution in order to catch some of the shorter pulses. For a multi-second graph, this takes a very long time with this method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

绝對不後悔。 2024-07-20 22:53:48

我自己没有使用过它,但也许 开罗情节值得一看。

I haven't use it myself, but perhaps Cairo Plot is worth taking a look at.

无远思近则忧 2024-07-20 22:53:48

Matplotlib 可能有效。 看看这个条形图演示

Matplotlib might work. Take a look at this strip chart demo.

憧憬巴黎街头的黎明 2024-07-20 22:53:48

您可以尝试使用 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_

you may try using 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 )

For more information, check CairoPlot

Edit:

I didn't understand your function fn(t) here. The idea of the function_plot is to plot a function not a vector.

To plot those points, you could use function_plot on this way:

#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 )

I guess that will work

For the 100% pinning CPU, that shouldn't happen... I'll take a look at it later today. Thanks for pointing it
\o_

归途 2024-07-20 22:53:48
#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
#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
郁金香雨 2024-07-20 22:53:48

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

http://bitworking.org/projects/sparklines/ provides a tiny graph for you.

走走停停 2024-07-20 22:53:48

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

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

GnuPlot is a the old reliable answer here, easy graphing with lots of options. I believe there are python bindings but it's probably easier to export your data and run it through regular gnuplot. Here's an ancient quick start doc for it.

I'm also using matplotlib with great success for larger data sizes.

帅的被狗咬 2024-07-20 22:53:48

对于仅使用 tkinter 的实时条形图应用程序(不需要外部包),请参阅 wxPython 最好的实时绘图小部件是什么?

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

For a realtime stripchart application using only tkinter (no external packages required), see What is the best real time plotting widget for wxPython?.

If I understand your question, you are receiving messages in realtime with nanosecond resolution timestamps, but you don't expect to see 10^9 messages per second. If the average message rate is low (100 messages per second or fewer), I'd just ignore the timestamp and plot the transitions one message at a time. If the graph timescale is 10ms per pixel, 4 transitions would be drawn over 40ms, but at least you wouldn't miss seeing that something happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文