FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ENH: Add Axes.colorbar() method by timhoffm · Pull Request #31029 · matplotlib/matplotlib · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (27) .pyi  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 16 additions & 5 deletions doc/api/axes_api.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ Axis limits and direction
Axes.set_ybound
Axes.get_ybound

Axis labels, title, and legend
------------------------------
Axis labels and title
---------------------

.. autosummary::
:toctree: _as_gen
Expand All @@ -330,9 +330,6 @@ Axis labels, title, and legend

Axes.set_title
Axes.get_title
Axes.legend
Axes.get_legend
Axes.get_legend_handles_labels

Axis scales
-----------
Expand Down Expand Up @@ -438,6 +435,20 @@ Ticks and tick labels
Axes.locator_params


Legend and colorbar
===================

.. autosummary::
:toctree: _as_gen
:template: autosummary.rst
:nosignatures:

Axes.legend
Axes.get_legend
Axes.get_legend_handles_labels
Axes.colorbar


Units
=====

Expand Down
20 changes: 7 additions & 13 deletions galleries/examples/color/colorbar_basics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,20 @@

fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)

# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')

# add the colorbar using the figure's method,
# telling which mappable we're talking about and
# which Axes object it should be near
fig.colorbar(pos, ax=ax1)
# plot just the positive data and add a colorbar
ax1.imshow(Zpos, cmap='Blues', interpolation='none')
ax1.colorbar()

# repeat everything above for the negative data
# you can specify location, anchor and shrink the colorbar
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
fig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7)
ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
ax2.colorbar(location='right', anchor=(0, 0.3), shrink=0.7)

# Plot both positive and negative values between +/- 1.2
pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2,
interpolation='none')
ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2, interpolation='none')
# Add minorticks on the colorbar to make it easy to read the
# values off the colorbar.
cbar = fig.colorbar(pos_neg_clipped, ax=ax3, extend='both')
cbar = ax3.colorbar(extend='both')
cbar.minorticks_on()
plt.show()

Expand Down
22 changes: 11 additions & 11 deletions galleries/examples/color/custom_cmap.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
# Create the colormap
cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
# Fewer bins will result in "coarser" colomap interpolation
im = ax.imshow(Z, origin='lower', cmap=cmap)
ax.imshow(Z, origin='lower', cmap=cmap)
ax.colorbar()
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)


# %%
Expand Down Expand Up @@ -226,20 +226,20 @@
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)

im1 = axs[0, 0].imshow(Z, cmap=blue_red1)
fig.colorbar(im1, ax=axs[0, 0])
axs[0, 0].imshow(Z, cmap=blue_red1)
axs[0, 0].colorbar()

im2 = axs[1, 0].imshow(Z, cmap='BlueRed2')
fig.colorbar(im2, ax=axs[1, 0])
axs[1, 0].imshow(Z, cmap='BlueRed2')
axs[1, 0].colorbar()

# Now we will set the third cmap as the default. One would
# not normally do this in the middle of a script like this;
# it is done here just to illustrate the method.

plt.rcParams['image.cmap'] = 'BlueRed3'

im3 = axs[0, 1].imshow(Z)
fig.colorbar(im3, ax=axs[0, 1])
axs[0, 1].imshow(Z)
axs[0, 1].colorbar()
axs[0, 1].set_title("Alpha = 1")

# Or as yet another variation, we can replace the rcParams
Expand All @@ -252,12 +252,12 @@
# Draw a line with low zorder so it will be behind the image.
axs[1, 1].plot([0, 10 * np.pi], [0, 20 * np.pi], color='c', lw=20, zorder=-1)

im4 = axs[1, 1].imshow(Z)
fig.colorbar(im4, ax=axs[1, 1])
im = axs[1, 1].imshow(Z)
axs[1, 1].colorbar()

# Here it is: changing the colormap for the current image and its
# colorbar after they have been plotted.
im4.set_cmap('BlueRedAlpha')
im.set_cmap('BlueRedAlpha')
axs[1, 1].set_title("Varying alpha")

fig.suptitle('Custom Blue-Red colormaps', fontsize=16)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
data2d = np.sin(t)[:, np.newaxis] * np.cos(t)[np.newaxis, :]

fig, ax = plt.subplots()
im = ax.imshow(data2d)
ax.imshow(data2d)
ax.colorbar(label='Interactive colorbar')
ax.set_title('Pan on the colorbar to shift the color mapping\n'
'Zoom on the colorbar to scale the color mapping')

fig.colorbar(im, ax=ax, label='Interactive colorbar')

plt.show()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

fig, ax = plt.subplots(2, 1)

pcm = ax[0].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest')
fig.colorbar(pcm, ax=ax[0], extend='max', label='linear scaling')
ax[0].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest')
ax[0].colorbar(extend='max', label='linear scaling')

pcm = ax[1].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest',
norm=colors.LogNorm(vmin=Z.min(), vmax=Z.max()))
fig.colorbar(pcm, ax=ax[1], extend='max', label='LogNorm')
ax[1].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest',
norm=colors.LogNorm(vmin=Z.min(), vmax=Z.max()))
ax[1].colorbar(extend='max', label='LogNorm')

# %%
# PowerNorm
Expand All @@ -53,12 +53,12 @@

fig, ax = plt.subplots(2, 1)

pcm = ax[0].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest')
fig.colorbar(pcm, ax=ax[0], extend='max', label='linear scaling')
ax[0].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest')
ax[0].colorbar(extend='max', label='linear scaling')

pcm = ax[1].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest',
norm=colors.PowerNorm(gamma=0.5))
fig.colorbar(pcm, ax=ax[1], extend='max', label='PowerNorm')
ax[1].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest',
norm=colors.PowerNorm(gamma=0.5))
ax[1].colorbar(extend='max', label='PowerNorm')

