matplotlib colorbar交替使用顶部和底部标签 [英] matplotlib colorbar alternating top bottom labels

查看:170
本文介绍了matplotlib colorbar交替使用顶部和底部标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是一个自我解答的问题,因为我认为这在某些情况下会有所帮助,例如在此帖子中,作者尝试过要隐藏每个其他标签以避免文本重叠,一种替代方法可能是更改标签位置,以便保留所有标签并避免重叠(如果没有疯狂的标签数量),这篇文章旨在解决的问题:

First of all, this was intended as a self-answer question, because I believe it would be helpful in certain situations, e.g. in this post the author tried to hide every other label to avoid texts overlapping, one alternative might be to alternate the label position so that one retains all labels and avoids overlapping (if there isn't a crazy number of labels) as well, which is what this post aims to solve:

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

How to make matplotlib colorbar with alternating top and bottom labels?

推荐答案

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

Jump to a simple working example:

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()手动添加了它们.

Basically the bottom labels are plotted using the default method cbar.ax.set_xticklabels(lbot). For the top labels, I added them manually using cbar.ax.text().

情节看起来像这样:

The plot looks like this:

对我的答案的重要更新:

当颜色栏扩展/溢出时,在相关端使用三角形表示值溢出.在这种情况下,最上面的刻度线标签需要进行一些调整,以正确地与颜色栏部分对齐.

When the colorbar has extend/overflow, a triangle is used on the relevant end to indicate value overflow. In such cases the top line tick labels need some adjustment to properly align with colorbar sections.

默认情况下,三角形的大小是颜色条轴的5%,用于获取正确的偏移量和缩放比例以对齐标签.

By default the triangle size is 5% of the colorbar axis, this is used to get the proper offset and scaling to align the labels.

请参见下面的示例,该示例在两端都有扩展.使用我以前的方法,结果看起来像这样:

See an example below which has extends on both ends. Using my previous method, the result looks like this:

最上面一行的2个结束编号与三角形的尖端对齐.如果只有一端延伸并且轮廓级别数很大(> = 10左右),则未对准会变得更糟.

The 2 end numbers at top line are aligned with the tip of the triangles. If only one end has extend and the number of contour levels are big (>=10 or so), the misalignment will get worse.

校正后的图:

这是生成正确图的代码:

And this is the code to generate the correct plot:

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)

这篇关于matplotlib colorbar交替使用顶部和底部标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