在分段式色标中绘制不同颜色之间的分隔线。

huangapple go评论64阅读模式
英文:

Drawing dividers between different colors in a segmented colorbar

问题

I would like to draw black horizontal dividers between the different individual colors in the segmented colorbar.

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()
英文:

在分段式色标中绘制不同颜色之间的分隔线。

I would like to draw black horizontal dividers between the different individual colors in the segmented colorbar.

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()

答案1

得分: 2

这是我的方法,使用for循环中的函数axhline,您可以通过文档中的参数调整线条:

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

plt.plot() # 虚拟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()

图示:
在分段式色标中绘制不同颜色之间的分隔线。

英文:

Here is my method with the function axhline in a for loop, you can adjust the lines with the arguments in the doc :

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()

Figure:
在分段式色标中绘制不同颜色之间的分隔线。

huangapple
  • 本文由 发表于 2023年5月11日 18:57:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定