强制将科学样式用于底图颜色栏标签 [英] Force use of scientific style for basemap colorbar labels

查看:72
本文介绍了强制将科学样式用于底图颜色栏标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串格式可用于为matplotlib.basemap颜色条标签指定科学记号:

String formatting can by used to specify scientific notation for matplotlib.basemap colorbar labels:

cb = m.colorbar(cs, ax=ax1, format='%.4e')

但是,每个标签都带有科学标记.

But then each label is scientifically notated with the base.

如果数字足够大,则颜色变色框会自动将其简化为科学计数法,将基数(即x10^n)放在颜色条的顶部,仅保留系数数字作为标签.

If numbers are large enough, the colobar automatically reduces them to scientific notation, placing the base (i.e. x10^n) at the top of the color bar, leaving only the coefficient numbers as labels.

您可以使用以下标准轴进行操作:

You can do this with a standard axis with the following:

ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))

对于matplotlib.basemap颜色条,或者标准的matplotlib颜色条,是否存在等效的方法?

Is there an equivalent method for matplotlib.basemap colorbars, or perhaps a standard matplotlib colorbar?

推荐答案

没有单行方法,但是您可以通过更新颜色条的formatter然后调用colorbar.update_ticks()来实现.

There's no one-line method, but you can do this by updating the colorbar's formatter and then calling colorbar.update_ticks().

import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)

cb.formatter.set_powerlimits((0, 0))
cb.update_ticks()

plt.show()

之所以采用奇怪的处理方式,是因为颜色栏实际上具有静态分配的刻度和刻度标签.彩条的轴(colorbar.ax)实际上始终在0到1之间.(因此,更改colorbar.ax.yaxis.formatter并没有任何用处.)刻度位置和标签由colorbar.locatorcolorbar.formatter计算得出,并在颜色栏已创建.因此,如果需要精确控制色条的刻度/刻度线,则需要在自定义刻度线的显示方式后显式调用colorbar.update_ticks().彩条的便捷功能可在后台为您完成此操作,但据我所知,您无法通过其他方法来完成您的操作.

The reason for the slightly odd way of doing things is that a colorbar actually has statically assigned ticks and ticklabels. The colorbar's axes (colorbar.ax) actually always ranges between 0 and 1. (Therefore, altering colorbar.ax.yaxis.formatter doesn't do anything useful.) The tick positions and labels are calculated from colorbar.locator and colorbar.formatter and are assigned when the colorbar is created. Therefore, if you need precise control over a colorbar's ticks/ticklables, you need to explicitly call colorbar.update_ticks() after customizing how the ticks are displayed. The colorbar's convenience functions do this for you behind the scenes, but as far as I know, what you want can't be done through another method.

这篇关于强制将科学样式用于底图颜色栏标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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