8000 Merge pull request #7900 from naoyak/mep12-pie-polar · matplotlib/matplotlib@60cd171 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60cd171

Browse files
authored
Merge pull request #7900 from naoyak/mep12-pie-polar
DOC MEP12: pie/polar and color examples + style sheets fix
2 parents dcba98d + eecad0e commit 60cd171

File tree

9 files changed

+60
-38
lines changed

9 files changed

+60
-38
lines changed

examples/color/color_cycle_default.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""
2+
====================================
3+
Colors in the default property cycle
4+
====================================
5+
26
Display the colors from the default prop_cycle.
37
"""
4-
58
import numpy as np
69
import matplotlib.pyplot as plt
710

11+
812
prop_cycle = plt.rcParams['axes.prop_cycle']
913
colors = prop_cycle.by_key()['color']
1014

examples/color/color_cycle_demo.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
"""
2-
Demo of custom property-cycle settings to control colors and such
3-
for multi-line plots.
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.
48
59
This example demonstrates two different APIs:
610
7-
1. Setting the default rc-parameter specifying the property cycle.
11+
1. Setting the default rc parameter specifying the property cycle.
812
This affects all subsequent axes (but not axes already created).
9-
2. Setting the property cycle for a specific axes. This only
10-
affects a single axes.
13+
2. Setting the property cycle for a single pair of axes.
1114
"""
1215
from cycler import cycler
1316
import numpy as np
1417
import matplotlib.pyplot as plt
1518

19+
1620
x = np.linspace(0, 2 * np.pi)
1721
offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
1822
# Create array with shifted-sine curve along each column
1923
yy = np.transpose([np.sin(x + phi) for phi in offsets])
2024

25+
# 1. Setting prop cycle on default rc parameter
2126
plt.rc('lines', linewidth=4)
2227
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
2328
cycler('linestyle', ['-', '--', ':', '-.'])))
2429
fig, (ax0, ax1) = plt.subplots(nrows=2)
2530
ax0.plot(yy)
2631
ax0.set_title('Set default color cycle to rgby')
2732

33+
# 2. Define prop cycle for single set of axes
2834
ax1.set_prop_cycle(cycler('color', ['c', 'm', 'y', 'k']) +
2935
cycler('lw', [1, 2, 3, 4]))
3036
ax1.plot(yy)

examples/color/colormaps_reference.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""
2+
==================
3+
Colormap reference
4+
==================
5+
26
Reference for colormaps included with Matplotlib.
37
48
This reference example shows all colormaps included with Matplotlib. Note that
@@ -35,9 +39,9 @@
3539
import numpy as np
3640
import matplotlib.pyplot as plt
3741

42+
3843
# Have colormaps separated into categories:
3944
# http://matplotlib.org/examples/color/colormaps_reference.html
40-
4145
cmaps = [('Perceptually Uniform Sequential',
4246
['viridis', 'inferno', 'plasma', 'magma']),
4347
('Sequential', ['Blues', 'BuGn', 'BuPu',
@@ -65,7 +69,7 @@
6569
gradient = np.vstack((gradient, gradient))
6670

6771

68-
def plot_color_gradients(cmap_category, cmap_list):
72+
def plot_color_gradients(cmap_category, cmap_list, nrows):
6973
fig, axes = plt.subplots(nrows=nrows)
7074
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
7175
axes[0].set_title(cmap_category + ' colormaps', fontsize=14)
@@ -81,7 +85,8 @@ def plot_color_gradients(cmap_category, cmap_list):
8185
for ax in axes:
8286
ax.set_axis_off()
8387

88+
8489
for cmap_category, cmap_list in cmaps:
85-
plot_color_gradients(cmap_category, cmap_list)
90+
plot_color_gradients(cmap_category, cmap_list, nrows)
8691

8792
plt.show()

examples/color/named_colors.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
"""
2-
Visualization of named colors.
2+
========================
3+
Visualizing named colors
4+
========================
35
46
Simple plot example with the named colors and its visual representation.
57
"""
8+
from __future__ import division
69

7-
from __future__ import (absolute_import, division, print_function,
8-
unicode_literals)
9-
10-
import six
11-
12-
import numpy as np
1310
import matplotlib.pyplot as plt
1411
from matplotlib import colors as mcolors
1512

1613

1714
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
1815

19-
# Sort by hue, saturation, value and name.
16+
# Sort colors by hue, saturation, value and name.
2017
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
2118
for name, color in colors.items())
22-
23-
# Get the sorted color names.
2419
sorted_names = [name for hsv, name in by_hsv]
2520

2621
n = len(sorted_names)
2722
ncols = 4
28-
nrows = int(np.ceil(1. * n / ncols))
23+
nrows = n // ncols + 1
2924

3025
fig, ax = plt.subplots(figsize=(8, 5))
3126

27+
# Get height and width
3228
X, Y = fig.get_dpi() * fig.get_size_inches()
33-
34-
# row height
3529
h = Y / (nrows + 1)
36-
# col width
3730
w = X / ncols
3831

3932
for i, name in enumerate(sorted_names):
4033
col = i % ncols
41-
row = int(i / ncols)
34+
row = i // ncols
4235
y = Y - (row * h) - h
4336

4437
xi_line = w * (col + 0.05)
@@ -49,8 +42,8 @@
4942
horizontalalignment='left',
5043
verticalalignment='center')
5144

52-
ax.hlines(
53-
y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6))
45+
ax.hlines(y + h * 0.1, xi_line, xf_line,
46+
color=colors[name], linewidth=(h * 0.6))
5447

5548
ax.set_xlim(0, X)
5649
ax.set_ylim(0, Y)

examples/pie_and_polar_charts/pie_demo_features.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""
2+
===============
3+
Basic pie chart
4+
===============
5+
26
Demo of a basic pie chart plus a few additional features.
37
48
In addition to the basic pie chart, this demo shows a few optional features:

examples/pie_and_polar_charts/polar_bar_demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
"""
2+
=======================
3+
Pie chart on polar axis
4+
=======================
5+
26
Demo of bar plot on a polar axis.
37
"""
48
import numpy as np
59
import matplotlib.pyplot as plt
610

11+
712
# Fixing random state for reproducibility
813
np.random.seed(19680801)
914

15+
# Compute pie slices
1016
N = 20
1117
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
1218
radii = 10 * np.random.rand(N)
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""
2+
==========================
3+
Scatter plot on polar axis
4+
==========================
5+
26
Demo of scatter plot on a polar axis.
37
48
Size increases radially in this example and color increases with angle
@@ -7,18 +11,18 @@
711
import numpy as np
812
import matplotlib.pyplot as plt
913

14+
1015
# Fixing random state for reproducibility
1116
np.random.seed(19680801)
1217

13-
18+
# Compute areas and colors
1419
N = 150
1520
r = 2 * np.random.rand(N)
1621
theta = 2 * np.pi * np.random.rand(N)
1722
area = 200 * r**2
1823
colors = theta
1924

2025
ax = plt.subplot(111, projection='polar')
21-
c = ax.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv)
22-
c.set_alpha(0.75)
26+
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
2327

2428
plt.show()

examples/style_sheets/plot_bmh.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
def plot_beta_hist(ax, a, b):
1919
ax.hist(beta(a, b, size=10000), histtype="stepfilled",
2020
bins=25, alpha=0.8, normed=True)
21-
return ax
2221

2322

2423
fig, ax = plt.subplots()

examples/style_sheets/plot_fivethirtyeight.py

Lines changed: 9 additions & 8 deletions
+
plt.style.use('fivethirtyeight')
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
from matplotlib import pyplot as plt
1212
import numpy as np
1313

14
15+
1416
x = np.linspace(0, 10)
1517

1618
# Fixing random state for reproducibility
1719
np.random.seed(19680801)
1820

1921
fig, ax = plt.subplots()
2022

21-
with plt.style.context('fivethirtyeight'):
22-
ax.plot(x, np.sin(x) + x + np.random.randn(50))
23-
ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
24-
ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
25-
ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50))
26-
ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50))
27-
ax.plot(x, np.sin(x) + np.random.randn(50))
28-
ax.set_title("'fivethirtyeight' style sheet")
23+
ax.plot(x, np.sin(x) + x + np.random.randn(50))
24+
ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
25+
ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
26+
ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50))
27+
ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50))
28+
ax.plot(x, np.sin(x) + np.random.randn(50))
29+
ax.set_title("'fivethirtyeight' style sheet")
2930

3031

3132
plt.show()

0 commit comments

Comments
 (0)
0