Reputation: 4774
I want to plot a figure that is made up of 3 subplots: 2 of them are normal graphs and 1 has a color bar.
So basically I want to put the images next to each other (horizontally).
Here is the code I wrote to generate the first figure
:
plt.scatter(x, y, s = 40, c=z)
plt.colorbar()
and here's the code for the second figure
:
fig, axes = plt.subplots(1, 2, sharey = True)
axes[0].plot(z, y, 'ok')
axes[1].plot(x, y, 'ok')
In order to place the figures horizontally next to each other I tried to do the following: (but I got an error)
fig, axes = plt.subplots(1, 3, sharey = True)
axes[0].plot(z, y, 'ok')
axes[1].plot(x, y, 'ok')
axes[2].scatter(x, y, s = 40, c = z)
fig.colorbar()
Can someone tell me why I am getting an error and how to solve this?
Upvotes: 0
Views: 159
Reputation: 6510
According to http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar you have to pass neccessary argument mappable
to colorbar
method of Figure
.
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [1,2,1,3]
z = [10, 20, 30, 40]
fig, axes = plt.subplots(1, 3, sharey = True)
axes[0].plot(z, y, 'ok')
axes[1].plot(x, y, 'ok')
a2 = axes[2].scatter(x, y, s = 40, c=z)
fig.colorbar(a2)
plt.show()
Code above works fine.
As for the first example, plt
is just a wise shortcut to latest Figure
instance. As far as you created only 1 plot and 1 artist, everything works good.
Upvotes: 1