Python 显示matplotlib流图的子图颜色栏

Python 显示matplotlib流图的子图颜色栏,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,我想做的很简单:对于使用plt.subplots()创建的绘图,我想显示一个颜色栏 所以,我就是这么做的: def plotVF(self, u, v): m = np.sqrt(np.power(u, 2) + np.power(v, 2)) xrange = np.linspace(0, u.shape[1], u.shape[1]); yrange = np.linspace(0, u.shape[0], u.shape[0]); x, y = np.m

我想做的很简单:对于使用
plt.subplots()
创建的绘图,我想显示一个颜色栏

所以,我就是这么做的:

def plotVF(self, u, v):
    m = np.sqrt(np.power(u, 2) + np.power(v, 2))

    xrange = np.linspace(0, u.shape[1], u.shape[1]);
    yrange = np.linspace(0, u.shape[0], u.shape[0]);

    x, y = np.meshgrid(xrange, yrange)
    mag = np.hypot(u, v)
    scale = 1
    lw = scale * mag / mag.max()

    f, ax = plt.subplots()
    h = ax.streamplot(x, y, u, v, color=mag, linewidth=lw, density=3, arrowsize=1, norm=plt.Normalize(0, 70))
    ax.set_xlim(0, u.shape[1])
    ax.set_ylim(0, u.shape[0])
    ax.set_xticks([])
    ax.set_yticks([])
    cbar = f.colorbar(h, ax=ax)
    cbar.ax.tick_params(labelsize=5) 

    plt.show()
与图中所示内容相应。 然而,我不断收到:

AttributeError: 'StreamplotSet' object has no attribute 'autoscale_None'

这个例子只有一个绘图,但我会有多个,这就是为什么我不直接使用
plt.colorbar()。
ax。streamplot
返回一个
StreamplotSet
对象。这不是一个可用于制作
颜色条的mappapple实例。但是,根据,它包含
LineCollection
FancyArrowPatch
对象的集合。我们可以使用
LineCollection
制作色条

可以使用
h.lines
从您的
h
访问它。因此,要制作彩色条,您需要:

cbar = f.colorbar(h.lines, ax=ax)
您可以在
matplotlib
图库中看到此示例。

的可能副本