Skip to content

Commit c4ad328

Browse files
authored
Merge pull request #14810 from rmcdermo/master
Python: add legend, title to fdsplotlib
2 parents ac415d1 + 1f06bb6 commit c4ad328

File tree

1 file changed

+78
-22
lines changed

1 file changed

+78
-22
lines changed

Utilities/Python/fdsplotlib.py

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,32 @@ def dataplot(config_filename,**kwargs):
110110
# print(col_names)
111111
y = E[col_names].values.astype(float)
112112

113+
styles = [c.strip() for c in pp.d1_Style.split('|')]
114+
key_labels = [c.strip() for c in pp.d1_Key.split('|')]
115+
113116
for i, label in enumerate(col_names):
114-
# plot the exp data
115-
f = plot_to_fig(x_data=x, y_data=y[:, i],
116-
data_label=pp.d1_Key,
117-
x_label=pp.Ind_Title,
118-
y_label=pp.Dep_Title,
119-
marker_style=pp.d1_Style,
120-
x_min=pp.Min_Ind,x_max=pp.Max_Ind,
121-
y_min=pp.Min_Dep,y_max=pp.Max_Dep,
122-
legend_location=pp.Key_Position
123-
)
117+
if i==0:
118+
# plot the exp data
119+
f = plot_to_fig(x_data=x, y_data=y[:, i],
120+
data_label=key_labels[i],
121+
x_label=pp.Ind_Title,
122+
y_label=pp.Dep_Title,
123+
marker_style=styles[i],
124+
x_min=pp.Min_Ind,x_max=pp.Max_Ind,
125+
y_min=pp.Min_Dep,y_max=pp.Max_Dep,
126+
legend_location=matlab_legend_to_matplotlib(pp.Key_Position)
127+
)
128+
elif i>0:
129+
f = plot_to_fig(x_data=x, y_data=y[:, i],
130+
figure_handle=f,
131+
data_label=key_labels[i],
132+
x_label=pp.Ind_Title,
133+
y_label=pp.Dep_Title,
134+
marker_style=styles[i],
135+
x_min=pp.Min_Ind,x_max=pp.Max_Ind,
136+
y_min=pp.Min_Dep,y_max=pp.Max_Dep,
137+
legend_location=matlab_legend_to_matplotlib(pp.Key_Position)
138+
)
124139

125140
# plt.figure(f.number) # make figure current
126141
# plt.show()
@@ -144,7 +159,7 @@ def dataplot(config_filename,**kwargs):
144159
marker_style=pp.d1_Style,
145160
x_min=pp.Min_Ind,x_max=pp.Max_Ind,
146161
y_min=pp.Min_Dep,y_max=pp.Max_Dep,
147-
legend_location=pp.Key_Position
162+
legend_location=matlab_legend_to_matplotlib(pp.Key_Position)
148163
)
149164

150165
# get the model results
@@ -161,17 +176,20 @@ def dataplot(config_filename,**kwargs):
161176
version_string = Lines[0].strip()
162177
file1.close()
163178

179+
styles = [c.strip() for c in pp.d2_Style.split('|')]
180+
key_labels = [c.strip() for c in pp.d2_Key.split('|')]
181+
164182
for i, label in enumerate(col_names):
165183
f = plot_to_fig(x_data=x, y_data=y[:, i],
166184
revision_label=version_string,
167185
figure_handle=f,
168186
x_label=pp.Ind_Title,
169187
y_label=pp.Dep_Title,
170-
data_label=pp.d2_Key,
171-
line_style=pp.d2_Style,
188+
data_label=key_labels[i],
189+
line_style=styles[i],
172190
x_min=pp.Min_Ind,x_max=pp.Max_Ind,
173191
y_min=pp.Min_Dep,y_max=pp.Max_Dep,
174-
legend_location=pp.Key_Position,
192+
legend_location=matlab_legend_to_matplotlib(pp.Key_Position),
175193
plot_title=pp.Plot_Title
176194
)
177195

@@ -255,9 +273,11 @@ def plot_to_fig(x_data,y_data,**kwargs):
255273
# convert matlab styles to matplotlib
256274
if kwargs.get('marker_style'):
257275
style = kwargs.get('marker_style')
258-
259276
color,marker,linestyle = parse_matlab_style(style)
260277

278+
if kwargs.get('line_style'):
279+
style = kwargs.get('line_style')
280+
color,marker,linestyle = parse_matlab_style(style)
261281

262282
# other plot parameters
263283
if kwargs.get('data_markevery'):
@@ -367,14 +387,21 @@ def plot_to_fig(x_data,y_data,**kwargs):
367387
if kwargs.get('legend_location')=='outside':
368388
plt.legend(fontsize=legend_fontsize,bbox_to_anchor=(1,1),loc='upper left',framealpha=kwargs.get('legend_framealpha'))
369389
else:
370-
if kwargs.get('show_legend'):
371-
plt.legend(fontsize=legend_fontsize,loc=kwargs.get('legend_location'),framealpha=kwargs.get('legend_framealpha'))
390+
# if kwargs.get('show_legend'):
391+
plt.legend(fontsize=legend_fontsize,loc=kwargs.get('legend_location'),framealpha=kwargs.get('legend_framealpha'))
372392

373-
# plot titles
374-
if kwargs.get('title_fontsize'):
375-
title_fontsize=kwargs.get('title_fontsize')
376-
else:
377-
title_fontsize=default_title_fontsize
393+
# plot title
394+
if kwargs.get('plot_title'):
395+
if kwargs.get('title_fontsize'):
396+
title_fontsize=kwargs.get('title_fontsize')
397+
else:
398+
title_fontsize=default_title_fontsize
399+
400+
plt.text(0.05, 0.95, kwargs.get('plot_title'),
401+
transform=plt.gca().transAxes,
402+
fontsize=title_fontsize,
403+
verticalalignment='top',
404+
horizontalalignment='left')
378405

379406
# set axes and tick properties
380407
ymin=kwargs.get('y_min')
@@ -686,3 +713,32 @@ def __repr__(self):
686713

687714
return plot_parameters
688715

716+
717+
def matlab_legend_to_matplotlib(position):
718+
"""
719+
Convert a MATLAB legend position string to the corresponding matplotlib location string.
720+
721+
Parameters:
722+
position (str): MATLAB-style legend position (e.g., 'northeast', 'southwestoutside')
723+
724+
Returns:
725+
str: Matplotlib-compatible legend location (e.g., 'upper right')
726+
"""
727+
position = position.strip().lower()
728+
mapping = {
729+
'north': 'upper center',
730+
'south': 'lower center',
731+
'east': 'center right',
732+
'west': 'center left',
733+
'northeast': 'upper right',
734+
'southeast': 'lower right',
735+
'southwest': 'lower left',
736+
'northwest': 'upper left',
737+
'northeastoutside': 'center left', # rough equivalent
738+
'northwestoutside': 'center right',
739+
'southeastoutside': 'center left',
740+
'southwestoutside': 'center right',
741+
'best': 'best'
742+
}
743+
return mapping.get(position, 'best')
744+

0 commit comments

Comments
 (0)