Matplotlib: colorbar tick locations not correct
I'm making a protein secondary structure plot, at first with trivial data
import matplotlib.pyplot as plt
fig, ax0 = plt.subplots(1,1, layout = "constrained")
from matplotlib.colors import ListedColormap
colors = \["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f"\]
this_cmap = ListedColormap(colors)`
d = [[][]] # just a bunch of 0s, too big for this
im0 = ax0.imshow(d , cmap = this_cmap, vmax = 7, vmin = 0)
# these tick locations *should* be correct
cbar = fig.colorbar(im0 , ticks = [0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5])
cbar.ax.set_yticklabels(["Loops and irregular elements","Residue in isolated β-bridge","Extended strand, participates in β ladder","3-helix (3/10 helix)","Alpha helix","5 helix (pi helix)","bend","hydrogen bonded turn"])
but the output:
clearly shows that the ticks are not in the correct locations for the majority of the colored sections (all but first and last)
>>> cbar.get_ticks()
array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5])
even shows the exact same numbers that I entered.
How can I get the ticks to show up exactly in the middle of each colored section?