11import math
2- from omnetpp .scave import results , chart , utils , ideplot
2+ from omnetpp .scave import results , chart , utils , ideplot , vectorops , delta_measurement
33import matplotlib .pyplot as plt
44import pandas as pd
55import ast
@@ -128,6 +128,9 @@ def plot_vectors(df, props, legend_func=utils.make_legend_label, global_style=No
128128 - `cycle_seed`: Alters the sequence in which colors and markers are assigned to series.
129129 """
130130 log_chart_name (logger , props )
131+
132+ x_unit = "s" # vectime is always simtime
133+ y_unit = utils ._check_same_unit (df )
131134 p = ideplot if chart .is_native_chart () else plt
132135
133136 props ['plot_function' ] = inspect .currentframe ().f_code .co_name
@@ -141,6 +144,28 @@ def get_prop(k):
141144 df .sort_values (by = 'order' , inplace = True )
142145 else :
143146 df .sort_values (by = legend_cols , inplace = True )
147+
148+ # X unit
149+ target_x_unit = get_prop ("xaxis_unit" )
150+ if target_x_unit is None :
151+ target_x_unit = x_unit
152+ elif target_x_unit == "" and x_unit :
153+ target_x_unit = utils ._get_best_unit (df .vectime , x_unit )
154+ if x_unit != target_x_unit and x_unit :
155+ df .vectime = utils ._convert_to_unit (df .vectime , x_unit , target_x_unit )
156+ x_unit = target_x_unit
157+
158+ # Y unit
159+ target_y_unit = get_prop ("yaxis_unit" )
160+ if target_y_unit is None :
161+ target_y_unit = y_unit
162+ elif target_y_unit == "" and y_unit :
163+ target_y_unit = utils ._get_best_unit (df .vecvalue , y_unit )
164+ if y_unit != target_y_unit and y_unit :
165+ df .vecvalue = utils ._convert_to_unit (df .vecvalue , y_unit , target_y_unit )
166+ y_unit = target_y_unit
167+ df ["unit" ] = target_y_unit
168+
144169 for t in df .itertuples (index = False ):
145170 style = utils ._make_line_args (props , t , df )
146171 logger .debug (f"orig style: { style } " )
@@ -161,7 +186,21 @@ def get_prop(k):
161186 title = get_prop ("title" ) or utils .make_chart_title (df , title_cols )
162187 utils .set_plot_title (title )
163188
164- p .ylabel (utils .make_chart_title (df , ["title" ]))
189+ ylabel = utils .make_chart_title (df , ["title" ])
190+ if "xaxis_unit" in props or "yaxis_unit" in props :
191+ utils ._set_xlabel (p , props , x_unit , "Simulation Time" )
192+ utils ._set_xlimits (p , props , x_unit )
193+ utils ._set_ylabel (p , props , y_unit , ylabel )
194+ utils ._set_ylimits (p , props , y_unit )
195+ else :
196+ # backward compatibility for old charts that don't have the xaxis_unit and yaxis_unit properties yet
197+ p .xlabel ("Simulation Time [s]" )
198+ if y_unit is not None :
199+ ylabel += f" [{ y_unit } ]"
200+ p .ylabel (ylabel )
201+
202+ if not ideplot .is_native_plot ():
203+ plt .gca ().delta_measurement = delta_measurement .DeltaMeasurement (plt .gcf (), plt .gca ())
165204
166205def plot_vectors_separate (df , props , legend_func = utils .make_legend_label , global_style = None , share_axes = 'x' ):
167206 """
0 commit comments