Reputation: 9844
What I want to do is simple: for a plot created using plt.subplots()
I would like to display a color bar.
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().
Upvotes: 1
Views: 2035
Reputation: 69193
ax.streamplot
returns a StreamplotSet
object. This is not a mappabple instance that can be used in the making of a colorbar
. However, as per the docs for streamplot
, it contains the LineCollection
and a collection of FancyArrowPatch
objects. We can use the LineCollection
to make the colorbar.
It can be accessed from you h
, using h.lines
. 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.
Upvotes: 1