10000 DOC: new plot gallery by jklymak · Pull Request #19703 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: new plot gallery #19703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<li><a href="{{ pathto('users/installing') }}">Installation</a></li>
<li><a href="{{ pathto('contents') }}">Documentation</a></li>
<li><a href="{{ pathto('api/index') }}">API</a></li>
<li><a href="{{ pathto('plot_types/index') }}">Plot Types</a></li>
<li><a href="{{ pathto('gallery/index') }}">Examples</a></li>
<li><a href="{{ pathto('tutorials/index') }}">Tutorials</a></li>
<li><a href="{{ pathto('devel/index') }}">Contributing</a></li>
Expand Down
4 changes: 2 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ def _check_dependencies():

# Sphinx gallery configuration
sphinx_gallery_conf = {
'examples_dirs': ['../examples', '../tutorials'],
'examples_dirs': ['../examples', '../tutorials', '../plot_types'],
'filename_pattern': '^((?!sgskip).)*$',
'gallery_dirs': ['gallery', 'tutorials'],
'gallery_dirs': ['gallery', 'tutorials', 'plot_types'],
'doc_module': ('matplotlib', 'mpl_toolkits'),
'reference_url': {
'matplotlib': None,
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/mpl_plot_gallery.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# from the Matplotlib cheatsheet as used in our gallery:

axes.grid: True
axes.axisbelow: True

figure.figsize: 2, 2
# make it so the axes labels don't show up. Obviously
# not good style for any quantitative analysis:
figure.subplot.left: 0.01
figure.subplot.right: 0.99
figure.subplot.bottom: 0.01
figure.subplot.top: 0.99

xtick.major.size: 0.0
ytick.major.size: 0.0

# colors:
image.cmap : Blues
# axes.prop_cycle: cycler('color', ['FF7F0E', '1F77B4', '2CA02C'])
6 changes: 6 additions & 0 deletions plot_types/A_basic/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _basic_plots:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this in the top-level (of the repo, not the docs site) instead of in doc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other galleries are at the top level...


Basic
-----

Basic plot types, usually x versus y.
25 changes: 25 additions & 0 deletions plot_types/A_basic/a_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
======================
plot([X], Y, [fmt]...)
======================
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave out the parameters in the title. They are a bit cluttered.

We should however add a bit of info inside the example, e.g.

  • What this plot is. In the simplest case, this is just a repetition of the title line of the function documentation. But one might also give a little more context.
  • Prominent link to the docs
  • Optional: You could add the pse 341A udo-signature from the title if you think that is helpful.
Suggested change
"""
======================
plot([X], Y, [fmt]...)
======================
"""
"""
======
plot()
======
"""
.. code-block:: none
plot([X], Y, [fmt]...)
Plot y versus x as lines and/or markers.
Documentation: `matplotlib.axes.Axes.plot()` / `matplotlib.pyplot.plot()`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was basically copying the cheatsheet, which I think was pretty good. If you would like to simplify it later, and add more info to the main text, I think we could do as follow-ups.


import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
X = np.linspace(0, 10, 100)
Y = 4 + 2 * np.sin(2 * X)

# plot
fig, ax = plt.subplots()

ax.plot(X, Y, linewidth=2.0)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))
plt.show()
27 changes: 27 additions & 0 deletions plot_types/A_basic/b_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
==================
scatter(X, Y, ...)
==================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make the data
np.random.seed(3)
X = 4 + np.random.normal(0, 2, 24)
Y = 4 + np.random.normal(0, 2, len(X))
# size and color:
S = np.random.uniform(15, 80, len(X))

# plot
fig, ax = plt.subplots()

