【问题标题】:Title missing in the last subplot最后一个子图中缺少标题
【发布时间】:2020-07-09 09:59:11
【问题描述】:

我创建了包含从示波器获取的测量值的子图。 N 是决定子图数量的参数。 问题是当有 1 个地块时,它没有标题或 y 标签。 当有多个地块时,只影响最后一个地块

##Plots a time trend of the active measurements on a MSO4/5/6
##Pierre Dupont - Tek - 2020

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import visa
import sys

rm = visa.ResourceManager()
scope = rm.open_resource('TCPIP::192.168.1.34::INSTR')

##determines number of active measurements
def active_measurements():
    meas_list=scope.query('MEASUrement:LIST?')
    N=meas_list.count(",")+1
    if "NONE" in meas_list:
        scope.close()
        sys.exit("No measurement found, exiting...")
    return(N)

N=active_measurements()
plots_set={} ##dictionary that will contain the subplots. One per active measurement.
ydata_set={} ##dictionary that will contain the array of logged data
fig=plt.figure()
IDN=scope.query('*idn?')
fig.suptitle('Measurement data logger / connected to: '+IDN,fontsize=10,color='purple')
plt.style.use('ggplot')

##definition of the subplots in the dictionary + subplots titles and axis legend
for i in range (1,N+1):
    plots_set["ax"+str(i)]=fig.add_subplot(N,1,i)
    meas_type=scope.query('MEASUrement:MEAS{}:TYPe?'.format(i))
    plots_set["ax"+str(i)].set_title(meas_type,fontsize='small')
    meas_unit=scope.query('MEASUrement:MEAS{}:YUNIT?'.format(i))
    plots_set["ax"+str(i)].set_ylabel(meas_unit.replace('"',''))
    print(i,meas_type,meas_unit)
    ydata_set["y"+str(i)]=[]


index=0
x=[]

## function that runs at every data log. Appends the arrays of measurement 
## data and updates the subplots
def animate(i):
    global index
    index+=1
    x.append(index)
    t=1
    for k,k2 in zip(ydata_set,plots_set):
        scope.query('*OPC?')
        M=float(scope.query('MEASUrement:MEAS{}:RESUlts:CURRentacq:MEAN?'.format(t)))
        t+=1
        plt.cla()
        ydata_set[k].append(M)
        plots_set[k2].plot(x,ydata_set[k],marker="X",linewidth=0.5,color='blue')


##frames parameter = number of logs // interval parameter = time between 2 logs (in ms)
ani=FuncAnimation(plt.gcf(), animate, frames=1000, interval=500, repeat=False)
plt.tight_layout()
plt.show()

scope.close()

输出:

1 RISETIME
 "s"

2 POVERSHOOT
 "%"

3 MAXIMUM
 "V"

非常感谢您的意见。抱歉,这是我的第一篇文章,内容不够清晰。

【问题讨论】:

  • 请修改您的代码并添加缺少的变量,以便我们自己测试。
  • 也许 scope.query('MEASUrement:MEAS{}:TYPe?'.format(i)) 会为 i == N 返回一个空字符串?也许你应该使用 .format(i-1) ?也许你可以打印它的价值?显然没有提供可以回答您问题的数据。
  • @JohanC 谢谢我添加了一个打印以确保没有返回空字符串
  • plt.cla() 删除当前的斧头。所以,可能它正在清除最后一把斧头。您需要重新设置标题,或者不删除该斧头。
  • for k,k2 in zip(ydata_set,plots_set): 然后使用 ydata_set[k] 绝对不是 Python 的工作方式。它应该更像for k, ax in zip(range(len(ydata_set)), plots_set),然后将其用作ydata_set[k].append(...)ax.plot(...)

标签: python matplotlib title subplot pyvisa


【解决方案1】:

调用plt.cla() 会清除当前的轴,在这种情况下是最近创建的轴。这将清除所有行、标签、标题等。如果你想在你的动画函数中使用plt.cla(),你需要每次都重置它们,例如

def animate(i):
    # ...
    plt.cla()
    plt.set_title("title")
    plt.set_ylabel("label")
    # etc

另一种方法是使用set_data 来更新您的绘图,即

lines = [subplot.plot(x, ydata, marker="X", linewidth=0.5, color='blue')[0] 
         for subplot, ydata in zip(plots_set, ydata)]
def animate(i):
    global index
    index+=1
    x.append(index)
    t=1    
    for ydata, line in zip(ydata_set, lines):
        scope.query('*OPC?')
        M=float(scope.query('MEASUrement:MEAS{}:RESUlts:CURRentacq:MEAN?'.format(t)))
        t+=1
        ydata.append(M)
        line.set_data(x, ydata)

这不需要每次新数据进入时清除整个子图。

【讨论】:

  • 你是对的 plt.cla() 是原因。我只是重置了动画函数中的最后一个绘图参数。谢谢!
  • @PierreDupont 很高兴它有帮助,不要忘记accept 答案,以便将来的用户标记为这样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多