Skip to content

Commit 32e02d3

Browse files
committed
Python: update fdsplotlib and pyrolysis.py conversion example
1 parent 9d4904d commit 32e02d3

File tree

4 files changed

+309
-163
lines changed

4 files changed

+309
-163
lines changed

Utilities/Python/fdsplotlib.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Collection of functions for plotting and analysis
1010
"""
1111

12+
import os
1213
import sys
1314
import math
1415
import matplotlib
@@ -998,3 +999,145 @@ def dataplot(config_filename,**kwargs):
998999
print("Error in row {whichrow}, skipping case...".format(whichrow=irow+1))
9991000
continue
10001001

1002+
1003+
def add_version_string(ax, filename, plot_type='linear', scale_x=0.60, scale_y=1.02,
1004+
font_name='Times', font_size=10):
1005+
"""
1006+
Adds a version string to a matplotlib plot.
1007+
1008+
Parameters:
1009+
ax (matplotlib.axes.Axes): The axes to add the version string to.
1010+
filename (str): Path to the version string file.
1011+
plot_type (str): Type of plot ('loglog', 'semilogx', 'semilogy', or 'linear').
1012+
scale_x (float): Scaling factor for X-position.
1013+
scale_y (float): Scaling factor for Y-position.
1014+
font_name (str): Font name for the text.
1015+
font_interpreter (str): Interpreter type (not applicable in matplotlib, kept for compatibility).
1016+
font_size (int): Font size for the text.
1017+
1018+
"""
1019+
if os.path.exists(filename):
1020+
with open(filename, 'r') as file:
1021+
version_str = file.read().strip()
1022+
1023+
x_lim = ax.get_xlim()
1024+
y_lim = ax.get_ylim()
1025+
1026+
if plot_type == 'loglog':
1027+
x_pos = 10**(np.log10(x_lim[0]) + scale_x * (np.log10(x_lim[1]) - np.log10(x_lim[0])))
1028+
y_pos = 10**(np.log10(y_lim[0]) + scale_y * (np.log10(y_lim[1]) - np.log10(y_lim[0])))
1029+
elif plot_type == 'semilogx':
1030+
x_pos = 10**(np.log10(x_lim[0]) + scale_x * (np.log10(x_lim[1]) - np.log10(x_lim[0])))
1031+
y_pos = y_lim[0] + scale_y * (y_lim[1] - y_lim[0])
1032+
elif plot_type == 'semilogy':
1033+
x_pos = x_lim[0] + scale_x * (x_lim[1] - x_lim[0])
1034+
y_pos = 10**(np.log10(y_lim[0]) + scale_y * (np.log10(y_lim[1]) - np.log10(y_lim[0])))
1035+
else:
1036+
x_pos = x_lim[0] + scale_x * (x_lim[1] - x_lim[0])
1037+
y_pos = y_lim[0] + scale_y * (y_lim[1] - y_lim[0])
1038+
1039+
ax.text(x_pos, y_pos, version_str, fontsize=font_size, fontname=font_name, verticalalignment='bottom')
1040+
1041+
1042+
def get_plot_style(style="fds"):
1043+
"""
1044+
Returns a dictionary of plot style parameters based on the specified style.
1045+
1046+
Parameters:
1047+
- style (str): The style to use ('fds', 'paper', etc.). Default is 'fds'.
1048+
1049+
Returns:
1050+
- dict: A dictionary containing plot style parameters.
1051+
"""
1052+
if style == "fds":
1053+
return {
1054+
# Font properties
1055+
"Font_Name": "Times",
1056+
"Font_Interpreter": "TeX",
1057+
"Key_Font_Size": 12,
1058+
"Title_Font_Size": 16,
1059+
"Label_Font_Size": 16,
1060+
"Scat_Title_Font_Size": 14,
1061+
"Scat_Label_Font_Size": 14,
1062+
"Marker_Size": 4,
1063+
"D1_Marker_Size": 4,
1064+
"D2_Marker_Size": 4,
1065+
1066+
# Line properties
1067+
"Line_Width": 1.0,
1068+
"D1_Line_Width": 1.0,
1069+
"D2_Line_Width": 1.0,
1070+
1071+
# Plot properties
1072+
"Plot_Units": "inches",
1073+
"Plot_Width": 5.0,
1074+
"Plot_Height": 3.4,
1075+
"Plot_X": 1.2,
1076+
"Plot_Y": 0.8,
1077+
"Scat_Plot_Width": 4.75,
1078+
"Scat_Plot_Height": 4.75,
1079+
"Scat_Plot_X": 1.00,
1080+
"Scat_Plot_Y": 0.75,
1081+
"Subtitle_Text_Offset": 0.05,
1082+
"VerStr_Scale_X": 0.60,
1083+
"VerStr_Scale_Y": 1.05,
1084+
1085+
# Paper properties
1086+
"Paper_Units": "inches",
1087+
"Paper_Width": 6.5,
1088+
"Paper_Height": 4.5,
1089+
"Scat_Paper_Height": 6.0,
1090+
"Scat_Paper_Width": 6.0,
1091+
1092+
# Print properties
1093+
"Figure_Visibility": "off",
1094+
"Image_File_Type": "-dpdf",
1095+
}
1096+
1097+
elif style == "paper":
1098+
return {
1099+
# Font properties
1100+
"Font_Name": "Helvetica",
1101+
"Font_Interpreter": "TeX",
1102+
"Key_Font_Size": 16,
1103+
"Title_Font_Size": 20,
1104+
"Label_Font_Size": 20,
1105+
"Scat_Title_Font_Size": 14,
1106+
"Scat_Label_Font_Size": 14,
1107+
"Marker_Size": 10,
1108+
"D1_Marker_Size": 10,
1109+
"D2_Marker_Size": 10,
1110+
1111+
# Line properties
1112+
"Line_Width": 1.0,
1113+
"D1_Line_Width": 1.0,
1114+
"D2_Line_Width": 1.0,
1115+
1116+
# Plot properties
1117+
"Plot_Units": "normalized",
1118+
"Plot_X": 0.1500,
1119+
"Plot_Y": 0.1500,
1120+
"Plot_Width": 0.7750,
1121+
"Plot_Height": 0.8150 * 0.95, # Adjusted for exponential y-axis tick labels
1122+
"Scat_Plot_Width": 4.75,
1123+
"Scat_Plot_Height": 4.75,
1124+
"Scat_Plot_X": 0.75,
1125+
"Scat_Plot_Y": 0.75,
1126+
"Subtitle_Text_Offset": 0.05,
1127+
"VerStr_Scale_X": 0.60,
1128+
"VerStr_Scale_Y": 1.05,
1129+
1130+
# Paper properties
1131+
"Paper_Units": "inches",
1132+
"Paper_Width": 8.0,
1133+
"Paper_Height": 6.0,
1134+
"Scat_Paper_Height": 6.0,
1135+
"Scat_Paper_Width": 6.0,
1136+
1137+
# Print properties
1138+
"Figure_Visibility": "on",
1139+
"Image_File_Type": "-dpdf",
1140+
}
1141+
1142+
else:
1143+
raise ValueError(f"Unknown style '{style}'. Please choose 'fds' or 'paper'.")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Matlab to Python conversion
2+
3+
## 1. Use an AI code converter
4+
5+
This step will get you 90% of the way there. Then there usually a few extra steps to get a working script.
6+
7+
## 2. Import `fdsplotlib`
8+
9+
You will use this library for the Matlab equivalent of `plot_style` and `addverstr`. At the top of your script add the following:
10+
11+
```
12+
import fdsplotlib
13+
```
14+
15+
For now, we will assume that you are running your scripts from the `fds/Utilities/Python/` directory where `fdsplotlib` lives. If you need to run your python scripts from another directory, you need to add `fds/Utilities/Python/` to the path or else copy `fdsplotlibl.py` to your working directory.
16+
17+
## 3. Add the plot style parameters
18+
19+
Somewhere above where you make the plots add,
20+
21+
```
22+
plot_style = fdsplotlib.get_plot_style("fds")
23+
plt.rcParams["font.family"] = plot_style["Font_Name"]
24+
plt.rcParams["font.size"] = plot_style["Label_Font_Size"]
25+
# print(plot_style) # uncomment to see list of parameters
26+
```
27+
28+
This will polulate a Python "dictionary" called `plot_style` with all the parameters. To see a list of the parameters uncomment the print statement.
29+
30+
## 4. Add Git version string
31+
32+
Similar to the Matlab plots, we need to add
33+
34+
```
35+
fdsplotlib.add_version_string(ax, git_file, plot_type='linear')
36+
```
37+
38+
where `ax` is the handle of the plot.

0 commit comments

Comments
 (0)