【问题标题】:Showing subplot colorbar for a matplotlib streamplot显示 matplotlib 流图的子图颜色条
【发布时间】:2017-02-22 12:33:14
【问题描述】:

我想做的很简单:对于使用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()

根据docs 中显示的内容。 但是,我一直收到:

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

这个例子只有一个情节,但我会有不止一个,这就是为什么我不直接使用plt.colorbar().

【问题讨论】:

标签: python matplotlib colorbar


【解决方案1】:

ax.streamplot 返回一个StreamplotSet 对象。这不是可用于制作colorbar 的可映射实例。但是,根据docs for streamplot,它包含LineCollectionFancyArrowPatch 对象的集合。我们可以使用LineCollection 来制作颜色条。

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

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

您可以在matplotlibhere 中查看此示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 2014-09-10
    • 2020-10-07
    • 1970-01-01
    • 2011-08-29
    • 2021-09-11
    • 2018-06-21
    相关资源
    最近更新 更多