Kernel: Python 3 (CoCalc)
In [1]:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes import glob import re import yaml import matplotlib matplotlib.rcParams['pdf.fonttype'] = 42 label_fontsize = 9 legend_fontsize = 8 title_fontsize = 20 line_width = 2 #insets inset_label_fontsize = 10 inset_marker_size = 6 #annotations annotation_fontsize = 10 inset_annotation_fontsize = 8 #tick sizes tick_sizes = 8 inset_tick_sizes = 8
Useful methods
In [2]:
def extract_float(filename): match = re.search(r"([0-9]*\.?[0-9]+)\.dat", filename) return float(match.group(1)) if match else float('inf')
Load data for Panel (a)
In [3]:
###list all C_t_kappa files and sort by kappa pattern = 'C_t_kappa_' C_t_kappa = sorted(glob.glob("data/C_t_kappa/C_t_kappa_*.dat"), key=extract_float) green_colors = ['#F7FCF5', '#DDEEDB','#BFE7B7','#8FD08A','#66BD63','#31A354','#006D2C','#00441B'] green_colors = green_colors[::-1] #load the theory C_t_kappa_th = np.loadtxt('data/C_t_kappa/C_t_kappa_theory.txt',skiprows=1) #load the inset data C_k_list_rep_0p3, C_k_list_rep_0p43, C_k_list_rep_0p6 = np.loadtxt('data/Res_data.dat', unpack=True) # make sure gamma_LR is positive C_k_list_rep_0p3, C_k_list_rep_0p43, C_k_list_rep_0p6 = -1.0*C_k_list_rep_0p3, -1.0*C_k_list_rep_0p43, -1.0*C_k_list_rep_0p6 kappa_list = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
Load data for Panel (b)
In [4]:
###list all C_t_Rep files and sort by Rep pattern = 'C_t_Rep_' C_t_Rep = sorted(glob.glob("data/C_t_Rep/C_t_Rep_*.dat"), key=extract_float) ###list of colors blue_colors = ['#E6F1FA', '#DBE9F6', '#BAD6EB', '#89BEDC', '#539ECD', '#2B7BBA', '#0B559F', '#08306B'] blue_colors = blue_colors[::-1] #load the inset data loaded_data = np.genfromtxt('data/kappa_data.dat', skip_header=1) #split loaded_C_k_list_kappa_0p2 = loaded_data[:, 0] loaded_C_k_list_kappa_0p4 = loaded_data[:, 1] loaded_C_k_list_kappa_0p6 = loaded_data[:, 2] # Remove NaN values if we want the original array lengths back C_k_list_kappa_0p2 = loaded_C_k_list_kappa_0p2[~np.isnan(loaded_C_k_list_kappa_0p2)] C_k_list_kappa_0p4 = loaded_C_k_list_kappa_0p4[~np.isnan(loaded_C_k_list_kappa_0p4)] C_k_list_kappa_0p6 = loaded_C_k_list_kappa_0p6[~np.isnan(loaded_C_k_list_kappa_0p6)] # keep gamma_LR positive C_k_list_kappa_0p2 = -C_k_list_kappa_0p2 C_k_list_kappa_0p4 = -C_k_list_kappa_0p4 C_k_list_kappa_0p6 = -C_k_list_kappa_0p6 #set the Rep rep_list_kappa_0p2 = np.array([0.2, 0.3, 0.4, 0.6, 0.8, 1.0]) rep_list_kappa_0p4 = np.array([0.2, 0.4, 0.6, 0.8, 1.0]) rep_list_kappa_0p6 = np.array([0.2, 0.4, 0.6, 0.8, 1.0]) #load the theory # Open and load YAML file with open("data/Ck_values.yaml", "r") as f: theory = yaml.safe_load(f)
Linear fit
In [5]:
plt.figure(figsize=[3.5,3]) plt.plot(kappa_list, C_k_list_rep_0p3, '--s', markersize = inset_marker_size , color='blue',label=r'$Re_p=0.30$') plt.plot(kappa_list, C_k_list_rep_0p6, '--*', markersize = inset_marker_size , color='red',label=r'$Re_p=0.60$') plt.plot(kappa_list, C_k_list_rep_0p43, '--v', markersize = inset_marker_size , color='green',label=r'$Re_p=0.43$') plt.plot([0], -theory['C_k_theory_0p3'], 's', color='orange', markersize = 15) #inset_a.grid('on') plt.ylim([0, 0.04]) plt.xlabel(r'$\kappa$', fontsize=inset_label_fontsize,labelpad=0.1) plt.ylabel(r'$\gamma_{LR}/Re_p$', fontsize=inset_label_fontsize) #inset_a.legend(ncol=1,loc='upper center',bbox_to_anchor=(0.7,1.8),fontsize=legend_fontsize) plt.annotate(r'$Theory$',xy=(0.12, 0.036), xytext=(0.6,0.035),arrowprops=dict(color='orange',arrowstyle='<|-',lw=2), fontsize=inset_annotation_fontsize,color='orange',ha='right'); # ----- fit for a linear function of the data ----- kappa_all = np.concatenate([ kappa_list, kappa_list, kappa_list, np.array([0]), np.array([0]), np.array([0]) # kappa=0 ]) # all C(k) Ck_all = np.concatenate([ C_k_list_rep_0p3, C_k_list_rep_0p6, C_k_list_rep_0p43, np.array([-theory['C_k_theory_0p3']]), np.array([-theory['C_k_theory_0p3']]), np.array([-theory['C_k_theory_0p3']]) ]) # linear fit slope, intercept = np.polyfit(kappa_all, Ck_all, 1) print("Slope =", slope) print("Intercept =", intercept) # plot fitted line kappa_fit = np.linspace(0, 0.75, 100) Ck_fit = slope * kappa_fit + intercept plt.plot(kappa_fit, Ck_fit, '--', color='black', label='Fit line') plt.legend() plt.show()
Out[5]:
Slope = -0.0388961703571168
Intercept = 0.03504676909866383
Plot
In [6]:
scaling_factor = 1 # fig, axs = plt.subplots(1, 2, figsize=(7.3*scaling_factor, 4.5*scaling_factor), constrained_layout=True, sharey=True) fig, axs = plt.subplots( 1, 2, figsize=(7.3 * scaling_factor, 4.1 * scaling_factor), constrained_layout=False, sharey=True ) C_kappa = [] kappas = [] #Panel (a) for j, file in enumerate(C_t_kappa): time_C_array, C_array = np.loadtxt(file, unpack=True) kappa = extract_float(file) C_kappa.append(C_array) kappas.append(kappa) axs[0].plot(time_C_array, C_array, color=green_colors[j+1],linewidth=line_width,label=rf'$\kappa = {kappa}$') #theory axs[0].plot(C_t_kappa_th[:,0],C_t_kappa_th[:,1],c='red',ls='--',label=r'$\text{Theory}$',linewidth=line_width,zorder=0) axs[0].set_xlabel('$t \dot{\gamma}$',fontsize=label_fontsize) axs[0].set_ylabel(r'$C$',fontsize=label_fontsize) #axs[0].legend(fontsize=legend_fontsize,bbox_to_anchor=(0.25, 1.25), loc='upper center',ncol=2) #axs[1].set_title(rf'$Re_p=0.2$',fontsize=title_fontsize); axs[0].axhline(0.0,c='grey',alpha=0.5,zorder=0); axs[0].set_xlim(0,) axs[0].annotate(r"$\kappa ↑ \quad \text{Re}_p=0.43$",xy=(50, 0.25),xytext=(500.0, 1.2), arrowprops=dict(arrowstyle="<|-",color="black",lw=1.5, shrinkA=10), fontsize=annotation_fontsize,color='black',ha='left', va='center', bbox=dict(boxstyle="round,pad=0.3",facecolor='lightblue',edgecolor='lightblue',alpha=0.6)) ###inset of the panel a inset_a = inset_axes(axs[0], width="50%", height="40%", loc='upper right', borderpad=1.2) inset_a.plot(kappa_list, C_k_list_rep_0p3, '--s', markersize = inset_marker_size , color='blue',label=r'$\text{Re}_p=0.30$') inset_a.plot(kappa_list, C_k_list_rep_0p6, '--*', markersize = inset_marker_size , color='red',label=r'$\text{Re}_p=0.60$') inset_a.plot(kappa_list, C_k_list_rep_0p43, '--v', markersize = inset_marker_size , color='green',label=r'$\text{Re}_p=0.43$') inset_a.plot([0], -theory['C_k_theory_0p3'], 's', color='orange', markersize = 10) # fitted line plotting inset_a.plot(kappa_fit, Ck_fit, '--', color='black', label='Linear fit') #inset_a.grid('on') inset_a.set_ylim([0, 0.045]) inset_a.set_xlabel(r'$\kappa$', fontsize=inset_label_fontsize,labelpad=0.1) plt.ylabel(r'$\gamma_{LR}/\text{Re}_p$', fontsize=inset_label_fontsize) #inset_a.legend(ncol=1,loc='upper center',bbox_to_anchor=(0.7,1.8),fontsize=legend_fontsize) inset_a.annotate(r'$\text{Theory}$',xy=(0.12, 0.036), xytext=(0.7,0.035),arrowprops=dict(color='orange',arrowstyle='<|-',lw=2), fontsize=inset_annotation_fontsize,color='orange',ha='right'); C_rep = [] Reps = [] #Panel (b) for j, file in enumerate(C_t_Rep): time_C_array, C_array = np.loadtxt(file, unpack=True) Rep = extract_float(file) C_rep.append(C_array) Reps.append(Rep) axs[1].plot(time_C_array, C_array, color=blue_colors[j+1],linewidth=line_width,label=rf'$\text{{Re}}_p = {Rep}$') axs[1].set_xlabel('$t \dot{\gamma}$',fontsize=label_fontsize) axs[1].set_ylabel(r'$C$',fontsize=label_fontsize) #axs[1].legend(fontsize=legend_fontsize,bbox_to_anchor=(0.25, 1.34), loc='upper center',ncol=2) #axs[1].set_title(rf'$\kappa=0.2$',fontsize=title_fontsize); axs[1].axhline(0.0,c='grey',alpha=0.5,zorder=0); axs[1].set_xlim(0,) axs[1].annotate(r"$\text{Re}_p ↑ \quad \kappa=0.2$",xy=(50, 0.25),xytext=(360.0, 1.2), arrowprops=dict(arrowstyle="-|>",color="black",lw=1.5, shrinkA=10), fontsize=annotation_fontsize,color='black',ha='left', va='center', bbox=dict(boxstyle="round,pad=0.3",facecolor='lightblue',edgecolor='lightblue',alpha=0.6)) ###inset of the panel b inset_b = inset_axes(axs[1], width="50%", height="40%", loc='upper right', borderpad=1.2) inset_b.set_xlabel(r'$\text{Re}_p$',fontsize=inset_label_fontsize,labelpad=0.1) inset_b.set_ylabel(r'$\gamma_{LR}/\text{Re}_p$',fontsize=inset_label_fontsize) #plot the results inset_b.plot(rep_list_kappa_0p2, C_k_list_kappa_0p2/rep_list_kappa_0p2, '--s', markersize = inset_marker_size , color='blue',label='$\\kappa=0.2$'); inset_b.plot(rep_list_kappa_0p4, C_k_list_kappa_0p4/rep_list_kappa_0p4, '--v', markersize = inset_marker_size , color='green',label='$\\kappa=0.4$'); inset_b.plot(rep_list_kappa_0p6, C_k_list_kappa_0p6/rep_list_kappa_0p6, '--*', markersize = inset_marker_size , color='red', label='$\\kappa=0.6$'); #plot the theory inset_b.plot([0], -theory['C_k_theory_0p3'], 's', color='orange', markersize = 10)#, label='Theory') inset_b.hlines( y = -theory['C_k_theory_0p3'], # 水平线的 y 值 xmin = inset_b.get_xlim()[0], xmax = inset_b.get_xlim()[1], color = "orange", linestyles = "--", linewidth = 2 ) inset_b.set_ylim([0, 0.045]) # xx = np.linspace(0.2, 1, len(Ck_fit)) # inset_b.plot(xx, 0.2*slope+intercept+0*xx , '--', color='blue', label='Linear fit') # inset_b.plot(xx, 0.4*slope+intercept+0*xx , '--', color='green', label='Linear fit') # inset_b.plot(xx, 0.6*slope+intercept+0*xx , '--', color='red', label='Linear fit') #inset_b.grid('on') # inset_b.legend(ncol=1,loc='upper center',bbox_to_anchor=(0.7,1.725),fontsize=legend_fontsize) # inset_b.annotate(r'$\text{Theory}$',xy=(0.12, 0.036), xytext=(0.85,0.035),arrowprops=dict(color='orange',arrowstyle='<|-',lw=2), # fontsize=inset_annotation_fontsize,color='orange',ha='right'); #panel labels axs[0].text(-350,4.0,r'$(a)$'); axs[1].text(-210,3.75,r'$(b)$'); #tick sizes axs[0].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks axs[1].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks inset_a.tick_params(axis='both', labelsize=inset_tick_sizes) inset_b.tick_params(axis='both', labelsize=inset_tick_sizes) # After all plots are drawn # --- Collect handles and labels --- handles_main_a, labels_main_a = axs[0].get_legend_handles_labels() handles_inset_a, labels_inset_a = inset_a.get_legend_handles_labels() handles_main_b, labels_main_b = axs[1].get_legend_handles_labels() handles_inset_b, labels_inset_b = inset_b.get_legend_handles_labels() # --- Create separate legend boxes, anchored to the figure --- fig.legend(handles_main_a, labels_main_a, loc='upper center', bbox_to_anchor=(0.2, 0.95), ncol=2, fontsize=legend_fontsize,labelspacing=0.75, borderpad=0.5, handletextpad=0.5, borderaxespad=0.2, frameon=True, fancybox=True) fig.legend(handles_inset_a, labels_inset_a, loc='upper center', bbox_to_anchor=(0.4, 0.95), ncol=1, fontsize=legend_fontsize, labelspacing=0.58, borderpad=0.5, handletextpad=0.5, borderaxespad=0.2, frameon=True, fancybox=True) fig.legend(handles_main_b, labels_main_b, loc='upper center', labelspacing=0.48, borderpad=0.5, handletextpad=0.5, borderaxespad=0.2, bbox_to_anchor=(0.65, 0.95), ncol=2, fontsize=legend_fontsize, frameon=True, fancybox=True) fig.legend(handles_inset_b, labels_inset_b, loc='upper center',labelspacing=0.6, bbox_to_anchor=(0.85, 0.95), ncol=1, fontsize=legend_fontsize, borderpad=0.5, handletextpad=0.5, borderaxespad=0.2, frameon=True, fancybox=True) # --- Adjust top spacing to make room for all legends --- fig.subplots_adjust(top=0.7) fig.subplots_adjust(wspace=0.275) plt.savefig('FIG3_NEW3.pdf', dpi=600, pad_inches=0.2) plt.show()
Out[6]:
In [7]:
plt.figure(figsize=[3,3]) plt.plot(rep_list_kappa_0p2, C_k_list_kappa_0p2/rep_list_kappa_0p2, '--s', markersize = inset_marker_size , color='blue',label='$\\kappa=0.2$'); plt.plot(rep_list_kappa_0p4, C_k_list_kappa_0p4/rep_list_kappa_0p4, '--v', markersize = inset_marker_size , color='green',label='$\\kappa=0.4$'); plt.plot(rep_list_kappa_0p6, C_k_list_kappa_0p6/rep_list_kappa_0p6, '--*', markersize = inset_marker_size , color='red', label='$\\kappa=0.6$'); plt.ylim([0, 0.04]) # #panel labels plt.xlabel(r'$Re_p$', fontsize=inset_label_fontsize,labelpad=0.1) plt.ylabel(r'$\gamma_{LR}/Re_p$', fontsize=inset_label_fontsize) #tick sizes axs[0].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks axs[1].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks # --------- normalized plot ---------- plt.figure(figsize=[3,3]) plt.plot(rep_list_kappa_0p2, C_k_list_kappa_0p2/rep_list_kappa_0p2/(slope*0.2 + intercept), '--s', markersize = inset_marker_size , color='blue',label='$\\kappa=0.2$'); plt.plot(rep_list_kappa_0p4, C_k_list_kappa_0p4/rep_list_kappa_0p4/(slope*0.4 + intercept), '--v', markersize = inset_marker_size , color='green',label='$\\kappa=0.4$'); plt.plot(rep_list_kappa_0p6, C_k_list_kappa_0p6/rep_list_kappa_0p6/(slope*0.6 + intercept), '--*', markersize = inset_marker_size , color='red', label='$\\kappa=0.6$'); plt.ylim([0, 2]) # #panel labels plt.xlabel(r'$Re_p$', fontsize=inset_label_fontsize,labelpad=0.1) plt.ylabel(r'$\gamma_{LR}/Re_p/(slope*\kappa+intercept)$', fontsize=inset_label_fontsize) #tick sizes axs[0].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks axs[1].tick_params(axis='both', labelsize=tick_sizes) # both x and y ticks
Out[7]:
In [0]:
In [0]: