|
3 | 3 | Scatter plot with pie chart markers
|
4 | 4 | ===================================
|
5 | 5 |
|
6 |
| -This example makes custom 'pie charts' as the markers for a scatter plot. |
7 |
| -
|
8 |
| -Thanks to Manuel Metz for the example. |
| 6 | +This example shows two methods to make custom 'pie charts' as the markers |
| 7 | +for a scatter plot. |
9 | 8 | """
|
10 | 9 |
|
| 10 | +########################################################################## |
| 11 | +# Manually creating marker vertices |
| 12 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 13 | +# |
| 14 | + |
11 | 15 | import numpy as np
|
12 | 16 | import matplotlib.pyplot as plt
|
13 | 17 |
|
14 |
| -# first define the ratios |
| 18 | +# first define the cumulative ratios |
15 | 19 | r1 = 0.2 # 20%
|
16 | 20 | r2 = r1 + 0.4 # 40%
|
17 | 21 |
|
|
36 | 40 | s3 = np.abs(xy3).max()
|
37 | 41 |
|
38 | 42 | fig, ax = plt.subplots()
|
39 |
| -ax.scatter(range(3), range(3), marker=xy1, s=s1**2 * sizes, facecolor='blue') |
40 |
| -ax.scatter(range(3), range(3), marker=xy2, s=s2**2 * sizes, facecolor='green') |
41 |
| -ax.scatter(range(3), range(3), marker=xy3, s=s3**2 * sizes, facecolor='red') |
| 43 | +ax.scatter(range(3), range(3), marker=xy1, s=s1**2 * sizes, facecolor='C0') |
| 44 | +ax.scatter(range(3), range(3), marker=xy2, s=s2**2 * sizes, facecolor='C1') |
| 45 | +ax.scatter(range(3), range(3), marker=xy3, s=s3**2 * sizes, facecolor='C2') |
| 46 | + |
| 47 | +plt.show() |
| 48 | + |
| 49 | + |
| 50 | +########################################################################## |
| 51 | +# Using wedges as markers |
| 52 | +# ~~~~~~~~~~~~~~~~~~~~~~~ |
| 53 | +# |
| 54 | +# An alternative is to create custom markers from the `~.path.Path` of a |
| 55 | +# `~.patches.Wedge`, which might be more versatile. |
| 56 | +# |
| 57 | + |
| 58 | +import numpy as np |
| 59 | +import matplotlib.pyplot as plt |
| 60 | +from matplotlib.patches import Wedge |
| 61 | +from matplotlib.markers import MarkerStyle |
| 62 | + |
| 63 | +# first define the ratios |
| 64 | +r1 = 0.2 # 20% |
| 65 | +r2 = r1 + 0.3 # 50% |
| 66 | +r3 = 1 - r1 - r2 # 30% |
| 67 | + |
| 68 | +def markers_from_ratios(ratios, width=1): |
| 69 | + markers = [] |
| 70 | + angles = 360*np.concatenate(([0], np.cumsum(ratios))) |
| 71 | + for i in range(len(angles)-1): |
| 72 | + # create a Wedge within the unit square in between the given angles... |
| 73 | + w = Wedge((0, 0), 0.5, angles[i], angles[i+1], width=width/2) |
| 74 | + # ... and create a custom Marker from its path. |
| 75 | + markers.append(MarkerStyle(w.get_path(), normalize_path=False)) |
| 76 | + return markers |
| 77 | + |
| 78 | +# define some sizes of the scatter marker |
| 79 | +sizes = np.array([100, 200, 400, 800]) |
| 80 | +# collect the markers and some colors |
| 81 | +markers = markers_from_ratios([r1, r2, r3], width=0.6) |
| 82 | +colors = plt.cm.tab10.colors[:len(markers)] |
| 83 | + |
| 84 | +fig, ax = plt.subplots() |
| 85 | + |
| 86 | +for marker, color in zip(markers, colors): |
| 87 | + ax.scatter(range(len(sizes)), range(len(sizes)), marker=marker, s=sizes, |
| 88 | + edgecolor="none", facecolor=color) |
42 | 89 |
|
| 90 | +ax.margins(0.1) |
43 | 91 | plt.show()
|
44 | 92 |
|
45 | 93 | #############################################################################
|
|
55 | 103 | import matplotlib
|
56 | 104 | matplotlib.axes.Axes.scatter
|
57 | 105 | matplotlib.pyplot.scatter
|
| 106 | +matplotlib.patches.Wedge |
| 107 | +matplotlib.markers.MarkerStyle |
0 commit comments