我试图将颜色条上的刻度线与离散颜色对齐,但收效甚微。以下有效,但刻度线与颜色不对齐。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
import matplotlib.cm as cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.colors as colors
from scipy import ndimage
from PIL import Image
plasma = mpl.colormaps['plasma'].resampled(8)
fig, ax = plt.subplots(figsize=(7, 5))
ax.axis("off")
img = plt.imread(image_file_name)
rotated_img = ndimage.rotate(img, -90)
im = ax.imshow(rotated_img, cmap=plasma)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="10%", pad=0.05)
cb = fig.colorbar(im, cax=cax, orientation="vertical")
print(cb.ax.get_yticks())
cb.set_ticks(cb.ax.get_yticks())
ticks = np.linspace(70,160,num=7)
#cb.set_ticks(ticks)
cb.set_ticklabels([f'{tick:.2f}' for tick in ticks])
print(cb.ax.get_yticks())
产量
而
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
import matplotlib.cm as cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.colors as colors
from scipy import ndimage
from PIL import Image
plasma = mpl.colormaps['plasma'].resampled(8)
fig, ax = plt.subplots(figsize=(7, 5))
ax.axis("off")
img = plt.imread(image_file_name)
rotated_img = ndimage.rotate(img, -90)
im = ax.imshow(rotated_img, cmap=plasma)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="10%", pad=0.05)
cb = fig.colorbar(im, cax=cax, orientation="vertical")
print(cb.ax.get_yticks())
#cb.set_ticks(cb.ax.get_yticks())
ticks = np.linspace(70,160,num=9)
cb.set_ticks(ticks)
cb.set_ticklabels([f'{tick:.2f}' for tick in ticks])
print(cb.ax.get_yticks())
产量
颜色图是从内置的“plasma”cmap 获得的 -
plasma = mpl.colormaps['plasma'].resampled(8)
图像数据只是 RGB 值 - 每个通道为 0 到 255。 “最热”或最白的颜色对应于最高测量温度。
此答案假设您希望将图像中的最低颜色映射到 70,将最高颜色映射到 160。
imshow()
在内部将数据中的最低值映射到颜色图的深紫色,将最高值映射到黄色。我们可以通过设置 vmin 和 vmax 来明确这一点。这些 vmin 和 vmax 将是颜色条中的最低和最高 y 值。刻度被选择为最低值和最高值之间的一系列“漂亮的、四舍五入的”值。这与颜色图中的颜色数量无关。 .get_ticks()
(或.get_yticks()
)可能非常棘手,因为最终的刻度仅在绘图绘制到屏幕上时决定。这就是为什么您会得到 300
值,该值超出了范围,并导致了奇怪的白色区域。
使用
cb.set_ticks()
,您可以设置刻度线的位置。在这种情况下,您希望它们从最低到最高 y 值,每个颜色区域之间有一个刻度。
使用
cb.set_ticklabels()
设置相应的标签。当您想要在图像值(例如 0 到 255 之间)和显示的刻度之间进行转换时,您可以使用这些新值作为标签。
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
from scipy.ndimage import gaussian_filter
# create a dummy test image
img = gaussian_filter(np.random.rand(1000, 1000), 80)
img -= img.min()
img /= img.max()
img = np.array(img * 256, dtype=int)
fig, ax = plt.subplots(figsize=(7, 5))
ax.axis("off")
num_colors = 8
plasma = plt.get_cmap('plasma', num_colors)
vmin = img.min()
vmax = img.max()
im = ax.imshow(img, cmap=plasma, vmin=vmin, vmax=vmax)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="10%", pad=0.05)
cb = fig.colorbar(im, cax=cax)
cb.set_ticks(np.linspace(vmin, vmax, num_colors + 1))
cb.set_ticklabels([f'{t:.2f}' for t in np.linspace(70, 160, num_colors + 1)])
plt.tight_layout()
plt.show()