简体   繁体   English

显示Matplotlib流图的子图颜色条

[英]Showing subplot colorbar for a matplotlib streamplot

What I want to do is simple: for a plot created using plt.subplots() I would like to display a color bar. 我想做的很简单:对于使用plt.subplots()创建的图,我想显示一个颜色条。

So, this is what I do: 所以,这就是我要做的:

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()

Accordingly to what is shown in the docs . 根据文档中显示的内容。 However, I keep receiving: 但是,我一直收到:

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

This example has only one plot, but I'll have more than one, that's why I'm not directly using plt.colorbar(). 这个示例只有一个图,但是我将有多个图,这就是为什么我不直接使用plt.colorbar().

ax.streamplot returns a StreamplotSet object. ax.streamplot返回StreamplotSet对象。 This is not a mappabple instance that can be used in the making of a colorbar . 这不是一个mappabple例如可在的制作中使用colorbar However, as per the docs for streamplot , it contains the LineCollection and a collection of FancyArrowPatch objects. 但是,根据streamplot文档 ,它包含LineCollectionFancyArrowPatch对象的集合。 We can use the LineCollection to make the colorbar. 我们可以使用LineCollection制作LineCollection

It can be accessed from you h , using h.lines . 您可以使用h.linesh访问它。 Thus, to make your colorbar, you need: 因此,要制作颜色条,您需要:

cbar = f.colorbar(h.lines, ax=ax)

You can see an example of this in the matplotlib gallery, here . 您可以在matplotlib画廊的此处查看示例。

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

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