如何添加Matplotlib Colorbar Ticks

时间:2011-07-16 05:00:00

标签: python matplotlib colorbar

堆栈溢出有很多matplotlib colorbar问题,但我无法理解它们以解决我的问题。

如何在彩条上设置yticklabels?

以下是一些示例代码:

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()

6 个答案:

答案 0 :(得分:25)

更新刻度线和刻度标签:

cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])

答案 1 :(得分:5)

沿着条形图有五个刻度的工作示例(对于任何值范围)是:

m0=int(np.floor(field.min()))            # colorbar min value
m4=int(np.ceil(field.max()))             # colorbar max value
m1=int(1*(m4-m0)/4.0 + m0)               # colorbar mid value 1
m2=int(2*(m4-m0)/4.0 + m0)               # colorbar mid value 2
m3=int(3*(m4-m0)/4.0 + m0)               # colorbar mid value 3
cbar.set_ticks([m0,m1,m2,m3,m4])
cbar.set_ticklabels([m0,m1,m2,m3,m4])

答案 2 :(得分:1)

treenick回答让我开始但是如果你的颜色条在0和1之间缩放,那么如果你的fields没有在0和1之间缩放,那么代码就不会绘制刻度线。所以我使用了

m0=int(np.floor(field.min()))            # colorbar min value
m4=int(np.ceil(field.max()))             # colorbar max value
num_ticks = 10
# to get ticks
ticks = np.linspace(0, 1, num_ticks)
# get labels
labels = np.linspace(m0, m1, num_ticks)

如果你想要间隔标签你可以像这样做python列表索引: 假设跳过其他每个滴答

ticks = ticks[::2]
labels = labels[::2]

答案 3 :(得分:1)

您可以尝试类似的

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()))
v1 = np.linspace(Z.min(), Z.max(), 8, endpoint=True)
cbar=plt.colorbar(ticks=v1)              # the mystery step ???????????
cbar.ax.set_yticklabels(["{:4.2f}".format(i) for i in v1]) # add the labels
plt.show()

enter image description here

答案 4 :(得分:0)

基于Eryk Sun的答案,仅使用:

cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])

将图mnmdmx滴到0到1之间的间隔。例如,如果变量mn,md,mx0,1,2,则仅将显示mnmd

相反,首先定义刻度线标签,然后在0到1之间映射颜色条刻度线。

import numpy as np

ticklabels = ['a', 'b', 'c', 'd']
cbar.set_ticks(np.linspace(0, 1, len(ticklabels)))
cbar.set_ticklabels(ticklabels)

答案 5 :(得分:0)

这可以工作

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()))
v1 = np.linspace(Z.min(), Z.max(), 8, endpoint=True)
cbar=plt.colorbar(ticks=v1)              # the mystery step ???????????
cbar.ax.set_yticklabels(["{:4.2f}".format(i) for i in v1]) # add the labels
plt.show()