Matplotlib colorbar 刻度定位 - matplotlib 版本之间的变化?
Matplotlib colorbar tick positioning - change between matplotlib versions?
Matplotlib 似乎已经改变了次要颜色条刻度的设置方式。下面是一个最小的例子。在 matplotlib 2.2.3 中,该代码在 0 到 10 之间创建 11 个小刻度,在 matplotlib 3.0.3 中在 0 到 1 之间创建。对此有任何参考吗?我在变更日志中找不到任何内容。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
np.random.seed(1000)
im = plt.imshow(np.random.rand(10,10) * 10)
cbar = plt.colorbar()
plt.clim(0,10)
cax = cbar.ax
cax.yaxis.set_ticks(np.linspace(0,1,11),minor=True)
你是对的,版本 3 中的颜色条发生了重大变化,不幸的是没有得到很好的沟通。它以某种方式隐藏在 this What's new entry
中
The ticks for colorbar now adjust for the size of the colorbar
Colorbar ticks now adjust for the size of the colorbar if the colorbar is made from a mappable that is not a contour or doesn't have a BoundaryNorm, or boundaries are not specified. If boundaries, etc are specified, the colorbar maintains the original behavior.
这也意味着颜色条现在像任何其他轴一样缩放。意思是,如果你想在 0 到 10 之间有 11 个刻度,你需要使用
ticks = np.linspace(0,10,11)
cbar.ax.xaxis.set_ticks(ticks,minor=True)
Matplotlib 似乎已经改变了次要颜色条刻度的设置方式。下面是一个最小的例子。在 matplotlib 2.2.3 中,该代码在 0 到 10 之间创建 11 个小刻度,在 matplotlib 3.0.3 中在 0 到 1 之间创建。对此有任何参考吗?我在变更日志中找不到任何内容。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
np.random.seed(1000)
im = plt.imshow(np.random.rand(10,10) * 10)
cbar = plt.colorbar()
plt.clim(0,10)
cax = cbar.ax
cax.yaxis.set_ticks(np.linspace(0,1,11),minor=True)
你是对的,版本 3 中的颜色条发生了重大变化,不幸的是没有得到很好的沟通。它以某种方式隐藏在 this What's new entry
中The ticks for colorbar now adjust for the size of the colorbar
Colorbar ticks now adjust for the size of the colorbar if the colorbar is made from a mappable that is not a contour or doesn't have a BoundaryNorm, or boundaries are not specified. If boundaries, etc are specified, the colorbar maintains the original behavior.
这也意味着颜色条现在像任何其他轴一样缩放。意思是,如果你想在 0 到 10 之间有 11 个刻度,你需要使用
ticks = np.linspace(0,10,11)
cbar.ax.xaxis.set_ticks(ticks,minor=True)