创建后重新缩放 matplotlib.colorbar 和 matplotlib.contour

Daa*_*aan 5 python matplotlib

我有一个使用 matplotlib 生成的轮廓图。

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)

fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)
Run Code Online (Sandbox Code Playgroud)

这会生成这个数字:

在此处输入图片说明

现在想象一下,由于某种原因,这个数字是通过一些功能的调用交给用户,用户只得到了axfigcbar对象。

更新:我想这样做的原因是因为我必须以自动化的方式制作大量的等高线图。我已经在后端自动创建了轮廓图(在上面模糊地描述为“函数调用”)。但是对于某些情节,需要稍作修改才能生成精美的情节。因此,而不是操纵备份或者不必增加大量数额的每一种情况的选择,这将是很好,如果这对夫妻的我需要调整的数字+400出来的数字通过可调axfigcbar对象。

现在只操作这些对象,我想放大,并重新调整颜色条和级别。

放大:

ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

更改颜色条和刻度上的限制:

new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

更改颜色条本身的比例:

cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

现在这里有一些问题:

  1. 我似乎无法找到如何在顶部没有那个烦人的空矩形的情况下重新缩放颜色条的轴。
  2. 创建实例后,我找不到如何调整轮廓图和颜色栏中的级别数。

非常感谢!