8000 DOC: add a basic colorbar examples · matplotlib/matplotlib@c57cf8a · GitHub
[go: up one dir, main page]

Skip to content

Commit c57cf8a

Browse files
committed
DOC: add a basic colorbar examples
The main thing here is that we have multiple axes and use the `fig.colorbar` method, specifying which mappables should be colorbar'd next to which Axes.
1 parent ff2f779 commit c57cf8a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/api/colorbar_basics.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
# setup some generic data
5+
N = 37
6+
x, y = np.mgrid[:N, :N]
7+
Z = (np.cos(x*0.2) + np.sin(y*0.3))
8+
9+
# mask out the negative and positve values, respectively
10+
Zpos = np.ma.masked_less(Z, 0)
11+
Zneg = np.ma.masked_greater(Z, 0)
12+
13+
fig, (ax1, ax2) = plt.subplots(figsize=(8, 3), ncols=2)
14+
15+
# plot just the positive data and save the
16+
# color "mappable" object returned by ax1.imshow
17+
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')
18+
19+
# add the colorbar using the figure's method,
20+
# telling which mappable we're talking about and
21+
# which axes object it should be near
22+
fig.colorbar(pos, ax=ax1)
23+
24+
# repeat everything above for the the negative data
25+
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
26+
fig.colorbar(neg, ax=ax2)
27+
28+
plt.show()

0 commit comments

Comments
 (0)
0