我试图将颜色条上的刻度线与离散颜色对齐,但收效甚微。以下方法有效,但刻度线与颜色不对齐。
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。“最热”或最白的颜色对应于最高测量温度。