|
1 | 1 | """
|
2 |
| -================ |
3 |
| -Patch hatchcolor |
4 |
| -================ |
| 2 | +=============== |
| 3 | +Hatchcolor Demo |
| 4 | +=============== |
| 5 | +
|
| 6 | +Patch Hatchcolor |
| 7 | +---------------- |
5 | 8 |
|
6 | 9 | This example shows how to use the *hatchcolor* parameter to set the color of
|
7 | 10 | the hatch. The *hatchcolor* parameter is available for `~.patches.Patch`,
|
|
11 | 14 | import matplotlib.pyplot as plt
|
12 | 15 | import numpy as np
|
13 | 16 |
|
| 17 | +import matplotlib.cm as cm |
14 | 18 | from matplotlib.patches import Rectangle
|
15 | 19 |
|
16 | 20 | fig, (ax1, ax2) = plt.subplots(1, 2)
|
|
28 | 32 | ax2.set_xlim(0, 5)
|
29 | 33 | ax2.set_ylim(0, 5)
|
30 | 34 |
|
| 35 | +# %% |
| 36 | +# Collection Hatchcolor |
| 37 | +# --------------------- |
| 38 | +# |
| 39 | +# The following example shows how to use the *hatchcolor* parameter to set the color of |
| 40 | +# the hatch in a scatter plot. The *hatchcolor* parameter can also be passed to |
| 41 | +# `~.collections.Collection`, child classes of Collection, and methods that pass |
| 42 | +# through to Collection. |
| 43 | + |
| 44 | +fig, ax = plt.subplots() |
| 45 | + |
| 46 | +num_points_x = 10 |
| 47 | +num_points_y = 9 |
| 48 | +x = np.linspace(0, 1, num_points_x) |
| 49 | +y = np.linspace(0, 1, num_points_y) |
| 50 | + |
| 51 | +X, Y = np.meshgrid(x, y) |
| 52 | +X[1::2, :] += (x[1] - x[0]) / 2 # stagger every alternate row |
| 53 | + |
| 54 | +# As ax.scatter (PathCollection) is drawn row by row, setting hatchcolors to the |
| 55 | +# first row is enough, as the colors will be cycled through for the next rows. |
| 56 | +colors = [cm.rainbow(val) for val in x] |
| 57 | + |
| 58 | +ax.scatter( |
| 59 | + X.ravel(), |
| 60 | + Y.ravel(), |
| 61 | + s=1700, |
| 62 | + facecolor="none", |
| 63 | + edgecolor="gray", |
| 64 | + linewidth=2, |
| 65 | + marker="h", # Use hexagon as marker |
| 66 | + hatch="xxx", |
| 67 | + hatchcolor=colors, |
| 68 | +) |
| 69 | +ax.set_xlim(0, 1) |
| 70 | +ax.set_ylim(0, 1) |
| 71 | + |
| 72 | +# Remove ticks and labels |
| 73 | +ax.set_xticks([]) |
| 74 | +ax.set_yticks([]) |
| 75 | +ax.set_xticklabels([]) |
| 76 | +ax.set_yticklabels([]) |
| 77 | + |
31 | 78 | plt.show()
|
32 | 79 |
|
33 | 80 | # %%
|
|
41 | 88 | # - `matplotlib.patches.Polygon`
|
42 | 89 | # - `matplotlib.axes.Axes.add_patch`
|
43 | 90 | # - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`
|
| 91 | +# - `matplotlib.collections` |
| 92 | +# - `matplotlib.axes.Axes.scatter` / `matplotlib.pyplot.scatter` |
0 commit comments