简体   繁体   English

在matplotlib 2.0中,如何将颜色条行为还原为matplotlib 1.5?

[英]In matplotlib 2.0, how do I revert colorbar behaviour to that of matplotlib 1.5?

I just upgraded to matplotlib 2.0 and in general, I'm very happy with the new defaults. 我刚刚升级到matplotlib 2.0,总体而言,我对新的默认设置感到非常满意。 One thing which I'd like to revert to the 1.5 behaviour is that of plt.colorbar , specifically the yticks. 我想恢复为1.5行为的一件事是plt.colorbar ,特别是yticks。 In the old matplotlib, only major ticks were plotted on my colorbars; 在旧的matplotlib中,我的色条上只绘制了主要的刻度线。 in the new matplotlib, minor and major ticks are drawn, which I do not want. 在新的matplotlib中,绘制了次要和主要刻度线,我不希望这样做。

Below is shown a comparison of the 1.5 behaviour (left) and the 2.0 behaviour (right) using the same colormap and logarithmic ticks. 下面显示了使用相同的颜色图和对数刻度的1.5行为(左)和2.0行为(右)的比较。

彩条yticks的比较

What defaults do I need to set in matplotlibrc in order to revert to the 1.5 behaviour shown on the left? 我需要在matplotlibrc中设置哪些默认值才能恢复到左侧显示的1.5行为? If there is no way to do this using matplotlibrc , what other avenues are available for altering this globally beyond downgrading to matplotlib 1.5? 如果无法使用matplotlibrc进行此操作,除了降级到matplotlib 1.5之外,还有哪些其他途径可用于全局更改?

I have tried simply setting cbar.ax.minorticks_off() after every instance of cbar = plt.colorbar(mesh) , but that doesn't solve the issue. 我试过在cbar = plt.colorbar(mesh)每个实例之后简单地设置cbar.ax.minorticks_off() cbar = plt.colorbar(mesh) ,但这不能解决问题。

It should be sufficient to just set the colorbar locator to a LogLocator from the matplotlib.ticker module, and then call update_ticks() on the colorbar instance. 只需从matplotlib.ticker模块LogLocator locator设置为LogLocator ,然后在颜色条实例上调用update_ticks()就足够了。

For example, consider this minimal example which produces the colorbar you are seeing with minor ticks: 例如,请考虑以下最小示例,该示例将产生您看到的带有较小刻度的颜色条:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.colors as colors
import numpy as np

fig, ax = plt.subplots(1)

# Some random data in your range 1e-26 to 1e-19
data = 10**(-26. + 7. * np.random.rand(10, 10))

p = ax.imshow(data, norm=colors.LogNorm(vmin=1e-26, vmax=1e-19))

cb = fig.colorbar(p, ax=ax)

plt.show()

在此处输入图片说明

If we now add the following two lines before calling plt.show() , we remove the minor ticks: 如果现在在调用plt.show()之前添加以下两行, plt.show()删除较小的刻度:

cb.locator = ticker.LogLocator()
cb.update_ticks()

在此处输入图片说明

Alternatively, to achieve the same thing, you can use the ticks kwarg when creating the colorbar, and set that to the LogLocator() 或者,为了实现同样的事情,你可以使用ticks kwarg创建彩条,并设置到时LogLocator()

cb = fig.colorbar(p, ax=ax, ticks=ticker.LogLocator())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM