简体   繁体   中英

Ticks plotted with an offset in colorbar

Problem: I am plotting the colorbar using Matplotlib, but these ticks are set at 0.0, 0.1.. to 0.5.

在此处输入图片说明

I wanted to get more intervals in between, but that leads to me having this following problem : Irregularly spaced tick labels.

In this picture, I have marked in red the offset in the ticks.

在此处输入图片说明

Code:

plt.pcolor(data_mod, vmin = 0.01, vmax = 0.5, cmap=cmap)
cb = plt.colorbar(extend='both')
cb.set_label('CPRESS', fontsize=7, labelpad=-10, y=1.05, rotation=0)
tick_locator = ticker.MaxNLocator(nbins = 10)
cb.locator = tick_locator
cb.update_ticks()
plt.imshow(data_mod)

What could I be doing wrong? Would it be possible to make the ticks just on top (starting) of the colors?

I could imagine you want to fix the boundaries of colors shown on the plot and in the colorbar to the values of numpy.linspace(0,.5,11) .

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
cmap = plt.get_cmap("jet",11)

data = np.random.rand(10,10)/2.
norm=matplotlib.colors.BoundaryNorm(np.linspace(0,0.5,11),11)
plt.pcolor(data, norm=norm, cmap=cmap)
cb = plt.colorbar(extend='both', ticks=np.linspace(0,0.5,11))
cb.set_label('CPRESS', fontsize=7, labelpad=-10, y=1.05, rotation=0)

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM