….
1.I am making a python code that creates plots of data imported from a CITIfile. I want to run the code such that each plot made will have a different title. For example, plot one will have the title S11 Log Magnitude, the second plot will have the title S12 Log Magntitude, the third plot S12 Log Magnitude, and the fourth plot with the title S22 Log magnitude. The code I have written now will produce titles 0, 1, 2, and 3, using plt.title(str(i)). What modifications can I make to this code so that it will produce the desired plot titles in this sequence?
….
import citidata
import glob
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
keyslist = [] # data name
datalist = [] # data arrays
M = N = 0
all_my_files = glob.glob("*.citi")
for filename in all_my_files:
M += 1
print("=== %s ===" % filename)
citi_file = citidata.genfromfile(filename)
for package in citi_file.packages:
print(package)
print(package.indep)
#print(package.deps) # suppress screen output
for key in package.deps:
N += 1
value = package.deps[key] # get data field
keyslist.append(key) # append key
datalist.append(value['data']) # append np array data
print('\n ', M, 'files read;', N, 'datasets recorded.')
print('dataset : name')
#plt.figure(0)
w = []
x = np.linspace(8, 12, 201)
for i in range(N):
fig = plt.figure(i)
print(i, ':', keyslist[i])
y = datalist[i] # data
# print(y)
test = np.abs(y)
f = sqrt(test)
mag = 20*log10(f)
print(mag)
# [S11, S21, S12,S22]
# y = np.append(mag)
plt.xlabel('Frequancy (Hz')
plt.ylabel('Log Magnitude (dB')
plt.plot(x, mag)
plt.title(str(i))
>Solution :
I think the only way to do this is by dictionary as there is no sequence in the name. Create a dictionary with integer key and the value being the name of the graph in the global scope:
name_dict = {
0: "S11 Log Magnitude"
1: "S12 Log Magntitude"
2: "S12 Log Magnitude"
3: "S22 Log magnitude"
}
After that, you just change the last code line to
plt.title(name_dict[i])
I hope this was helpful!
EDIT 1:
Sorry, I have changed the key number to start from 0.