[英]How to add Matplotlib Colorbar Ticks
There are many matplotlib colorbar questions on stack overflow, but I can't make sense of them in order to solve my problem.堆栈溢出有很多 matplotlib 颜色条问题,但我无法理解它们以解决我的问题。
How do I set the yticklabels on the colorbar?如何在颜色栏上设置 yticklabels?
Here is some example code:这是一些示例代码:
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()
Update the ticks and the tick labels:更新刻度和刻度标签:
cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])
A working example (for any value range) with five ticks along the bar is:一个带有五个刻度的工作示例(对于任何值范围)是:
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 answer got me started but if your colorbar is scaled between 0 and 1, that code will not plot the ticks if your fields
is not scaled between 0 and 1. So instead I used 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)
If you want spaced out labels you can do python list indexing like so: assuming skipping every other ticks如果您想要间隔标签,您可以执行 python 列表索引,如下所示:假设跳过每隔一个刻度
ticks = ticks[::2]
labels = labels[::2]
you can try something like你可以尝试类似的东西
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()
Based on the answer of Eryk Sun
, using only:根据Eryk Sun
的回答,仅使用:
cbar.set_ticks([mn,md,mx])
cbar.set_ticklabels([mn,md,mx])
Will map ticks mn
, md
and mx
to the interval between 0 and 1. For example, if the variables mn,md,mx
are 0,1,2
then only mn
and md
will be shown.将 map 标记mn
、 md
和mx
到 0 和 1 之间的区间。例如,如果变量mn,md,mx
是0,1,2
则只显示mn
和md
。
Instead, first define the tick labels and then map the colorbar ticks between 0 and 1:相反,首先定义刻度标签,然后 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)
this would work这会工作
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.