Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 22735cb

Browse filesBrowse files
NelleVMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #9408: updating color cycle tutorial
1 parent 0bbde92 commit 22735cb
Copy full SHA for 22735cb

File tree

Expand file treeCollapse file tree

3 files changed

+164
-20
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+164
-20
lines changed

‎doc/users/prev_whats_new/whats_new_1.5.rst

Copy file name to clipboardExpand all lines: doc/users/prev_whats_new/whats_new_1.5.rst
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ You can even multiply cyclers, which is like using `itertools.product()`
109109
on two or more property cycles. Remember to use parentheses if writing
110110
a multi-line `prop_cycle` parameter.
111111

112-
.. figure:: ../../gallery/color/images/sphx_glr_color_cycle_001.png
113-
:target: ../../gallery/color/color_cycle.html
112+
.. figure:: ../../tutorials/intermediate/images/sphx_glr_color_cycle_001.png
113+
:target: ../../tutorials/intermediate/color_cycle.html
114114
:align: center
115115
:scale: 50
116116

‎examples/animation/histogram.py

Copy file name to clipboardExpand all lines: examples/animation/histogram.py
+37-18Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
Animated histogram
44
==================
55
6-
This example shows how to use a path patch to draw a bunch of
7-
rectangles for an animated histogram.
6+
Use a path patch to draw a bunch of rectangles for an animated histogram.
87
98
"""
109
import numpy as np
@@ -14,8 +13,6 @@
1413
import matplotlib.path as path
1514
import matplotlib.animation as animation
1615

17-
fig, ax = plt.subplots()
18-
1916
# Fixing random state for reproducibility
2017
np.random.seed(19680801)
2118

@@ -30,13 +27,23 @@
3027
top = bottom + n
3128
nrects = len(left)
3229

33-
# here comes the tricky part -- we have to set up the vertex and path
34-
# codes arrays using moveto, lineto and closepoly
35-
36-
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
37-
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
38-
# it to keep the codes aligned with the vertices
39-
nverts = nrects*(1 + 3 + 1)
30+
###############################################################################
31+
# Here comes the tricky part -- we have to set up the vertex and path codes
32+
# arrays using ``plt.Path.MOVETO``, ``plt.Path.LINETO`` and
33+
# ``plt.Path.CLOSEPOLY`` for each rect.
34+
#
35+
# * We need 1 ``MOVETO`` per rectangle, which sets the initial point.
36+
# * We need 3 ``LINETO``'s, which tell Matplotlib to draw lines from
37+
# vertex 1 to vertex 2, v2 to v3, and v3 to v4.
38+
# * We then need one ``CLOSEPOLY`` which tells Matplotlib to draw a line from
39+
# the v4 to our initial vertex (the ``MOVETO`` vertex), in order to close the
40+
# polygon.
41+
#
42+
# .. note::
43+
#
44+
# The vertex for ``CLOSEPOLY`` is ignored, but we still need a placeholder
45+
# in the ``verts`` array to keep the codes aligned with the vertices.
46+
nverts = nrects * (1 + 3 + 1)
4047
verts = np.zeros((nverts, 2))
4148
codes = np.ones(nverts, int) * path.Path.LINETO
4249
codes[0::5] = path.Path.MOVETO
@@ -50,13 +57,12 @@
5057
verts[3::5, 0] = right
5158
verts[3::5, 1] = bottom
5259

53-
barpath = path.Path(verts, codes)
54-
patch = patches.PathPatch(
55-
barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
56-
ax.add_patch(patch)
57-
58-
ax.set_xlim(left[0], right[-1])
59-
ax.set_ylim(bottom.min(), top.max())
60+
###############################################################################
61+
# To animate the histogram, we need an ``animate`` function, which generates
62+
# a random set of numbers and updates the locations of the vertices for the
63+
# histogram (in this case, only the heights of each rectangle). ``patch`` will
64+
# eventually be a ``Patch`` object.
65+
patch = None
6066

6167

6268
def animate(i):
@@ -68,5 +74,18 @@ def animate(i):
6874
verts[2::5, 1] = top
6975
return [patch, ]
7076

77+
###############################################################################
78+
# And now we build the `Path` and `Patch` instances for the histogram using
79+
# our vertices and codes. We add the patch to the `Axes` instance, and setup
80+
# the `FuncAnimation` with our animate function.
81+
fig, ax = plt.subplots()
82+
barpath = path.Path(verts, codes)
83+
patch = patches.PathPatch(
84+
barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
85+
ax.add_patch(patch)
86+
87+
ax.set_xlim(left[0], right[-1])
88+
ax.set_ylim(bottom.min(), top.max())
89+
7190
ani = animation.FuncAnimation(fig, animate, 100, repeat=False, blit=True)
7291
plt.show()

‎tutorials/intermediate/color_cycle.py

Copy file name to clipboard
+125Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""
2+
===================
3+
Styling with cycler
4+
===================
5+
6+
Demo of custom property-cycle settings to control colors and other style
7+
properties for multi-line plots.
8+
9+
.. note::
10+
11+
More complete documentation of the ``cycler`` API can be found
12+
`here <http://matplotlib.org/cycler/>`_.
13+
14+
This example demonstrates two different APIs:
15+
16+
1. Setting the default rc parameter specifying the property cycle.
17+
This affects all subsequent axes (but not axes already created).
18+
2. Setting the property cycle for a single pair of axes.
19+
20+
"""
21+
from cycler import cycler
22+
import numpy as np
23+
import matplotlib.pyplot as plt
24+
25+
###############################################################################
26+
# First we'll generate some sample data, in this case, four offset sine
27+
# curves.
28+
x = np.linspace(0, 2 * np.pi, 50)
29+
offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False)
30+
yy = np.transpose([np.sin(x + phi) for phi in offsets])
31+
32+
###############################################################################
33+
# Now ``yy`` has shape
34+
print(yy.shape)
35+
36+
###############################################################################
37+
# So ``yy[:, i]`` will give you the ``i``-th offset sine curve. Let's set the
38+
# default prop_cycle using :func:`matplotlib.pyplot.rc`. We'll combine a color
39+
# cycler and a linestyle cycler by adding (``+``) two ``cycler``'s together.
40+
# See the bottom of this tutorial for more information about combining
41+
# different cyclers.
42+
default_cycler = cycler('color', ['r', 'g', 'b', 'y']) \
43+
+ cycler('linestyle', ['-', '--', ':', '-.'])
44+
45+
plt.rc('lines', linewidth=4)
46+
plt.rc('axes', prop_cycle=default_cycler)
47+
48+
###############################################################################
49+
# Now we'll generate a figure with two axes, one on top of the other. On the
50+
# first axis, we'll plot with the default cycler. On the second axis, we'll
51+
# set the prop_cycler using :func:`matplotlib.axes.Axes.set_prop_cycle`
52+
# which will only set the ``prop_cycle`` for this :mod:`matplotlib.axes.Axes`
53+
# instance. We'll use a second ``cycler`` that combines a color cycler and a
54+
# linewidth cycler.
55+
custom_cycler = cycler('color', ['c', 'm', 'y', 'k']) \
56+
+ cycler('lw', [1, 2, 3, 4])
57+
58+
fig, (ax0, ax1) = plt.subplots(nrows=2)
59+
ax0.plot(yy)
60+
ax0.set_title('Set default color cycle to rgby')
61+
ax1.set_prop_cycle(custom_cycler)
62+
ax1.plot(yy)
63+
ax1.set_title('Set axes color cycle to cmyk')
64+
65+
# Add a bit more space between the two plots.
66+
fig.subplots_adjust(hspace=0.3)
67+
plt.show()
68+
69+
###############################################################################
70+
# Setting ``prop_cycler`` in the ``matplotlibrc`` file or style files
71+
# -------------------------------------------------------------------
72+
#
73+
# Remember, if you want to set a custom ``prop_cycler`` in your
74+
# ``.matplotlibrc`` file or a style file (``style.mplstyle``), you can set the
75+
# ``axes.prop_cycle`` property:
76+
#
77+
# ..code-block:: python
78+
#
79+
# axes.prop_cycle : cycler('color', 'bgrcmyk')
80+
#
81+
# Cycling through multiple properties
82+
# -----------------------------------
83+
#
84+
# You can add cyclers:
85+
#
86+
# .. code-block:: python
87+
#
88+
# from cycler import cycler
89+
# cc = (cycler(color=list('rgb')) +
90+
# cycler(linestyle=['-', '--', '-.']))
91+
# for d in cc:
92+
# print(d)
93+
#
94+
# Results in:
95+
#
96+
# .. code-block:: python
97+
#
98+
# {'color': 'r', 'linestyle': '-'}
99+
# {'color': 'g', 'linestyle': '--'}
100+
# {'color': 'b', 'linestyle': '-.'}
101+
#
102+
#
103+
# You can multiply cyclers:
104+
#
105+
# .. code-block:: python
106+
#
107+
# from cycler import cycler
108+
# cc = (cycler(color=list('rgb')) *
109+
# cycler(linestyle=['-', '--', '-.']))
110+
# for d in cc:
111+
# print(d)
112+
#
113+
# Results in:
114+
#
115+
# .. code-block:: python
116+
#
117+
# {'color': 'r', 'linestyle': '-'}
118+
# {'color': 'r', 'linestyle': '--'}
119+
# {'color': 'r', 'linestyle': '-.'}
120+
# {'color': 'g', 'linestyle': '-'}
121+
# {'color': 'g', 'linestyle': '--'}
122+
# {'color': 'g', 'linestyle': '-.'}
123+
# {'color': 'b', 'linestyle': '-'}
124+
# {'color': 'b', 'linestyle': '--'}
125+
# {'color': 'b', 'linestyle': '-.'}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.