Issue
I'm trying to plot the following and I notice that the labels on the y-axis is all on top of each other.
How can I avoid this?
I am using the code below to make the plots:
plt.figure(figsize=(18, 7))
fig, ax1 = plt.subplots(figsize=(18, 7))
for i in range(0, len(df_data), 1):
ax2 = ax1.twinx()
_df_data = df_data[i][(df_data[i].wav_um > 9.29) & (df_data[i].wav_um < 9.51)].reset_index(drop=True)
ax1.plot(_df_data.wav_um, _df_data.mircat_auc, 'o--', linewidth=1, c = 'blue', mfc= 'lightblue', mec='black', ms=4, alpha=0.7, label=hdf5_file_locations[i])
ax2.plot(_df_data.wav_um, _df_data.thorlabs_pw_mw, 'o--', linewidth=1, c = 'orange', mfc= 'salmon', mec='gray', ms=3, alpha=0.7)
ax1.set_xlabel(r'Wavelength [$\mu m$]', fontsize = 14)
ax1.set_ylabel(r' AUC', fontsize = 14, color='blue')
ax2.set_ylabel(r' power [mW]', fontsize = 14, color='orange')
ax1.tick_params(axis="x", labelsize=15)
ax2.grid(which='both', color='0.9', linestyle='-', linewidth=1)
Solution
I cannot run the code, as the df_data
object is not defined. However just from looking at the code, I can tell you should move the axis cloning ax2 = ax1.twinx()
out of the loop. Right now you create a new twin axis on every iteration, while you actually only need one. So try the following instead:
plt.figure(figsize=(18, 7))
fig, ax1 = plt.subplots(figsize=(18, 7))
ax2 = ax1.twinx()
for i in range(0, len(df_data), 1):
_df_data = df_data[i][(df_data[i].wav_um > 9.29) & (df_data[i].wav_um < 9.51)].reset_index(drop=True)
ax1.plot(_df_data.wav_um, _df_data.mircat_auc, 'o--', linewidth=1, c = 'blue', mfc= 'lightblue', mec='black', ms=4, alpha=0.7, label=hdf5_file_locations[i])
ax2.plot(_df_data.wav_um, _df_data.thorlabs_pw_mw, 'o--', linewidth=1, c = 'orange', mfc= 'salmon', mec='gray', ms=3, alpha=0.7)
ax1.set_xlabel(r'Wavelength [$\mu m$]', fontsize = 14)
ax1.set_ylabel(r' AUC', fontsize = 14, color='blue')
ax2.set_ylabel(r' power [mW]', fontsize = 14, color='orange')
ax1.tick_params(axis="x", labelsize=15)
ax2.grid(which='both', color='0.9', linestyle='-', linewidth=1)
I hope this helps!
Answered By - Axel Donath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.