我使用matplotlib在telnet上迭代一组测量值……我用telnetlib设置测量值,然后读入数据,并将其设置为绘图。问题是,我想用工程符号来表示(指数是三的倍数,所以,千,兆,千兆,等等)。但是,我不能使这工作…请看下面的代码…你会注意到我已经注释了我最近的尝试强制工程符号调用一个我手动定义的函数。在
有什么想法吗?在
import os
import telnetlib
import time
import csv
import sigan
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from decimal import Decimal
def eng(num):
return Decimal(num).normalize().to_eng_string()
print("What board revision is this?") # This name will be appended to the output files
board=input()
print("How long should I wait for the measurements to settle each time in seconds?") # This name will be appended to the output files
rest=int(input())
## Setup the Telnet link ##
HOST = "192.168.0.118"
tn = telnetlib.Telnet(HOST, "5024")
## Initialize the SA to a known State ##
sigan.initialize_sa(tn)
frequencies = [["30","100"],["100","500"],["500","1000"],["1000","2398"],["2485","3000"],["3000","3500"],["3500","4000"],["4000","4500"],["4500","5000"],["5000","5500"],["5500","6000"]]
frequencies = [[str.encode(c) for c in s] for s in frequencies]
tn.write(b'disp:wind:trac:y:rlev -25\r\n')
tn.read_until(b'SCPI>')
tn.write(b'pow:aran on\r\n')
tn.read_until(b'SCPI>')
tn.write(b'disp:wind:trac:y:pdiv 3\r\n')
tn.read_until(b'SCPI>')
tn.write(b'trac:type maxh\r\n')
tn.read_until(b'SCPI>')
tn.write(b'band:res 100khz\r\n')
tn.read_until(b'SCPI>')
for i in range(len(frequencies)):
if float(frequencies[1][0])>=1000:
tn.write(b'band:res 1mhz\r\n')
tn.read_until(b'SCPI>')
print('Setting up the Analyzer for ' + str(frequencies [i][0]) + ' to ' + str(frequencies[i][1]) + ' Mhz')
tn.write(b'sens:freq:stop '+frequencies[i][1]+b'mhz\r\n')
tn.read_until(b'SCPI>')
tn.write(b'sens:freq:star '+frequencies[i][0]+b'mhz\r\n')
tn.read_until(b'SCPI>')
print('Working on '+str(frequencies[i][0])[2:-1]+'MHz to ' +str(frequencies[i][1])[2:-1]+'MHz')
print('Letting Maximum values settle out for ' + str(rest) + ' seconds')
time.sleep(rest)
tn.write(b'trac1:upd off\r\n')
tn.read_until(b'SCPI>')
tn.write(b'read:san?\r\n')
data=tn.read_until(b'SCPI>')
tn.write(b'trac1:upd on\r\n')
tn.read_until(b'SCPI>')
data=data[:-7]
data=np.fromstring(data.decode(),float,sep=',')
x = data[::2]
x_line=[30E6,6000E6]
y_line=[-30,-30]
y = data[1::2]
# x = [eng(s) for s in x]
plt.title(board+' at '+str(frequencies[i][0])[2:-1]+'MHz to '+str(frequencies[i][1])[2:-1]+'MHz')
plt.xlabel('Frequency')
plt.ylabel('Conducted Power (dBm)')
plt.ylim(-60,-25)
plt.xlim(float(x[1]),float(x[-1]))
if (y>-30).sum():
failure=mpatches.Patch(color='red', label='Failure')
plt.legend(handles=[failure])
else:
success=mpatches.Patch(color='green', label='Success')
plt.legend(handles=[success])
plt.grid(True)
plt.plot(x,y)
plt.plot(x_line,y_line, color='r', linestyle='dashed', marker='x', markerfacecolor='black', markersize=12, lw=3, label='Threshold')
plt.savefig(board+"_"+str(frequencies[i][0])[2:-1]+"_"+str(frequencies[i][1])[2:-1])
plt.cla()
print("Test Finished and Images Written to '" + os.getcwd() + "'. SUCCESS!")
tn.close()
一种方法是获取刻度值,然后格式化它们,并将新的格式化值设置为标签。请查看here中的格式示例。示例代码可能是
另一个可能有点过火的想法是使用用户定义的格式化程序函数来标记刻度。Here就是一个例子。有了自己的函数格式化程序,您甚至可以返回字符串“kilo”、“Mega”、“atto”或任何您喜欢的字符串,而不是数字“10e-3”。在
相关问题 更多 >
编程相关推荐