|
9 | 9 | Collection of functions for plotting and analysis |
10 | 10 | """ |
11 | 11 |
|
| 12 | +import os |
12 | 13 | import sys |
13 | 14 | import math |
14 | 15 | import matplotlib |
@@ -998,3 +999,145 @@ def dataplot(config_filename,**kwargs): |
998 | 999 | print("Error in row {whichrow}, skipping case...".format(whichrow=irow+1)) |
999 | 1000 | continue |
1000 | 1001 |
|
| 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'.") |
0 commit comments