我想在颜色栏中添加顶部和底部值。 所以范围的最大值是我的数据中的最大数量,而底部是我的数据中的最小数量
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
data = np.genfromtxt("E:\\data\\data1.txt", delimiter="\t")
minVal = np.min(data[np.nonzero(data)])
maxVal = np.max(data[np.nonzero(data)])
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='Purples',
interpolation='nearest',
norm=matplotlib.colors.LogNorm(),
vmin=minVal,vmax = maxVal,
)
cbar = fig.colorbar(im)
print("min ",minVal)
print("max", maxVal)
plt.show()
答案 0 :(得分:0)
这里是一种使用一些随机数据进行演示的解决方案。这个想法如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
data = np.random.randint(0, 50000, (50,50))
minVal = np.min(data[np.nonzero(data)])
maxVal = np.max(data[np.nonzero(data)])
print (minVal, maxVal)
# 4, 49983
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='Purples', interpolation='nearest',
norm=matplotlib.colors.LogNorm(), vmin=minVal,vmax = maxVal,)
cbar = fig.colorbar(im)
# Get the default ticks and tick labels
ticklabels = cbar.ax.get_ymajorticklabels()
ticks = list(cbar.get_ticks())
# Append the ticks (and their labels) for minimum and the maximum value
cbar.set_ticks([minVal, maxVal] + ticks)
cbar.set_ticklabels([minVal, maxVal] + ticklabels)