python - matplotlib 颜色条交替顶部底部标签

标签 python matplotlib label colorbar

首先,这是一个 self 回答的问题,因为我相信这在某些情况下会有帮助,例如在 this post作者试图隐藏所有其他标签以避免文本重叠,一种替代方法可能是交替标签位置,以便保留所有标签并避免重叠(如果标签数量不是太多)这也是本文旨在解决的问题:

如何制作具有交替顶部和底部标签的 matplotlib 颜色条?

最佳答案

跳转到一个简单的工作示例:

import numpy
import matplotlib.pyplot as plt

#------------------Get some data------------------
X = numpy.arange(100)
Y = numpy.arange(100)
Z = numpy.arange(100**2).reshape((100,100))

levels=numpy.arange(0,100**2,1000)
ltop=levels[::2]           # labels appear on top
lbot=levels[1:][::2]       # labels appear at bottom

#-----------------------Plot-----------------------
f = plt.figure()
ax = f.gca()

cf = ax.contourf(X,Y,Z,100)
cbar=plt.colorbar(cf,orientation='horizontal',ticks=lbot,drawedges=True)

vmin=cbar.norm.vmin
vmax=cbar.norm.vmax

#-------------Print bottom tick labels-------------
cbar.ax.set_xticklabels(lbot)

#--------------Print top tick labels--------------
for ii in ltop:
    cbar.ax.text((ii-vmin)/(vmax-vmin), 1.5, str(ii), transform=cbar.ax.transAxes, va='bottom', ha='center')

plt.show(block=False)

基本上,底部标签是使用默认方法cbar.ax.set_xticklabels(lbot)绘制的。对于顶部标签,我使用 cbar.ax.text() 手动添加它们。

情节如下: colorbar with top and bottom labels

编辑:对我的答案的重要更新:

当颜色条发生扩展/溢出时,相关端会使用三角形来指示值溢出。在这种情况下,顶线刻度标签需要进行一些调整才能与颜色条部分正确对齐。

默认情况下,三角形大小为颜色条轴的 5%,这用于获取适当的偏移和缩放以对齐标签。

请参阅下面的示例,该示例在两端都有扩展。使用我之前的方法,结果如下所示:

enter image description here

顶行的 2 个末尾数字与三角形的尖端对齐。如果只有一端有延伸,并且轮廓层数较多(>=10 左右),则错位会变得更严重。

修正后的图: enter image description here

这是生成正确绘图的代码:

import numpy
import matplotlib.pyplot as plt

#------------------Get some data------------------
X = numpy.linspace(-1,1,100)
Y = numpy.linspace(-1,1,100)
X,Y=numpy.meshgrid(X,Y)
Z=numpy.sin(X**2)

levels=numpy.linspace(-0.8,0.8,9)

ltop=levels[::2]           # labels appear on top
lbot=levels[1:][::2]       # labels appear at bottom

#-----------------------Plot-----------------------
f = plt.figure()
ax = f.gca()

cf = ax.contourf(X,Y,Z,levels,extend='both')
cbar=plt.colorbar(cf,orientation='horizontal',ticks=lbot,drawedges=True)

#------------Compute top tick label locations------------
vmin=cbar.norm.vmin
vmax=cbar.norm.vmax

if cbar.extend=='min':
    shift_l=0.05
    scaling=0.95
elif cbar.extend=='max':
    shift_l=0.
    scaling=0.95
elif cbar.extend=='both':
    shift_l=0.05
    scaling=0.9
else:
    shift_l=0.
    scaling=1.0

#-------------Print bottom tick labels-------------
cbar.ax.set_xticklabels(lbot)

#--------------Print top tick labels--------------
for ii in ltop:
    cbar.ax.text(shift_l + scaling*(ii-vmin)/(vmax-vmin),
        1.5, str(ii), transform=cbar.ax.transAxes,
        va='bottom', ha='center')

plt.show(block=False)

关于python - matplotlib 颜色条交替顶部底部标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161022/

相关文章:

python - float() 参数必须是字符串或数字,而不是 'zip'

python - matplotlib 未在 python3 中导入

python - 使用 pandas 日期时间图时如何使轴占据多个子图?

JavaFX TableView 父列标签不可见

python - numpy 数组中相邻值的加权和

python - 无法在 OS X 10.10 Yosemite 上安装 Python-MySQL

python - 使用 MLPRegressor 拟合简单数据时遇到问题

python - block 中的 Django 模板翻译是不可能的

C# 使用滚动条控件/事件(没有文本框或窗口滚动)

ios - 使用日期/时间和核心图始终在原点显示标签