英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论