# 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()
制图的代码是网上找的,自己进行了修改。运行以后图片的窗口会卡死。请大家帮我看一看,谢谢!