Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b081bb9

Browse files
committed
added gallery example
1 parent 018a748 commit b081bb9

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

galleries/examples/shapes_and_collections/hatchcolor_demo.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""
2-
================
3-
Patch hatchcolor
4-
================
2+
===============
3+
Hatchcolor Demo
4+
===============
5+
6+
Patch Hatchcolor
7+
----------------
58
69
This example shows how to use the *hatchcolor* parameter to set the color of
710
the hatch. The *hatchcolor* parameter is available for `~.patches.Patch`,
@@ -11,6 +14,7 @@
1114
import matplotlib.pyplot as plt
1215
import numpy as np
1316

17+
import matplotlib.cm as cm
1418
from matplotlib.patches import Rectangle
1519

1620
fig, (ax1, ax2) = plt.subplots(1, 2)
@@ -28,6 +32,49 @@
2832
ax2.set_xlim(0, 5)
2933
ax2.set_ylim(0, 5)
3034

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+
3178
plt.show()
3279

3380
# %%
@@ -41,3 +88,5 @@
4188
# - `matplotlib.patches.Polygon`
4289
# - `matplotlib.axes.Axes.add_patch`
4390
# - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`
91+
# - `matplotlib.collections`
92+
# - `matplotlib.axes.Axes.scatter` / `matplotlib.pyplot.scatter`

0 commit comments

Comments
 (0)