如何添加Matplotlib Colorbar刻度 [英] How to add Matplotlib Colorbar Ticks
本文介绍了如何添加Matplotlib Colorbar刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于堆栈溢出有很多matplotlib颜色条问题,但是我无法理解它们来解决我的问题.
There are many matplotlib colorbar questions on stack overflow, but I can't make sense of them in order to solve my problem.
如何在颜色栏上设置ytick标签?
How do I set the yticklabels on the colorbar?
这是一些示例代码:
from pylab import *
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
f = np.arange(0,101) # frequency
t = np.arange(11,245) # time
z = 20*np.sin(f**0.56)+22 # function
z = np.reshape(z,(1,max(f.shape))) # reshape the function
Z = z*np.ones((max(t.shape),1)) # make the single vector to a mxn matrix
T, F = meshgrid(f,t)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolor(F,T,Z, norm=LogNorm(vmin=z.min(),vmax=z.max()))
plt.xlim((t.min(),t.max()))
mn=int(np.floor(Z.min())) # colorbar min value
mx=int(np.ceil(Z.max())) # colorbar max value
md=(mx-mn)/2 # colorbar midpoint value
cbar=plt.colorbar() # the mystery step ???????????
cbar.set_yticklabels([mn,md,mx]) # add the labels
plt.show()
推荐答案
更新刻度和刻度标签:
cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])
这篇关于如何添加Matplotlib Colorbar刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文