[英]How to add Matplotlib Colorbar Ticks
堆棧溢出有很多 matplotlib 顏色條問題,但我無法理解它們以解決我的問題。
如何在顏色欄上設置 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()
更新刻度和刻度標簽:
cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])
一個帶有五個刻度的工作示例(對於任何值范圍)是:
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])
treenick 回答讓我開始了,但是如果您的顏色條在 0 和 1 之間縮放,那么如果您的fields
未在 0 和 1 之間縮放,則該代碼不會 plot 刻度。所以我使用了
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]
你可以嘗試類似的東西
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()
根據Eryk Sun
的回答,僅使用:
cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])
將 map 標記mn
、 md
和mx
到 0 和 1 之間的區間。例如,如果變量mn,md,mx
是0,1,2
則只顯示mn
和md
。
相反,首先定義刻度標簽,然后 map 顏色條刻度在 0 和 1 之間:
import numpy as np
ticklabels = ['a', 'b', 'c', 'd']
cbar.set_ticks(np.linspace(0, 1, len(ticklabels)))
cbar.set_ticklabels(ticklabels)
這會工作
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.