diff --git a/Python_script/MeasurementPlot.py b/Python_script/MeasurementPlot.py new file mode 100644 index 0000000000000000000000000000000000000000..deddcd5e7db0de354b53ce565ff951c8c022186c --- /dev/null +++ b/Python_script/MeasurementPlot.py @@ -0,0 +1,111 @@ +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +import time + +class MeasurementPlot: + def __init__(self, title=''): + self.fig, self.ax1 = plt.subplots(2, figsize=(12, 10)) + self.fig.suptitle("Measurement "+title, color="red") + + # First plot: Phase and magnitude + self.path_collection_phase = self.ax1[0].scatter([], [], c='red', marker='<', label='Phase') + self.magnitude_axis = self.ax1[0].twinx() + self.path_collection_mag = self.magnitude_axis.scatter([], [], c='#3120E0', marker='4', label='Magnitude') + self.equi_axis0 = self.ax1[0].twinx() + self.equi_axis0.spines['right'].set_position(('outward', 40)) + self.path_collection_equi0 = self.equi_axis0.scatter([], [], c='black', marker=".", label='Equilibrium_Indicator') + + self.ax1[0].set_xlabel("TIMESTAMP") + self.ax1[0].set_ylabel("PHASE", color='red') + self.magnitude_axis.set_ylabel("MAGNITUDE", color='#3120E0') + self.equi_axis0.set_ylabel("EQUILIBRIUM_INDICATOR", color='black') + # fix range to 0..31 with some extra margin for plotting + self.equi_axis0.set_ylim(-1, 32) + + self.ax1[0].grid(True, linestyle=":") + all_path_collections = [self.path_collection_phase, self.path_collection_mag, self.path_collection_equi0] + labels = [pc.get_label() for pc in all_path_collections] + self.ax1[0].legend(all_path_collections, labels, loc='lower right') + + # Second plot: Humidity and temperature + self.path_collection_temp = self.ax1[1].scatter([], [], c='blue', marker='p', label="Temperature") + self.humidity_axis = self.ax1[1].twinx() + self.path_collection_hum = self.humidity_axis.scatter([], [], c='green', marker="*", label="Humidity") + self.equi_axis1 = self.ax1[1].twinx() + self.equi_axis1.spines['right'].set_position(('outward', 40)) + self.path_collection_equi1 = self.equi_axis1.scatter([], [], c='black', marker=".", label="Equilibrium_Indicator") + + self.ax1[1].set_xlabel("TIMESTAMP") + self.ax1[1].set_ylabel("TEMPERATURE ", color='blue') + self.humidity_axis.set_ylabel("HUMIDITY", color='green') + self.equi_axis1.set_ylabel("EQUILIBRIUM_INDICATOR", color='black') + self.equi_axis1.set_ylim(-1, 32) + + self.ax1[1].grid(True, linestyle=":") + all_path_collections = [self.path_collection_temp, self.path_collection_hum, self.path_collection_equi1] + labels = [pc.get_label() for pc in all_path_collections] + self.ax1[1].legend(all_path_collections, labels, loc='lower right') + + def draw(self, data_frame, save_pdf=True): + timestamps = data_frame.TIMESTAMP + minimum, maximum = self.get_extended_min_max(timestamps) + self.ax1[0].set_xlim(minimum, maximum) + self.ax1[1].set_xlim(minimum, maximum) + + phases = data_frame.S21_PHASE + minimum, maximum = self.get_extended_min_max(phases) + self.ax1[0].set_ylim(minimum, maximum) + self.path_collection_phase.set_offsets(np.c_[timestamps, phases]) + + magnitudes = data_frame.S21_MAGNITUDE + minimum, maximum = self.get_extended_min_max(magnitudes) + self.magnitude_axis.set_ylim(minimum, maximum) + self.path_collection_mag.set_offsets(np.c_[timestamps, magnitudes]) + + temperatures = data_frame.READBACK_TEMPERATURE + minimum, maximum = self.get_extended_min_max(temperatures) + self.ax1[1].set_ylim(minimum, maximum) + self.path_collection_temp.set_offsets(np.c_[timestamps, temperatures]) + + humidities = data_frame.READBACK_HUMIDITY + minimum, maximum = self.get_extended_min_max(humidities) + self.humidity_axis.set_ylim(minimum, maximum) + self.path_collection_hum.set_offsets(np.c_[timestamps, humidities]) + + self.path_collection_equi0.set_offsets(np.c_[timestamps, data_frame.EQUILIBRIUM_INDICATOR]) + self.path_collection_equi1.set_offsets(np.c_[timestamps, data_frame.EQUILIBRIUM_INDICATOR]) + + plt.show() + if plt.isinteractive(): + self.fig.canvas.draw() + self.fig.canvas.flush_events() + + # add 5 % of the distance between min and max to the range + def get_extended_min_max(self, array): + distance = array.max() - array.min() + return array.min()-0.05*distance, array.max()+0.05*distance + +if __name__ == '__main__': + m = MeasurementPlot() + plt.ion() + measurements = [] + + for i in range(20): + measurement = { + 'TIMESTAMP': i, + 'READBACK_TEMPERATURE': 25 - i, + 'READBACK_HUMIDITY': 10 + 0.1*i, + 'EQUILIBRIUM_INDICATOR': i % 4, + 'S21_PHASE': 20 - 2*i, + 'S21_MAGNITUDE': 0.3*i + } + measurements.append(measurement) + data_frame = pd.DataFrame(measurements) + m.draw(data_frame) + print(str(i)) + time.sleep(0.3) + + print('I am done. ') + plt.ioff() + m.draw(data_frame) diff --git a/Python_script/prototype.py b/Python_script/prototype.py index 17ba5f691b763f562de122455dc16ba44a6beceb..61a9ca679fd2d6d21e6333e798bbdf89ac89a2d9 100755 --- a/Python_script/prototype.py +++ b/Python_script/prototype.py @@ -70,6 +70,8 @@ class Measurements: # csv.dict writer add adda row wise writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() + self.data_collection = [] + self.subplot_tuple = plt.subplots(2, figsize=(12, 10)) with open(self.sweep_file) as file: try: @@ -134,7 +136,8 @@ class Measurements: print('Setpoint: ' + str(next_temp) + ' ' + str(next_hum) + ' | Temp: ' + data.temp + ' °C' + ' | Humid: ' + data.hum + '%' + ' | soaking read nr' + str(len(magnitudes_queue))) - self.write_data(writer, next_temp, next_hum, data, self.cook_up_equi_indicator()) + self.write_data(writer, next_temp, next_hum, data, self.cook_up_equi_indicator())#, + #data_frame, subplot_tuple) if self.temperature_stable and self.humidity_stable and self.magnitude_stable and\ self.phase_stable: @@ -198,8 +201,8 @@ class Measurements: return MeasurementData(int(self.clock.time()), temp, hum, power, frequency, s11, s21, s12, s22) - def write_data(self, writer, target_temp, target_hum, data, equi_indicator): - writer.writerow({ + def write_data(self, writer, target_temp, target_hum, data, equi_indicator):#, data_frame, subplot_tuple): + measurement = { 'TIMESTAMP': data.timestamp, 'TARGET_TEMPERATURE': target_temp, 'READBACK_TEMPERATURE': data.temp, @@ -216,7 +219,11 @@ class Measurements: 'S12_MAGNITUDE': self.calculate_mean_magnitude(data.s12), 'S22_PHASE': self.calculate_mean_phase(data.s22), 'S22_MAGNITUDE': self.calculate_mean_magnitude(data.s22) - }) + } + writer.writerow(measurement) + self.data_collection.append(measurement) + data_frame = pd.DataFrame(self.data_collection) + plot_measurement(data_frame, False, False, self.subplot_tuple) def current_milli_time(self): return int(round(self.clock.time() * 1000)) @@ -280,7 +287,6 @@ def plot_measurement(data_frame, show_blocking_plot, save_pdf=True, subplot_tupl fig, ax1 = subplot_tuple if fig is None or ax1 is None: fig, ax1 = plt.subplots(2, figsize=(12, 10)) - fig.suptitle("Graphical representation of chamber output", color="red") path_collection01 = ax1[0].scatter(data_frame.TIMESTAMP, data_frame.S21_PHASE, c='red', marker='<', label='Phase') twin2_0 = ax1[0].twinx() @@ -326,6 +332,8 @@ def plot_measurement(data_frame, show_blocking_plot, save_pdf=True, subplot_tupl plt.ioff() else: plt.ion() + fig.canvas.draw() + fig.canvas.flush_events() plt.show() def plot_output(output_basename, show_blocking_plot, save_pdf=True):