I recently started working with colorbars. I had a colorbar that looked like this:
Now I'd like to change all the values $x$ next to it, to $10^x$, but keep the new values at the original location of $x$. I tried to do that by using : cbar.set_ticks([10**t for t in cbar.ax.get_yticks()])
Now the colorbar code part looks like this:
kaart = ax.contourf(lons, lats, np.log10(Ui),cmap=plt.cm.BuGn, transform = ccrs.PlateCarree())
cbar =plt.colorbar(kaart)
cbar.set_label( label='Uncertainty in celcius', size = 20)
cbar.set_ticks([10**t for t in cbar.ax.get_yticks()])
But the resulting colorbar looks like :
How can I keep the labels at their original place? Thanks in advance :)
CodePudding user response:
You are changing the value of the ticks doing that, so maybe:
ticks = cbar.ax.get_yticks()
mylabels = [f'{10**t}' for t in ticks]
cbar.set_ticks(ticks, labels=mylabels)
However, if you are really plotting a logarithm of your data, consider contouring/pcolor on the original data and using LogNorm:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
data = 10**np.random.randn(10, 10)
fig, ax = plt.subplots()
pc = ax.pcolormesh(data, norm=mcolors.LogNorm(vmin=10**-3, vmax=10**3))
fig.colorbar(pc)
plt.show()
CodePudding user response: