matplotlib 在分段颜色条中绘制不同颜色之间的分隔符

mbjcgjjk  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(143)

我想画黑色的水平分隔线之间的不同个别颜色在分段颜色条。

mcve

import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.colors import BoundaryNorm

plt.plot() # dummy Axes

nmin, N = 0, 11
bounds = [nmin-0.5]+[n+0.5 for n in range(nmin, N)]
norm = BoundaryNorm(bounds, 256)
cm = plt.get_cmap('cool')

cb = plt.colorbar(ScalarMappable(norm, cm), format='%02d', ax=plt.gca())
cb.set_ticks(range(nmin, N))

plt.show()
btqmn9zl

btqmn9zl1#

下面是我在for循环中使用函数axhline的方法,你可以用文档中的参数调整行:

import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.colors import BoundaryNorm

plt.plot() # dummy Axes

nmin, N = 0, 11
bounds = [nmin-0.5]+[n+0.5 for n in range(nmin, N)]
norm = BoundaryNorm(bounds, 256)
cm = plt.get_cmap('cool')

cb = plt.colorbar(ScalarMappable(norm, cm), format='%02d', ax=plt.gca())
cax = cb.ax
for bound in bounds:
    cax.axhline(bound, c='k', linewidth=0.4)
cb.set_ticks(range(nmin, N))

plt.show()

图:

相关问题