diff --git a/dataprocessing/plottools.py b/dataprocessing/plottools.py index 78f083a6f09063a2c8b6397159050eabba4d8a64..c7a7a4c70e1180f77f6bc124d65bc6fb97fa857d 100644 --- a/dataprocessing/plottools.py +++ b/dataprocessing/plottools.py @@ -3,22 +3,38 @@ import numpy as np from .timeseries import * -def plot_timeseries(figure_id, timeseries, plt_linestyle='-'): +def plot_timeseries(figure_id, timeseries, plt_linestyle='-', plt_linewidth=2, plt_color=None): + """ + This function plots either a single timeseries or several timeseries in the figure defined by figure_id. + Several timeseries (handed over in a list) are plotted in several subplots. + In order to plot several timeseries in one plot, the function is to be called several times (hold is activated). + """ plt.figure(figure_id) - if not isinstance(timeseries, list): - plt.plot(timeseries.time, timeseries.values, linestyle=plt_linestyle, label=timeseries.label) + if plt_color: + plt.plot(timeseries.time, timeseries.values, linestyle=plt_linestyle, label=timeseries.label, linewidth=plt_linewidth, color=plt_color) + else: + plt.plot(timeseries.time, timeseries.values, linestyle=plt_linestyle, label=timeseries.label, linewidth=plt_linewidth) plt.gca().autoscale(axis='x', tight=True) plt.legend(loc='lower right') else: for ts in timeseries: plt.subplot(len(timeseries), 1, timeseries.index(ts) + 1) - plt.plot(ts.time, ts.values, linestyle=plt_linestyle, label=ts.label) + if plt_color: + plt.plot(ts.time, ts.values, linestyle=plt_linestyle, label=ts.label, linewidth=plt_linewidth, color=plt_color) + else: + plt.plot(ts.time, ts.values, linestyle=plt_linestyle, label=ts.label, linewidth=plt_linewidth) plt.gca().autoscale(axis='x', tight=True) plt.legend() -def set_time_series_labels(timeseries_list, time_series_labels): - for ts in timeseries_list: - ts.label = time_series_labels[timeseries_list.index(ts)] - +def set_timeseries_labels(timeseries, timeseries_labels): + """ + Sets label attribute of timeseries, later used in plotting functions. + Suitable for single timeseries as well as for several timeseries (handed over in a list). + """ + if not isinstance(timeseries, list): + timeseries.label = timeseries_labels + else: + for ts in timeseries: + ts.label = timeseries_labels[timeseries.index(ts)] diff --git a/dataprocessing/readtools.py b/dataprocessing/readtools.py index 50816497c6e6a21885d2205c08d45658ddfe0960..51376b9b618db8574c44dae58042003ab1bcfcd5 100644 --- a/dataprocessing/readtools.py +++ b/dataprocessing/readtools.py @@ -2,18 +2,22 @@ import numpy as np import pandas as pd from .timeseries import * + def read_timeseries_Modelica(filename, timeseries_names=None): from modelicares import SimRes sim = SimRes(filename) - timeseries_list = [] if timeseries_names is None: # No trajectory names specified, thus read in all print('TBD') else: # Read in specified time series - for name in timeseries_names: - timeseries_list.append(TimeSeries(name, sim(name).times(), sim(name).values())) - return timeseries_list + if not isinstance(timeseries_names, list): + timeseries = TimeSeries(timeseries_names, sim(timeseries_names).times(), sim(timeseries_names).values()) + else: + for name in timeseries_names: + timeseries = [] + timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values())) + return timeseries def read_timeseries_PLECS(filename, timeseries_names=None): diff --git a/examples/Comparison_Modelica_DPsim.py b/examples/Comparison_Modelica_DPsim.py new file mode 100644 index 0000000000000000000000000000000000000000..f87bfca1f1ea8b6f336f168bc24b0385a7428de6 --- /dev/null +++ b/examples/Comparison_Modelica_DPsim.py @@ -0,0 +1,28 @@ +from dataprocessing.readtools import * +from dataprocessing.plottools import * +import matplotlib.pyplot as plt +from plottingtools.config import * + +current_EMT = read_timeseries_Modelica( r"C:\Workspace\ReferenceExamples\Modelica\Synchronous Generator\UnitTest_Eremia_3rdOrderModel_Euler_1ms.mat", ["synchronousGenerator_Park.i[1]"]) + +figure_id = 1 +plt.figure(figure_id, figsize=(12,8)) +set_timeseries_labels(current_EMT, ["EMT"]) +plot_timeseries(figure_id, current_EMT, plt_color=blue) +plt.xlabel('Zeit [s]') +plt.ylabel('Strom [A]') +plt.show(block=True) + +multi_current_EMT = read_timeseries_Modelica(r"C:\Workspace\ReferenceExamples\Modelica\Synchronous Generator\UnitTest_Eremia_3rdOrderModel_Euler_1ms.mat",[["synchronousGenerator_Park.i[1]"],["synchronousGenerator_Park.i[2]"]]) + +figure_id = 2 +plt.figure(figure_id, figsize=(12,8)) +set_timeseries_labels(multi_current_EMT, ["Phase a","Phase b","Phase c"]) +plot_timeseries(figure_id, multi_current_EMT) +plt.xlabel('Zeit [s]') +plt.ylabel('Strom [A]') +plt.show(block=True) + + + +