python - Matplotlib:更改附加轴的定位器

标签 python matplotlib

我有许多如下所示的数据图像,其中我在右侧附加了一组新的轴,以强制颜色条与图像的大小相同。 enter image description here

颜色条上的刻度线太靠近,所以我想减少它们的数量。我尝试这样做:

# Set up colorbar
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", size="5%", pad=0.05)
cax.yaxis.set_major_locator(plticker.LinearLocator(numticks=5))
cbar = plt.colorbar(im, cax=cax)
cbar.set_label(cbtitle, **cbkw)

我知道,一般来说,以这种方式重新格式化轴需要调用 cbar.update_axis() 来强制更新,但这在这里似乎不起作用。 cax 是一个 Axes 对象,调用 cbar.update_axis() 也不会执行任何操作。事实上,这两种策略以及忽略更新都会产生上图。有人知道如何减少蜱虫吗?

最佳答案

您的示例代码包含许多在其外部定义的变量,因此很难看到您正在做的所有事情。但关于设置 cbar 上的刻度数,这对我有用:

fig, ax = plt.subplots()

im = ax.imshow(np.random.randn(10,10), interpolation='none')

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax, ticks=mpl.ticker.LinearLocator(numticks=3))

enter image description here

@hildensia,该图像显示了将彩虹切成正方形并将它们随机放置在网格中的结果,所以我真的需要“jet”cmap,抱歉。 ;) 当然,你是对的。

关于python - Matplotlib:更改附加轴的定位器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062175/

相关文章:

python - NumPy 数组索引

python - 放置在时间轴上的箱线图

python - 如何在 Spyder 的绘图 Pane 中启用放大/缩小和缩放到百分比按钮?

python - 列表列表的总和和长度

python - 让 Django 为 MySQL 后端创建外键

python - 在 Altair 中向分层图表添加图例

python - "TypeError: ' module ' object is not callable"在函数中初始化Gekko模块时

python - 值错误: x and y must be the same size

python - Matplotlib 等高线图标签

python - 如何在 OpenCV 中绘制图像的 3D 直方图