I'm trying to customize how my plot looks so I can then save it as an image. I have everything pretty much set about the plot and its colorbar. With the colorbar arguments, I made it to be horizontal and placed it at the top of the main plot by defining the axis for the colorbar. The default position of the units of the colorbar are set at the bottom of it and I wish for them to be at the top to save some space, but I can't find how. Here is my code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
img = ax.imshow(data, cmap='gray', origin='lower')
# positioning colorbar
cax = fig.add_axes([0.2235, 0.93, 0.58, 0.03])
fig.colorbar(img, orientation='horizontal', cax=cax)
fig.savefig('map.png', bbox_inches='tight')
To summarize, the units of the colorbar appear in between the colorbar and the main plot. I want to relocate the units to be at the top of the colorbar (if its at all possible). Any help is greatly appretiated.
You only need to pass ticklocation
to colorbar
:
fig.colorbar(img, orientation='horizontal', cax=cax, ticklocation='top')