From fa23f8814ab9d123d5b98ff9b5723c2f99fdd295 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 7 May 2019 10:47:51 +0200 Subject: [PATCH] Avoid using `axis([xlo, xhi, ylo, yhi])` in examples. In my view, `axis()` does too much: it can manipulate axis limits, axes aspect ratio, lines and label visibility, etc. This PR replaces in the examples `axis([xlo, xhi, ylo, yhi])` by `set(xlim=(xlo, xhi), ylim=(ylo, yhi))` (or `plt.xlim(xlo, xhi); plt.ylim(xlo, yhi)` for pyplot examples), which is easier to intuit IMO. There are also cases where I just remove the call to axis() when it doesn't add much to the example. Special cases: - anscombe can be greatly shortened by using shared axes. Note that the "old" xlims (2, 20) were immediately overwritten by xticks=(0, 10, 20) which expanded the xlims to (0, 20)... - fonts_demo/fonts_demo_kw are better showcased using figtext() anyways. - In pcolor_demo, the call to axis() is unneeded (pcolor/pcolormesh correctly set the artist sticky edges to have the right bounds). - In pyplot_formatstr (which could probably be deleted...), the call to axis() does not showcase anything. - In slider_demo, margins(x=0) is semantically more correct. --- examples/axes_grid1/inset_locator_demo.py | 4 +- .../ginput_manual_clabel_sgskip.py | 12 ++--- examples/event_handling/pick_event_demo.py | 2 +- .../irregulardatagrid.py | 4 +- .../images_contours_and_fields/pcolor_demo.py | 4 -- .../plot_streamplot.py | 2 +- examples/misc/contour_manual.py | 24 ++++----- examples/pyplots/pyplot_formatstr.py | 14 ++--- examples/pyplots/pyplot_text.py | 3 +- examples/pyplots/text_commands.py | 2 +- examples/specialty_plots/anscombe.py | 51 +++++++------------ examples/statistics/hexbin_demo.py | 4 +- .../subplots_axes_and_figures/axes_demo.py | 3 +- .../subplots_axes_and_figures/axhspan_demo.py | 2 - .../axis_equal_demo.py | 2 +- .../demo_text_rotation_mode.py | 1 - .../text_labels_and_annotations/fonts_demo.py | 43 +++++----------- .../fonts_demo_kw.py | 50 ++++++++---------- examples/units/units_scatter.py | 6 +-- examples/widgets/slider_demo.py | 2 +- lib/matplotlib/axes/_base.py | 4 +- 21 files changed, 96 insertions(+), 143 deletions(-) diff --git a/examples/axes_grid1/inset_locator_demo.py b/examples/axes_grid1/inset_locator_demo.py index de049219a9cd..fa2b100d0246 100644 --- a/examples/axes_grid1/inset_locator_demo.py +++ b/examples/axes_grid1/inset_locator_demo.py @@ -74,7 +74,7 @@ # We set the axis limits to something other than the default, in order to not # distract from the fact that axes coordinates are used here. -ax.axis([0, 10, 0, 10]) +ax.set(xlim=(0, 10), ylim=(0, 10)) # Note how the two following insets are created at the same positions, one by @@ -126,7 +126,7 @@ ax2 = fig.add_subplot(133) ax2.set_xscale("log") -ax2.axis([1e-6, 1e6, -2, 6]) +ax2.set(xlim=(1e-6, 1e6), ylim=(-2, 6)) # Create inset in data coordinates using ax.transData as transform axins3 = inset_axes(ax2, width="100%", height="100%", diff --git a/examples/event_handling/ginput_manual_clabel_sgskip.py b/examples/event_handling/ginput_manual_clabel_sgskip.py index e29a946e6dea..0dc41b05230f 100644 --- a/examples/event_handling/ginput_manual_clabel_sgskip.py +++ b/examples/event_handling/ginput_manual_clabel_sgskip.py @@ -28,7 +28,6 @@ def tellme(s): plt.clf() -plt.axis([-1., 1., -1., 1.]) plt.setp(plt.gca(), autoscale_on=False) tellme('You will define a triangle, click to begin') @@ -84,13 +83,14 @@ def f(x, y, pts): while True: tellme('Select two corners of zoom, middle mouse button to finish') - pts = np.asarray(plt.ginput(2, timeout=-1)) - + pts = plt.ginput(2, timeout=-1) if len(pts) < 2: break - - pts = np.sort(pts, axis=0) - plt.axis(pts.T.ravel()) + (x0, y0), (x1, y1) = pts + xmin, xmax = sorted([x0, x1]) + ymin, ymax = sorted([y0, y1]) + plt.xlim(xmin, xmax) + plt.ylim(ymin, ymax) tellme('All Done!') plt.show() diff --git a/examples/event_handling/pick_event_demo.py b/examples/event_handling/pick_event_demo.py index ad004abf3fa2..5e18f854c6c1 100644 --- a/examples/event_handling/pick_event_demo.py +++ b/examples/event_handling/pick_event_demo.py @@ -168,7 +168,7 @@ def pick_image(): ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True) ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True) ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True) - ax.axis([0, 5, 0, 5]) + ax.set(xlim=(0, 5), ylim=(0, 5)) def onpick4(event): artist = event.artist diff --git a/examples/images_contours_and_fields/irregulardatagrid.py b/examples/images_contours_and_fields/irregulardatagrid.py index 3166f755cf02..643e3a911c7e 100644 --- a/examples/images_contours_and_fields/irregulardatagrid.py +++ b/examples/images_contours_and_fields/irregulardatagrid.py @@ -62,7 +62,7 @@ fig.colorbar(cntr1, ax=ax1) ax1.plot(x, y, 'ko', ms=3) -ax1.axis((-2, 2, -2, 2)) +ax1.set(xlim=(-2, 2), ylim=(-2, 2)) ax1.set_title('grid and contour (%d points, %d grid points)' % (npts, ngridx * ngridy)) @@ -78,7 +78,7 @@ fig.colorbar(cntr2, ax=ax2) ax2.plot(x, y, 'ko', ms=3) -ax2.axis((-2, 2, -2, 2)) +ax2.set(xlim=(-2, 2), ylim=(-2, 2)) ax2.set_title('tricontour (%d points)' % npts) plt.subplots_adjust(hspace=0.5) diff --git a/examples/images_contours_and_fields/pcolor_demo.py b/examples/images_contours_and_fields/pcolor_demo.py index 2ddc0f3b5a4d..b66c55316571 100644 --- a/examples/images_contours_and_fields/pcolor_demo.py +++ b/examples/images_contours_and_fields/pcolor_demo.py @@ -54,15 +54,11 @@ ax = axs[0, 0] c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max) ax.set_title('pcolor') -# set the limits of the plot to the limits of the data -ax.axis([x.min(), x.max(), y.min(), y.max()]) fig.colorbar(c, ax=ax) ax = axs[0, 1] c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max) ax.set_title('pcolormesh') -# set the limits of the plot to the limits of the data -ax.axis([x.min(), x.max(), y.min(), y.max()]) fig.colorbar(c, ax=ax) ax = axs[1, 0] diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index 325298827bdb..2dc59934ecba 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -53,7 +53,7 @@ # Displaying the starting points with blue symbols. ax3.plot(seed_points[0], seed_points[1], 'bo') -ax3.axis((-w, w, -w, w)) +ax3.set(xlim=(-w, w), ylim=(-w, w)) # Create a mask mask = np.zeros(U.shape, dtype=bool) diff --git a/examples/misc/contour_manual.py b/examples/misc/contour_manual.py index 273ae3cfd230..cc47bce184c5 100644 --- a/examples/misc/contour_manual.py +++ b/examples/misc/contour_manual.py @@ -25,19 +25,19 @@ ############################################################################### -plt.figure() +fig, ax = plt.subplots() # Filled contours using filled=True. -cs = ContourSet(plt.gca(), [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone) -cbar = plt.colorbar(cs) +cs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone) +cbar = fig.colorbar(cs) # Contour lines (non-filled). -lines = ContourSet(plt.gca(), [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, - linewidths=3) +lines = ContourSet( + ax, [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, linewidths=3) cbar.add_lines(lines) -plt.axis([-0.5, 3.5, -0.5, 4.5]) -plt.title('User-specified contours') +ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 4.5), + title='User-specified contours') ############################################################################### # Multiple filled contour lines can be specified in a single list of polygon @@ -45,13 +45,13 @@ # Path class. This is particularly useful for polygons with holes. # Here a code type of 1 is a MOVETO, and 2 is a LINETO. -plt.figure() +fig, ax = plt.subplots() filled01 = [[[0, 0], [3, 0], [3, 3], [0, 3], [1, 1], [1, 2], [2, 2], [2, 1]]] kinds01 = [[1, 2, 2, 2, 1, 2, 2, 2]] -cs = ContourSet(plt.gca(), [0, 1], [filled01], [kinds01], filled=True) -cbar = plt.colorbar(cs) +cs = ContourSet(ax, [0, 1], [filled01], [kinds01], filled=True) +cbar = fig.colorbar(cs) -plt.axis([-0.5, 3.5, -0.5, 3.5]) -plt.title('User specified filled contours with holes') +ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 3.5), + title='User specified filled contours with holes') plt.show() diff --git a/examples/pyplots/pyplot_formatstr.py b/examples/pyplots/pyplot_formatstr.py index 057cba15b92f..3e5b1806b5c5 100644 --- a/examples/pyplots/pyplot_formatstr.py +++ b/examples/pyplots/pyplot_formatstr.py @@ -1,14 +1,14 @@ """ -================ -Pyplot Formatstr -================ +==================== +plot() format string +==================== -Use a format string to colorize a `~matplotlib.axes.Axes.plot` and set its -markers. +Use a format string (here, 'ro') to set the color and markers of a +`~matplotlib.axes.Axes.plot`. """ + import matplotlib.pyplot as plt -plt.plot([1,2,3,4], [1,4,9,16], 'ro') -plt.axis([0, 6, 0, 20]) +plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.show() ############################################################################# diff --git a/examples/pyplots/pyplot_text.py b/examples/pyplots/pyplot_text.py index 4492ce438c01..b5952ba00706 100644 --- a/examples/pyplots/pyplot_text.py +++ b/examples/pyplots/pyplot_text.py @@ -21,7 +21,8 @@ plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') -plt.axis([40, 160, 0, 0.03]) +plt.xlim(40, 160) +plt.ylim(0, 0.03) plt.grid(True) plt.show() diff --git a/examples/pyplots/text_commands.py b/examples/pyplots/text_commands.py index d7df2ecfca3d..1f6647faede0 100644 --- a/examples/pyplots/text_commands.py +++ b/examples/pyplots/text_commands.py @@ -35,7 +35,7 @@ ax.annotate('annotate', xy=(2, 1), xytext=(3, 4), arrowprops=dict(facecolor='black', shrink=0.05)) -ax.axis([0, 10, 0, 10]) +ax.set(xlim=(0, 10), ylim=(0, 10)) plt.show() diff --git a/examples/specialty_plots/anscombe.py b/examples/specialty_plots/anscombe.py index 3f5d98ef914c..2160f7ef98b7 100644 --- a/examples/specialty_plots/anscombe.py +++ b/examples/specialty_plots/anscombe.py @@ -8,52 +8,37 @@ Edward Tufte uses this example from Anscombe to show 4 datasets of x and y that have the same mean, standard deviation, and regression line, but which are qualitatively different. - -matplotlib fun for a rainy day """ import matplotlib.pyplot as plt import numpy as np -x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]) -y1 = np.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]) -y2 = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]) -y3 = np.array([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]) -x4 = np.array([8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]) -y4 = np.array([6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]) +x = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5] +y1 = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68] +y2 = [9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74] +y3 = [7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73] +x4 = [8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8] +y4 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89] def fit(x): return 3 + 0.5 * x -xfit = np.array([np.min(x), np.max(x)]) - -plt.subplot(221) -plt.plot(x, y1, 'ks', xfit, fit(xfit), 'r-', lw=2) -plt.axis([2, 20, 2, 14]) -plt.setp(plt.gca(), xticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) -plt.text(3, 12, 'I', fontsize=20) - -plt.subplot(222) -plt.plot(x, y2, 'ks', xfit, fit(xfit), 'r-', lw=2) -plt.axis([2, 20, 2, 14]) -plt.setp(plt.gca(), xticks=(0, 10, 20), xticklabels=[], - yticks=(4, 8, 12), yticklabels=[], ) -plt.text(3, 12, 'II', fontsize=20) +fig, axs = plt.subplots(2, 2, sharex=True, sharey=True) +axs[0, 0].set(xlim=(0, 20), ylim=(2, 14)) +axs[0, 0].set(xticks=(0, 10, 20), yticks=(4, 8, 12)) -plt.subplot(223) -plt.plot(x, y3, 'ks', xfit, fit(xfit), 'r-', lw=2) -plt.axis([2, 20, 2, 14]) -plt.text(3, 12, 'III', fontsize=20) -plt.setp(plt.gca(), yticks=(4, 8, 12), xticks=(0, 10, 20)) - -plt.subplot(224) +xfit = np.array([np.min(x), np.max(x)]) +axs[0, 0].plot(x, y1, 'ks', xfit, fit(xfit), 'r-', lw=2) +axs[0, 1].plot(x, y2, 'ks', xfit, fit(xfit), 'r-', lw=2) +axs[1, 0].plot(x, y3, 'ks', xfit, fit(xfit), 'r-', lw=2) xfit = np.array([np.min(x4), np.max(x4)]) -plt.plot(x4, y4, 'ks', xfit, fit(xfit), 'r-', lw=2) -plt.axis([2, 20, 2, 14]) -plt.setp(plt.gca(), yticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20)) -plt.text(3, 12, 'IV', fontsize=20) +axs[1, 1].plot(x4, y4, 'ks', xfit, fit(xfit), 'r-', lw=2) + +for ax, label in zip(axs.flat, ['I', 'II', 'III', 'IV']): + ax.label_outer() + ax.text(3, 12, label, fontsize=20) # verify the stats pairs = (x, y1), (x, y2), (x, y3), (x4, y4) diff --git a/examples/statistics/hexbin_demo.py b/examples/statistics/hexbin_demo.py index e04c68458bdd..8115f99548ea 100644 --- a/examples/statistics/hexbin_demo.py +++ b/examples/statistics/hexbin_demo.py @@ -29,14 +29,14 @@ fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93) ax = axs[0] hb = ax.hexbin(x, y, gridsize=50, cmap='inferno') -ax.axis([xmin, xmax, ymin, ymax]) +ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) ax.set_title("Hexagon binning") cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') ax = axs[1] hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno') -ax.axis([xmin, xmax, ymin, ymax]) +ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) ax.set_title("With a log color scale") cb = fig.colorbar(hb, ax=ax) cb.set_label('log10(N)') diff --git a/examples/subplots_axes_and_figures/axes_demo.py b/examples/subplots_axes_and_figures/axes_demo.py index 42e904f03595..5b07f2f2d820 100644 --- a/examples/subplots_axes_and_figures/axes_demo.py +++ b/examples/subplots_axes_and_figures/axes_demo.py @@ -21,7 +21,8 @@ # the main axes is subplot(111) by default plt.plot(t, s) -plt.axis([0, 1, 1.1 * np.min(s), 2 * np.max(s)]) +plt.xlim(0, 1) +plt.ylim(1.1 * np.min(s), 2 * np.max(s)) plt.xlabel('time (s)') plt.ylabel('current (nA)') plt.title('Gaussian colored noise') diff --git a/examples/subplots_axes_and_figures/axhspan_demo.py b/examples/subplots_axes_and_figures/axhspan_demo.py index 29716e800568..ab1164076e7e 100644 --- a/examples/subplots_axes_and_figures/axhspan_demo.py +++ b/examples/subplots_axes_and_figures/axhspan_demo.py @@ -32,6 +32,4 @@ plt.axvspan(1.25, 1.55, facecolor='#2ca02c', alpha=0.5) -plt.axis([-1, 2, -1, 2]) - plt.show() diff --git a/examples/subplots_axes_and_figures/axis_equal_demo.py b/examples/subplots_axes_and_figures/axis_equal_demo.py index 20af5c9f4dc7..002a2bb62b42 100644 --- a/examples/subplots_axes_and_figures/axis_equal_demo.py +++ b/examples/subplots_axes_and_figures/axis_equal_demo.py @@ -23,7 +23,7 @@ axs[1, 0].plot(3 * np.cos(an), 3 * np.sin(an)) axs[1, 0].axis('equal') -axs[1, 0].axis([-3, 3, -3, 3]) +axs[1, 0].set(xlim=(-3, 3), ylim=(-3, 3)) axs[1, 0].set_title('still a circle, even after changing limits', fontsize=10) axs[1, 1].plot(3 * np.cos(an), 3 * np.sin(an)) diff --git a/examples/text_labels_and_annotations/demo_text_rotation_mode.py b/examples/text_labels_and_annotations/demo_text_rotation_mode.py index 0f662000e395..2cb7fd66afd4 100644 --- a/examples/text_labels_and_annotations/demo_text_rotation_mode.py +++ b/examples/text_labels_and_annotations/demo_text_rotation_mode.py @@ -49,7 +49,6 @@ def test_rotation_mode(fig, mode, subplot_location): # prepare axes layout for axis in ax.axis.values(): axis.toggle(ticks=False, ticklabels=False) - ax.axis([0, 1, 0, 1]) ax.axvline(0.5, color="skyblue", zorder=0) ax.axhline(0.5, color="skyblue", zorder=0) ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1) diff --git a/examples/text_labels_and_annotations/fonts_demo.py b/examples/text_labels_and_annotations/fonts_demo.py index 92f5decf38e8..1aa38dcbcf38 100644 --- a/examples/text_labels_and_annotations/fonts_demo.py +++ b/examples/text_labels_and_annotations/fonts_demo.py @@ -11,8 +11,6 @@ from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt -plt.subplot(111, facecolor='w') - font0 = FontProperties() alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'} # Show family options @@ -22,71 +20,61 @@ font1 = font0.copy() font1.set_size('large') -t = plt.text(-0.8, 0.9, 'family', fontproperties=font1, - **alignment) +t = plt.figtext(0.1, 0.9, 'family', fontproperties=font1, **alignment) yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2] for k, family in enumerate(families): font = font0.copy() font.set_family(family) - t = plt.text(-0.8, yp[k], family, fontproperties=font, - **alignment) + t = plt.figtext(0.1, yp[k], family, fontproperties=font, **alignment) # Show style options styles = ['normal', 'italic', 'oblique'] -t = plt.text(-0.4, 0.9, 'style', fontproperties=font1, - **alignment) +t = plt.figtext(0.3, 0.9, 'style', fontproperties=font1, **alignment) for k, style in enumerate(styles): font = font0.copy() font.set_family('sans-serif') font.set_style(style) - t = plt.text(-0.4, yp[k], style, fontproperties=font, - **alignment) + t = plt.figtext(0.3, yp[k], style, fontproperties=font, **alignment) # Show variant options variants = ['normal', 'small-caps'] -t = plt.text(0.0, 0.9, 'variant', fontproperties=font1, - **alignment) +t = plt.figtext(0.5, 0.9, 'variant', fontproperties=font1, **alignment) for k, variant in enumerate(variants): font = font0.copy() font.set_family('serif') font.set_variant(variant) - t = plt.text(0.0, yp[k], variant, fontproperties=font, - **alignment) + t = plt.figtext(0.5, yp[k], variant, fontproperties=font, **alignment) # Show weight options weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black'] -t = plt.text(0.4, 0.9, 'weight', fontproperties=font1, - **alignment) +t = plt.figtext(0.7, 0.9, 'weight', fontproperties=font1, **alignment) for k, weight in enumerate(weights): font = font0.copy() font.set_weight(weight) - t = plt.text(0.4, yp[k], weight, fontproperties=font, - **alignment) + t = plt.figtext(0.7, yp[k], weight, fontproperties=font, **alignment) # Show size options sizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'] -t = plt.text(0.8, 0.9, 'size', fontproperties=font1, - **alignment) +t = plt.figtext(0.9, 0.9, 'size', fontproperties=font1, **alignment) for k, size in enumerate(sizes): font = font0.copy() font.set_size(size) - t = plt.text(0.8, yp[k], size, fontproperties=font, - **alignment) + t = plt.figtext(0.9, yp[k], size, fontproperties=font, **alignment) # Show bold italic @@ -94,23 +82,18 @@ font.set_style('italic') font.set_weight('bold') font.set_size('x-small') -t = plt.text(-0.4, 0.1, 'bold italic', fontproperties=font, - **alignment) +t = plt.figtext(0.3, 0.1, 'bold italic', fontproperties=font, **alignment) font = font0.copy() font.set_style('italic') font.set_weight('bold') font.set_size('medium') -t = plt.text(-0.4, 0.2, 'bold italic', fontproperties=font, - **alignment) +t = plt.figtext(0.3, 0.2, 'bold italic', fontproperties=font, **alignment) font = font0.copy() font.set_style('italic') font.set_weight('bold') font.set_size('x-large') -t = plt.text(-0.4, 0.3, 'bold italic', fontproperties=font, - **alignment) - -plt.axis([-1, 1, 0, 1]) +t = plt.figtext(-0.4, 0.3, 'bold italic', fontproperties=font, **alignment) plt.show() diff --git a/examples/text_labels_and_annotations/fonts_demo_kw.py b/examples/text_labels_and_annotations/fonts_demo_kw.py index bfcec95f93f2..afbdf1504431 100644 --- a/examples/text_labels_and_annotations/fonts_demo_kw.py +++ b/examples/text_labels_and_annotations/fonts_demo_kw.py @@ -10,75 +10,67 @@ import matplotlib.pyplot as plt -plt.subplot(111, facecolor='w') alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'} # Show family options families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'] -t = plt.text(-0.8, 0.9, 'family', size='large', **alignment) +t = plt.figtext(0.1, 0.9, 'family', size='large', **alignment) yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2] for k, family in enumerate(families): - t = plt.text(-0.8, yp[k], family, family=family, **alignment) + t = plt.figtext(0.1, yp[k], family, family=family, **alignment) # Show style options styles = ['normal', 'italic', 'oblique'] -t = plt.text(-0.4, 0.9, 'style', **alignment) +t = plt.figtext(0.3, 0.9, 'style', **alignment) for k, style in enumerate(styles): - t = plt.text(-0.4, yp[k], style, family='sans-serif', style=style, - **alignment) + t = plt.figtext(0.3, yp[k], style, family='sans-serif', style=style, + **alignment) # Show variant options variants = ['normal', 'small-caps'] -t = plt.text(0.0, 0.9, 'variant', **alignment) +t = plt.figtext(0.5, 0.9, 'variant', **alignment) for k, variant in enumerate(variants): - t = plt.text(0.0, yp[k], variant, family='serif', variant=variant, - **alignment) + t = plt.figtext(0.5, yp[k], variant, family='serif', variant=variant, + **alignment) # Show weight options weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black'] -t = plt.text(0.4, 0.9, 'weight', **alignment) +t = plt.figtext(0.7, 0.9, 'weight', **alignment) for k, weight in enumerate(weights): - t = plt.text(0.4, yp[k], weight, weight=weight, - **alignment) + t = plt.figtext(0.7, yp[k], weight, weight=weight, **alignment) # Show size options sizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'] -t = plt.text(0.8, 0.9, 'size', **alignment) +t = plt.figtext(0.9, 0.9, 'size', **alignment) for k, size in enumerate(sizes): - t = plt.text(0.8, yp[k], size, size=size, - **alignment) + t = plt.figtext(0.9, yp[k], size, size=size, **alignment) -x = -0.4 # Show bold italic -t = plt.text(x, 0.1, 'bold italic', style='italic', - weight='bold', size='x-small', - **alignment) - -t = plt.text(x, 0.2, 'bold italic', - style='italic', weight='bold', size='medium', - **alignment) - -t = plt.text(x, 0.3, 'bold italic', - style='italic', weight='bold', size='x-large', - **alignment) - -plt.axis([-1, 1, 0, 1]) +t = plt.figtext(0.3, 0.1, 'bold italic', style='italic', + weight='bold', size='x-small', + **alignment) +t = plt.figtext(0.3, 0.2, 'bold italic', + style='italic', weight='bold', size='medium', + **alignment) +t = plt.figtext(0.3, 0.3, 'bold italic', + style='italic', weight='bold', size='x-large', + **alignment) plt.show() diff --git a/examples/units/units_scatter.py b/examples/units/units_scatter.py index 095065815f4a..8a121b4afd0f 100644 --- a/examples/units/units_scatter.py +++ b/examples/units/units_scatter.py @@ -20,15 +20,11 @@ xsecs = secs * np.ma.MaskedArray(data, mask, float) fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True) + ax1.scatter(xsecs, xsecs) ax1.yaxis.set_units(secs) -ax1.axis([0, 10, 0, 10]) - ax2.scatter(xsecs, xsecs, yunits=hertz) -ax2.axis([0, 10, 0, 1]) - ax3.scatter(xsecs, xsecs, yunits=minutes) -ax3.axis([0, 10, 0, 0.2]) fig.tight_layout() plt.show() diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index f94b15e7f117..40a1f1c9d52a 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -21,7 +21,7 @@ delta_f = 5.0 s = a0 * np.sin(2 * np.pi * f0 * t) l, = plt.plot(t, s, lw=2) -plt.axis([0, 1, -10, 10]) +ax.margins(x=0) axcolor = 'lightgoldenrodyellow' axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 527501158b42..c549716c9d72 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1631,7 +1631,9 @@ def axis(self, *args, **kwargs): ---------- xmin, ymin, xmax, ymax : float, optional The axis limits to be set. Either none or all of the limits must - be given. + be given. This can also be achieved using :: + + ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) option : bool or str If a bool, turns axis lines and labels on or off. If a string,