V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yellowtail
V2EX  ›  Python

实时绘图实在绘不出来..请有了解的帮我看一下哪儿错了

  •  
  •   yellowtail · 2018-04-06 01:13:23 +08:00 · 2499 次点击
    这是一个创建于 2184 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # Uncomment the next two lines if you want to save the animation
    #import matplotlib
    #matplotlib.use("Agg")

    from matplotlib.pylab import *
    from mpl_toolkits.axes_grid1 import host_subplot
    import matplotlib.animation as animation
    import numpy as np
    import matplotlib.pyplot as plt
    import time
    import visa

    rm=visa.ResourceManager()
    print(rm.list_resources())
    #A=rm.open_resource("ASRL5::INSTR")
    A=rm.open_resource('USB0::0x05E6::0x2100::8007164::0::INSTR')
    B=rm.open_resource("ASRL4::INSTR")
    A.write("*RST ")
    A.write("*CLS ")
    B.timeout=25000
    B.write("*RST ")
    B.write("*CLS ")
    B.write(":SENSE:FUNCTION 'RES'")
    B.write(":FORMAT:ELEMENTS RES")

    B.write(":OUTPUT ON")
    #inst.write("CONFigure:RESistance")
    A.write("CONFigure:VOLTage:DC")
    # Sent for figure
    font = {'size' : 9}
    matplotlib.rc('font', **font)

    # Setup figure and subplots
    f0 = figure(num = 0, figsize = (12, 8))#, dpi = 100)
    f0.suptitle("test", fontsize=12)
    ax01 = subplot2grid((2, 2), (0, 0))
    ax02 = subplot2grid((2, 2), (0, 1))
    ax03 = subplot2grid((2, 2), (1, 0), colspan=2, rowspan=1)
    ax04 = ax03.twinx()
    #tight_layout()

    # Set titles of subplots
    ax01.set_title('Votage vs Time')
    ax02.set_title('Resistance vs Time')
    ax03.set_title('Votage and Resistance vs Time')

    # set y-limits
    ax01.set_ylim(1.67,5)
    ax02.set_ylim(0,20)
    ax03.set_ylim(1.67,5)
    ax04.set_ylim(0,20)

    # sex x-limits
    ax01.set_xlim(0,2000)
    ax02.set_xlim(0,2000)
    ax03.set_xlim(0,2000)
    ax04.set_xlim(0,2000)

    # Turn on grids
    ax01.grid(True)
    ax02.grid(True)
    ax03.grid(True)

    # set label names
    ax01.set_xlabel("time")
    ax01.set_ylabel("Votage")
    ax02.set_xlabel("time")
    ax02.set_ylabel("Resistance")
    ax03.set_xlabel("time")
    ax03.set_ylabel("Votage")
    ax04.set_ylabel("Resistance")

    # Data Placeholders
    yp1=zeros(0)
    yv1=zeros(0)
    t=zeros(0)

    # set plots
    p011, = ax01.plot(t,yp1,'b-', label="yp1")


    p021, = ax02.plot(t,yv1,'b-', label="yv1")


    p031, = ax03.plot(t,yp1,'b-', label="yp1")
    p032, = ax04.plot(t,yv1,'g-', label="yv1")

    # set lagends
    ax01.legend([p011], [p011.get_label()])
    ax02.legend([p021], [p021.get_label()])
    ax03.legend([p031], [p031.get_label()])

    # Data Update

    x=0

    def updateData(self):

    global yp1
    global yv1
    global t
    global x

    a1 =float(A.query("READ?"))
    b1 =float(B.query("READ?"))
    yp1=append(yp1,a1)
    yv1=append(yv1,b1)
    t=append(t,x)
    x+=1

    p011.set_data(t,yp1)


    p021.set_data(t,yv1)


    p031.set_data(t,yp1)
    p032.set_data(t,yv1)


    return p011, p021, p031, p032

    # interval: draw new frame every 'interval' ms
    # frames: number of frames to draw
    simulation = animation.FuncAnimation(f0, updateData, blit=False, frames=200, interval=20)

    # Uncomment the next line if you want to save the animation
    #simulation.save(filename='sim.mp4',fps=30,dpi=300)

    plt.show()

    制图的代码是网上找的,自己进行了修改。运行以后图片的窗口会卡死。请大家帮我看一看,谢谢!
    6 条回复    2018-04-06 11:32:45 +08:00
    yellowtail
        1
    yellowtail  
    OP
       2018-04-06 03:01:21 +08:00
    重新调试了一下 发现每次给 a1 和 b1 赋固定的数 是可以实现的 但是调用其他的读数就出问题
    yellowtail
        2
    yellowtail  
    OP
       2018-04-06 03:35:59 +08:00
    又调试了一遍 a1 可以读数 但是只要 b1 读数就卡死
    yellowtail
        3
    yellowtail  
    OP
       2018-04-06 04:03:15 +08:00
    成功了 原因和以前 仪器传输的信号没有添加分隔符\r 程序无法分割信号 造成卡死 是不是可以说图片窗口出来以后 制图的代码就没什么问题了?
    ZoomQuiet
        4
    ZoomQuiet  
       2018-04-06 08:50:38 +08:00 via iPhone
    是也乎 ╮(╯▽╰)╭

    等等…为毛一定要弹窗?
    输出为硬盘图片文件不好嘛?
    xmoiduts
        5
    xmoiduts  
       2018-04-06 09:03:19 +08:00 via Android
    先将就着用,遇到问题再查也来得及。但是硬件(串口)传数据有可能缺字节导致数据异常,要做好判定机制。
    @ZoomQuiet 说不定是无线抄表上位机呢 /滑稽
    bravecarrot
        6
    bravecarrot  
       2018-04-06 11:32:45 +08:00 via iPhone
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5257 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:18 · PVG 17:18 · LAX 02:18 · JFK 05:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.