ax.scatter(X, Y, s=S, c=-S, cmap=plt.get_cmap('Blues'), vmin=-100, vmax=0)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))
plt.show()
24 changes: 24 additions & 0 deletions plot_types/A_basic/c_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
======================
bar[h](x, height, ...)
======================
"""
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('mpl_plot_gallery')

# make data:
np.random.seed(3)
X = 0.5 + np.arange(8)
Y = np.random.uniform(2, 7, len(X))

# plot
fig, ax = plt.subplots()

ax.bar(X, Y, width=1, edgecolor="white", linewidth=0.7)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))
plt.show()
25 changes: 25 additions & 0 deletions plot_types/A_basic/d_stem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
=================
stem([x], y, ...)
=================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
np.random.seed(3)
X = 0.5 + np.arange(8)
Y = np.random.uniform(2, 7, len(X))

# plot
fig, ax = plt.subplots()

ax.stem(X, Y, bottom=0, linefmt="-", markerfmt="d")

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))
plt.show()
25 changes: 25 additions & 0 deletions plot_types/A_basic/e_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
=====================
step(x, y, where=...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to have stairs() as well. Two alternatives:

  • name this "step-like" and show both here
  • or make a separate entry and link between the two.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we can add more, or modify as wanted after this is merged.

=====================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
np.random.seed(3)
X = 0.5 + np.arange(8)
Y = np.random.uniform(2, 7, len(X))

# plot
fig, ax = plt.subplots()

ax.step(X, Y, linewidth=2.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird to have where= in the page title, but not shown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a gallery, not documentation for the functions. So the title provides useful keywords the user may be interested in, but we aren't necessarily trying to demo those kwargs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my proposal above for leaving parameters out of the title and optionally add them to the body.


ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))
plt.show()
31 changes: 31 additions & 0 deletions plot_types/A_basic/f_pie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
====================
pie(X, explode, ...)
Copy link
Member

Choose a reason for hiding this comment

57AE

The reason will be displayed to describe this comment to others. Learn more.

Again, explode is not shown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... as above...

====================
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')


# make data
X = [1, 2, 3, 4]
colors = np.zeros((len(X), 4))
colors[:] = mpl.colors.to_rgba("C0")
colors[:, 3] = np.linspace(0.25, 0.75, len(X))

# plot
fig, ax = plt.subplots()
ax.pie(X, colors=["white"]*len(X), radius=3, center=(4, 4),
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
ax.pie(X, colors=colors, radius=3, center=(4, 4),
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))

plt.show()
28 changes: 28 additions & 0 deletions plot_types/A_basic/g_fill_between.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
=================================
fill[_between][x](X, Y1, Y2, ...)
=================================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
np.random.seed(1)
X = np.linspace(0, 8, 16)
Y1 = 3 + 4*X/8 + np.random.uniform(0.0, 0.5, len(X))
Y2 = 1 + 2*X/8 + np.random.uniform(0.0, 0.5, len(X))

# plot
fig, ax = plt.subplots()

ax.fill_between(X, Y1, Y2, alpha=.5, linewidth=0)
ax.plot(X, (Y1+Y2)/2, linewidth=2.5)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))

plt.show()
6 changes: 6 additions & 0 deletions plot_types/B_arrays/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _array_plots:

Plots of arrays and fields
--------------------------

Plotting for arrays of data ``Z(x, y)`` and fields ``U(x, y), V(x, y)``
26 changes: 26 additions & 0 deletions plot_types/B_arrays/a_imshow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
=======================
imshow(Z, [cmap=], ...)
=======================
"""

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
Z = Z - Z.min()
Z = Z[::16, ::16]

# plot
fig, ax = plt.subplots()

ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest",
cmap=plt.get_cmap('Blues'), vmin=0, vmax=1.6)

ax.set_xticks([])
ax.set_yticks([])
plt.show()
33 changes: 33 additions & 0 deletions plot_types/B_arrays/b_pcolormesh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
===================================
pcolormesh([X, Y], Z, [cmap=], ...)
===================================

`~.axes.Axes.pcolormesh` is more flexible than `~.axes.Axes.imshow` in that
the x and y vectors need not be equally spaced (indeed they can be skewed).

"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make full-res data
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
Z = Z - Z.min()

# sample unevenly in x:
dx = np.sqrt((np.arange(16) - 8)**2) + 6
dx = np.floor(dx / sum(dx) * 255)
xint = np.cumsum(dx).astype('int')
X = X[0, xint]
Y = Y[::8, 0]
Z = Z[::8, :][:, xint]

# plot
fig, ax = plt.subplots()

ax.pcolormesh(X, Y, Z, vmin=0, vmax=1.5, shading='nearest')

plt.show()
23 changes: 23 additions & 0 deletions plot_types/B_arrays/c_contourf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
=====================================
contour[f]([X, Y], Z, [levels=], ...)
=====================================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2. + X**5 + Y**3)*np.exp(-X**2-Y**2)
Z = Z - Z.min()
levs = np.linspace(np.min(Z), np.max(Z), 7)

# plot
fig, ax = plt.subplots()

plt.contourf(X, Y, Z, levels=levs)
plt.contour(X, Y, Z, levels=levs, colors="white", linewidths=0.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, this looked like the bug with alpha and multiple filled contours to me. I don't know if you want to use a different colour for the contours, but I'm not sure which colour matches the theme.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following this concern.... what is wrong with just adding white contours on top of the contourf?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first saw it, I thought it was #4419 and not on purpose.


plt.show()
27 changes: 27 additions & 0 deletions plot_types/B_arrays/d_quiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
=========================
quiver([X, Y], U, V, ...)
=========================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make data
T = np.linspace(0, 2*np.pi, 8)
X, Y = 4 + 1 * np.cos(T), 4 + 1 * np.sin(T)
U, V = 1.5 * np.cos(T), 1.5 * np.sin(T)

# plot
fig, ax = plt.subplots()

plt.quiver(X, Y, U, V, color="C0", angles='xy',
scale_units='xy', scale=0.5, width=.05)

ax.set_xlim(0, 8)
ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8)
ax.set_yticks(np.arange(1, 8))

plt.show()
26 changes: 26 additions & 0 deletions plot_types/B_arrays/e_streamplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
========================
streamplot([X, Y], U, V)
========================
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('mpl_plot_gallery')

# make a stream function:
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
Z = Z - Z.min()
# make U and V out of the streamfunction:
V = np.diff(Z[1:, :], axis=1)
U = -np.diff(Z[:, 1:], axis=0)

# plot:
fig, ax = plt.subplots()
# contour stream function
ax.contour(X, Y, Z, colors='C1', alpha=0.5, zorder=1, linewidths=3)
# plot stream plot
ax.streamplot(X[1:, 1:], Y[1:, 1:], U, V, zorder=2)

plt.show()
6 changes: 6 additions & 0 deletions plot_types/C_stats/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _stats_plots:

Specialized statistics plots
============================

Matplotlib has some specialized plots for statistical analysis.
Loading
0