i am trying to plot an current-voltage acquisition from an instrument but for different temperatures. I need to keep the structure as presented but i would like to have for each temperature get the plot in 1 uniform color then for the next temperature the color is changed so i can identify different plot and read their legend (the temperature). right now i get the animation but i cannot iterate on temperatures and colors. thanks
import random
import csv
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
class IV_SWEEP():
def __init__(self):
self.IV_sweep()
def init_instrument(self):
#######settings
self.data_points = 10 # number of steps in the sweep
self.max_current = 0.001
self.min_current = -self.max_current
def measurement(self):
# Allocate arrays to store the measurement results
currents = np.linspace(self.min_current, self.max_current, num=self.data_points)
voltages = np.zeros_like(currents)
temp_list = [300, 310, 320] # want to plot a curve for each of this temperature
for i in range(self.data_points):
voltages[i] = random.random() + currents[i]
now = datetime.now()
self.record_csv(now, currents[i], voltages[i])
self.animate()
self.plot() # i am calling the plot method separately but dont know how to iterate the label so the color and legend is changed per temp
plt.pause(3)
self.plot_enable = True
def IV_sweep(self):
self.xdata = []
self.ydata = []
self.init_instrument()
self.measurement()
def animate(self):
data = pd.read_csv("test.csv")
self.xdata = data["Voltage (V)"]
self.ydata = data["Current (A)"]
def plot(self):
plt.gcf()
temp_list = [300, 310, 320]
labels = temp_list
# for lab in labels:
plt.scatter(self.xdata, self.ydata, label=temp_list) # need to add a different lab for each temperature scan
plt.xlabel('Voltage (V)')
plt.ylabel('Current (mA)')
# plt.title("IV sweep at {} K".format(temp_list[i]))
plt.title("IV sweep at temperature xx") # call the temperature so it s printed as legend
# plt.legend(loc='best')
plt.tight_layout()
return
def record_csv(filename,timestamp, currents, voltages):
filename = "test.csv"
with open(filename, 'a', newline='') as csvfile:
header = ["Timestamp", "Current (A)", "Voltage (V)", "Voltage stdv (V)"]
writer = csv.DictWriter(csvfile, fieldnames=header)
if csvfile.tell() == 0:
writer.writeheader()
writer.writerow(
{
"Timestamp": timestamp,
"Current (A)": currents,
"Voltage (V)": voltages,
}
)
csvfile.close()
IV_SWEEP()