diff options
author | Ralf Gommers <ralf.gommers@gmail.com> | 2021-03-21 23:16:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-21 23:16:41 +0100 |
commit | 172251dd0b3629f248c86bbc77ab782ab50d298a (patch) | |
tree | 043086fa7d9a6bec0cc8795818e8f23c5f7c46ab | |
parent | 5e53c94de47ab5df05778283c0012adf03793dae (diff) | |
parent | bfb5e9f3450b5ecc19c1093b3f09e61037588ede (diff) | |
download | numpy-172251dd0b3629f248c86bbc77ab782ab50d298a.tar.gz |
Merge pull request #18652 from timhoffm/doc-plot
DOC: Update some plotting code to current Matplotlib idioms
-rw-r--r-- | doc/source/reference/routines.polynomials.classes.rst | 6 | ||||
-rw-r--r-- | doc/source/user/absolute_beginners.rst | 3 | ||||
-rw-r--r-- | doc/source/user/plots/matplotlib3.py | 3 | ||||
-rw-r--r-- | doc/source/user/quickstart.rst | 4 | ||||
-rw-r--r-- | doc/source/user/tutorial-ma.rst | 9 | ||||
-rw-r--r-- | numpy/fft/_pocketfft.py | 8 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 3 |
7 files changed, 21 insertions, 15 deletions
diff --git a/doc/source/reference/routines.polynomials.classes.rst b/doc/source/reference/routines.polynomials.classes.rst index 10331e9c1..5f575bed1 100644 --- a/doc/source/reference/routines.polynomials.classes.rst +++ b/doc/source/reference/routines.polynomials.classes.rst @@ -290,7 +290,8 @@ polynomials up to degree 5 are plotted below. >>> import matplotlib.pyplot as plt >>> from numpy.polynomial import Chebyshev as T >>> x = np.linspace(-1, 1, 100) - >>> for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="$T_%d$"%i) + >>> for i in range(6): + ... ax = plt.plot(x, T.basis(i)(x), lw=2, label=f"$T_{i}$") ... >>> plt.legend(loc="upper left") <matplotlib.legend.Legend object at 0x3b3ee10> @@ -304,7 +305,8 @@ The same plots over the range -2 <= `x` <= 2 look very different: >>> import matplotlib.pyplot as plt >>> from numpy.polynomial import Chebyshev as T >>> x = np.linspace(-2, 2, 100) - >>> for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="$T_%d$"%i) + >>> for i in range(6): + ... ax = plt.plot(x, T.basis(i)(x), lw=2, label=f"$T_{i}$") ... >>> plt.legend(loc="lower right") <matplotlib.legend.Legend object at 0x3b3ee10> diff --git a/doc/source/user/absolute_beginners.rst b/doc/source/user/absolute_beginners.rst index 126f5f2a3..fda73c5fb 100644 --- a/doc/source/user/absolute_beginners.rst +++ b/doc/source/user/absolute_beginners.rst @@ -1672,9 +1672,8 @@ For example, you can plot a 1D array like this:: With Matplotlib, you have access to an enormous number of visualization options. :: - >>> from mpl_toolkits.mplot3d import Axes3D >>> fig = plt.figure() - >>> ax = Axes3D(fig) + >>> ax = fig.add_subplot(projection='3d') >>> X = np.arange(-5, 5, 0.15) >>> Y = np.arange(-5, 5, 0.15) >>> X, Y = np.meshgrid(X, Y) diff --git a/doc/source/user/plots/matplotlib3.py b/doc/source/user/plots/matplotlib3.py index 20a8c0767..7b56067ef 100644 --- a/doc/source/user/plots/matplotlib3.py +++ b/doc/source/user/plots/matplotlib3.py @@ -1,9 +1,8 @@ import numpy as np import matplotlib.pyplot as plt -from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() -ax = Axes3D(fig) +ax = fig.add_subplot(projection='3d') X = np.arange(-5, 5, 0.15) Y = np.arange(-5, 5, 0.15) X, Y = np.meshgrid(X, Y) diff --git a/doc/source/user/quickstart.rst b/doc/source/user/quickstart.rst index b50d8a5ba..28262c89e 100644 --- a/doc/source/user/quickstart.rst +++ b/doc/source/user/quickstart.rst @@ -1464,11 +1464,13 @@ that ``pylab.hist`` plots the histogram automatically, while >>> mu, sigma = 2, 0.5 >>> v = rg.normal(mu, sigma, 10000) >>> # Plot a normalized histogram with 50 bins - >>> plt.hist(v, bins=50, density=1) # matplotlib version (plot) + >>> plt.hist(v, bins=50, density=True) # matplotlib version (plot) >>> # Compute the histogram with numpy and then plot it >>> (n, bins) = np.histogram(v, bins=50, density=True) # NumPy version (no plot) >>> plt.plot(.5 * (bins[1:] + bins[:-1]), n) +With Matplotlib >=3.4 you can also use ``plt.stairs(n, bins)``. + Further reading =============== diff --git a/doc/source/user/tutorial-ma.rst b/doc/source/user/tutorial-ma.rst index 88bad3cbe..a21c4aae1 100644 --- a/doc/source/user/tutorial-ma.rst +++ b/doc/source/user/tutorial-ma.rst @@ -366,12 +366,13 @@ after the beginning of the records: .. ipython:: python - plt.plot(t, china_total); - plt.plot(t[china_total.mask], cubic_fit[china_total.mask], '--', color='orange'); - plt.plot(7, np.polyval(params, 7), 'r*'); + plt.plot(t, china_total, label='Mainland China'); + plt.plot(t[china_total.mask], cubic_fit[china_total.mask], '--', + color='orange', label='Cubic estimate'); + plt.plot(7, np.polyval(params, 7), 'r*', label='7 days after start'); plt.xticks([0, 7, 13], dates[[0, 7, 13]]); plt.yticks([0, np.polyval(params, 7), 10000, 17500]); - plt.legend(['Mainland China', 'Cubic estimate', '7 days after start']); + plt.legend(); @savefig plot_covid_5.png plt.title("COVID-19 cumulative cases from Jan 21 to Feb 3 2020 - Mainland China\n" "Cubic estimate for 7 days after start"); diff --git a/numpy/fft/_pocketfft.py b/numpy/fft/_pocketfft.py index 4ed3042a6..ad69f7c83 100644 --- a/numpy/fft/_pocketfft.py +++ b/numpy/fft/_pocketfft.py @@ -300,9 +300,11 @@ def ifft(a, n=None, axis=-1, norm=None): >>> n = np.zeros((400,), dtype=complex) >>> n[40:60] = np.exp(1j*np.random.uniform(0, 2*np.pi, (20,))) >>> s = np.fft.ifft(n) - >>> plt.plot(t, s.real, 'b-', t, s.imag, 'r--') - [<matplotlib.lines.Line2D object at ...>, <matplotlib.lines.Line2D object at ...>] - >>> plt.legend(('real', 'imaginary')) + >>> plt.plot(t, s.real, label='real') + [<matplotlib.lines.Line2D object at ...>] + >>> plt.plot(t, s.imag, '--', label='imaginary') + [<matplotlib.lines.Line2D object at ...>] + >>> plt.legend() <matplotlib.legend.Legend object at ...> >>> plt.show() diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 44eac31ef..285f90a6f 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4276,7 +4276,8 @@ def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): >>> y = np.arange(-5, 5, 0.1) >>> xx, yy = np.meshgrid(x, y, sparse=True) >>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) - >>> h = plt.contourf(x,y,z) + >>> h = plt.contourf(x, y, z) + >>> plt.axis('scaled') >>> plt.show() """ |