# %%
# SymLogNorm
Expand All @@ -79,14 +79,13 @@

fig, ax = plt.subplots(2, 1)

pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
vmin=-np.max(Z))
fig.colorbar(pcm, ax=ax[0], extend='both', label='linear scaling')
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
ax[0].colorbar(extend='both', label='linear scaling')

pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=colors.SymLogNorm(linthresh=0.015,
vmin=-10.0, vmax=10.0, base=10))
fig.colorbar(pcm, ax=ax[1], extend='both', label='SymLogNorm')
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=colors.SymLogNorm(linthresh=0.015,
vmin=-10.0, vmax=10.0, base=10))
ax[1].colorbar(extend='both', label='SymLogNorm')

# %%
# Custom Norm
Expand All @@ -112,13 +111,12 @@ def __call__(self, value, clip=None):
# %%
fig, ax = plt.subplots(2, 1)

pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
vmin=-np.max(Z))
fig.colorbar(pcm, ax=ax[0], extend='both', label='linear scaling')
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
ax[0].colorbar(extend='both', label='linear scaling')

pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=MidpointNormalize(midpoint=0))
fig.colorbar(pcm, ax=ax[1], extend='both', label='Custom norm')
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=MidpointNormalize(midpoint=0))
ax[1].colorbar(extend='both', label='Custom norm')

# %%
# BoundaryNorm
Expand All @@ -129,25 +127,22 @@ def __call__(self, value, clip=None):

fig, ax = plt.subplots(3, 1, layout='constrained')

pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
vmin=-np.max(Z))
fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical',
label='linear scaling')
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
ax[0].colorbar(extend='both', orientation='vertical', label='linear scaling')

# Evenly-spaced bounds gives a contour-like effect.
bounds = np.linspace(-2, 2, 11)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=norm)
fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical',
label='BoundaryNorm\nlinspace(-2, 2, 11)')
ax[1].colorbar(extend='both', orientation='vertical',
label='BoundaryNorm\nlinspace(-2, 2, 11)')

# Unevenly-spaced bounds changes the colormapping.
bounds = np.array([-1, -0.5, 0, 2.5, 5])
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
pcm = ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
norm=norm)
fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical',
label='BoundaryNorm\n[-1, -0.5, 0, 2.5, 5]')
ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', norm=norm)
ax[2].colorbar(extend='both', orientation='vertical',
label='BoundaryNorm\n[-1, -0.5, 0, 2.5, 5]')

plt.show()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ def rbf(x, y):

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
vmin=-gain, vmax=gain, base=10),
**shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].pcolormesh(X, Y, Z, norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
vmin=-gain, vmax=gain, base=10),
**shadeopts)
ax[0].colorbar(extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,
**shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain, **shadeopts)
ax[1].colorbar(extend='both')
ax[1].text(-2.5, 1.5, 'linear')


Expand All @@ -67,18 +65,17 @@ def rbf(x, y):

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
vmin=-gain, vmax=gain, base=10),
**shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].pcolormesh(X, Y, Z,
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
vmin=-gain, vmax=gain, base=10),
**shadeopts)
ax[0].colorbar(extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z,
norm=colors.AsinhNorm(linear_width=lnrwidth,
vmin=-gain, vmax=gain),
**shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].pcolormesh(X, Y, Z,
norm=colors.AsinhNorm(linear_width=lnrwidth, vmin=-gain, vmax=gain),
**shadeopts)
ax[1].colorbar(extend='both')
ax[1].text(-2.5, 1.5, 'asinh')


Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@

cset3 = axs[0].contour(X, Y, Z, (0,), colors='g', linewidths=2)
axs[0].set_title('Filled contours')
fig.colorbar(cset1, ax=axs[0])

axs[0].colorbar(cset1)

axs[1].imshow(Z, extent=extent, cmap=cmap, norm=norm)
axs[1].contour(Z, levels, colors='k', origin='upper', extent=extent)
Expand All @@ -82,13 +81,12 @@
# This is intentional. The Z values are defined at the center of each
# image pixel (each color block on the following subplot), so the
# domain that is contoured does not extend beyond these pixel centers.
im = axs[3].imshow(Z, interpolation='nearest', extent=extent,
cmap=cmap, norm=norm)
im = axs[3].imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm)
axs[3].contour(Z, levels, colors='k', origin='image', extent=extent)
ylim = axs[3].get_ylim()
axs[3].set_ylim(ylim[::-1])
axs[3].set_title("Origin from rc, reversed y-axis")
fig.colorbar(im, ax=axs[3])
axs[3].colorbar(im)

fig.tight_layout()
plt.show()
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
fig, axs = plt.subplots(2, 2, layout="constrained")

for ax, extend in zip(axs.flat, extends):
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend)
fig.colorbar(cs, ax=ax, shrink=0.9)
ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend)
ax.colorbar(shrink=0.9)
ax.set_title("extend = %s" % extend)
ax.locator_params(nbins=4)

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap="PuBu_r")
ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap="PuBu_r")

# Alternatively, you can manually set the levels
# and the norm:
# lev_exp = np.arange(np.floor(np.log10(z.min())-1),
# np.ceil(np.log10(z.max())+1))
# levs = np.power(10, lev_exp)
# cs = ax.contourf(X, Y, z, levs, norm=colors.LogNorm())
# ax.contourf(X, Y, z, levs, norm=colors.LogNorm())

cbar = fig.colorbar(cs)
ax.colorbar()

plt.show()

Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL