0

I want to add a color bar to either the top of my figure horizontally under the title or to the whole right side in a vertical position. When I try to add it doesn't look good. Heres a small sample of my code below if needed:

print('Plotting Data....')
# Defining Data in CSV
header = ['X', 'Y', 'AverageEnergy', 'Energy1', 'Energy2', 'Energy3', 'Energy4', 'Energy5', 'norm_X','norm_Y', 'Radius', 'norm_av_energy', 'norm_energy1', 'norm_energy2', 'norm_energy3', 'norm_energy4', 'norm_energy5', 'n_radius', 'new_av_energy', 'new_energy1', 'new_energy2', 'new_energy3', 'new_energy4', 'new_energy5']
df = pd.read_csv('Pinhole_data.csv')
df.columns = header

# defining plotting values
pX = df['norm_X'].values
pY = df['norm_Y'].values
pZ = df['norm_av_energy'].values
num=df['n_radius'].values.sum()
energy_sum=round(sum(pZ))
pupil_fill=round(energy_sum/num,4)
pupil_txt='Pupil Fill Ratio = ' + str(pupil_fill)

# Plot Info
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3,figsize=(12,12))
fig.tight_layout()
Title_info = machineid + ' ' + title + ' ' + pupilfile + ' ' + time
csfont = {'fontname':'Arial'}
fig.suptitle(Title_info, **csfont)

# Average Plot
df_pivot_av = df.pivot(columns='norm_X', index = 'norm_Y', values='new_av_energy')
ax1.imshow(df_pivot_av, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax1.set_title('Average', **csfont)

# Pinhole 1 Plot
df_pivot1 = df.pivot(columns='norm_X', index = 'norm_Y', values='new_energy1')
ax2.imshow(df_pivot1, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax2.set_title('Pinhole 1', **csfont)

# Pinhole 2 Plot
df_pivot2 = df.pivot(columns='norm_X', index = 'norm_Y', values='new_energy2')
ax3.imshow(df_pivot2, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax3.set_title('Pinhole 2', **csfont)

# Pinhole 3 Plot
df_pivot3 = df.pivot(columns='norm_X', index = 'norm_Y', values='new_energy3')
ax4.imshow(df_pivot3, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax4.set_title('Pinhole 3', **csfont)

# Pinhole 4 Plot
df_pivot4 = df.pivot(columns='norm_X', index = 'norm_Y', values='new_energy4')
ax5.imshow(df_pivot4, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax5.set_title('Pinhole 4', **csfont)

# Pinhole 5 Plot
df_pivot5 = df.pivot(columns='norm_X', index = 'norm_Y', values='new_energy5')
ax6.imshow(df_pivot5, extent =[min(pX)-.1,max(pX)+.1,min(pY)-.1,max(pY)+.1], origin = 'lower', cmap=plt.cm.jet)
ax6.set_title('Pinhole 5', **csfont)

# show figure
plt.show()
print('Done.')

enter image description here

Sammy
  • 15
  • 4
  • There is a guide here. https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html – Jody Klymak Mar 15 '21 at 18:40
  • hmm.. i suppose i could redo my whole plotting code to try to look similar to those examples so i can use them.. – Sammy Mar 15 '21 at 18:46
  • I don't think you have to redo anything? `fig.colorbar(im, ax=[ax1, ax2, ax3, ax4, ax5, ax6], shrink=0.6)` should work fine (though you may wish to reconsider using a for loop and something like `values=f'new_energy{ind}')` – Jody Klymak Mar 15 '21 at 19:02

0 Answers0