I'm using matplotlib to iterate a bunch of measurements over telnet...I set up the measurement with telnetlib and then read in the data, and set it up to be plotted. The problem is, I want it in engineering notation (exponents to multiples of three, so, kilo, mega, giga, etc.). However, I cannot make this work...Please see the code below...You'll notice I have commented out my latest attempt to force engineering notation with a call to a function that I defined manually.
Any thoughts on how to do this?
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()