|
19 | 19 | .. redirect-from:: /gallery/shapes_and_collections/marker_path
|
20 | 20 | """
|
21 | 21 |
|
| 22 | +from matplotlib.markers import MarkerStyle |
22 | 23 | import matplotlib.pyplot as plt
|
23 | 24 | from matplotlib.lines import Line2D
|
| 25 | +from matplotlib.transforms import Affine2D |
24 | 26 |
|
25 | 27 |
|
26 | 28 | text_style = dict(horizontalalignment='right', verticalalignment='center',
|
@@ -159,3 +161,96 @@ def split_list(a_list):
|
159 | 161 | format_axes(ax)
|
160 | 162 |
|
161 | 163 | plt.show()
|
| 164 | + |
| 165 | +############################################################################### |
| 166 | +# Advanced marker modifications with transform |
| 167 | +# ============================================ |
| 168 | +# |
| 169 | +# Markers can be modified by passing a transform to the MarkerStyle |
| 170 | +# constructor. Following example shows how a supplied rotation is applied to |
| 171 | +# several marker shapes. |
| 172 | + |
| 173 | +common_style = {k: v for k, v in filled_marker_style.items() if k != 'marker'} |
| 174 | +angles = [0, 10, 20, 30, 45, 60, 90] |
| 175 | + |
| 176 | +fig, ax = plt.subplots() |
| 177 | +fig.suptitle('Rotated markers', fontsize=14) |
| 178 | + |
| 179 | +ax.text(-0.5, 0, 'Filled marker', **text_style) |
| 180 | +for x, theta in enumerate(angles): |
| 181 | + t = Affine2D().rotate_deg(theta) |
| 182 | + ax.plot(x, 0, marker=MarkerStyle('o', 'left', t), **common_style) |
| 183 | + |
| 184 | +ax.text(-0.5, 1, 'Un-filled marker', **text_style) |
| 185 | +for x, theta in enumerate(angles): |
| 186 | + t = Affine2D().rotate_deg(theta) |
| 187 | + ax.plot(x, 1, marker=MarkerStyle('1', 'left', t), **common_style) |
| 188 | + |
| 189 | +ax.text(-0.5, 2, 'Equation marker', **text_style) |
| 190 | +for x, theta in enumerate(angles): |
| 191 | + t = Affine2D().rotate_deg(theta) |
| 192 | + eq = r'$\frac{1}{x}$' |
| 193 | + ax.plot(x, 2, marker=MarkerStyle(eq, 'left', t), **common_style) |
| 194 | + |
| 195 | +for x, theta in enumerate(angles): |
| 196 | + ax.text(x, 2.5, f"{theta}°", horizontalalignment="center") |
| 197 | +format_axes(ax) |
| 198 | + |
| 199 | +fig.tight_layout() |
| 200 | +plt.show() |
| 201 | + |
| 202 | +############################################################################### |
| 203 | +# Setting marker cap style and join style |
| 204 | +# ======================================= |
| 205 | +# |
| 206 | +# Markers have default cap and join styles, but these can be |
| 207 | +# customized when creating a MarkerStyle. |
| 208 | + |
| 209 | +from matplotlib.markers import JoinStyle, CapStyle |
| 210 | + |
| 211 | +marker_inner = dict(markersize=35, |
| 212 | + markerfacecolor='tab:blue', |
| 213 | + markerfacecoloralt='lightsteelblue', |
| 214 | + markeredgecolor='brown', |
| 215 | + markeredgewidth=8, |
| 216 | + ) |
| 217 | + |
| 218 | +marker_outer = dict(markersize=35, |
| 219 | + markerfacecolor='tab:blue', |
| 220 | + markerfacecoloralt='lightsteelblue', |
| 221 | + markeredgecolor='white', |
| 222 | + markeredgewidth=1, |
| 223 | + ) |
| 224 | + |
| 225 | +fig, ax = plt.subplots() |
| 226 | +fig.suptitle('Marker CapStyle', fontsize=14) |
| 227 | +fig.subplots_adjust(left=0.1) |
| 228 | + |
| 229 | +for y, cap_style in enumerate(CapStyle): |
| 230 | + ax.text(-0.5, y, cap_style.name, **text_style) |
| 231 | + for x, theta in enumerate(angles): |
| 232 | + t = Affine2D().rotate_deg(theta) |
| 233 | + m = MarkerStyle('1', transform=t, capstyle=cap_style) |
| 234 | + ax.plot(x, y, marker=m, **marker_inner) |
| 235 | + ax.plot(x, y, marker=m, **marker_outer) |
| 236 | + ax.text(x, len(CapStyle) - .5, f'{theta}°', ha='center') |
| 237 | +format_axes(ax) |
| 238 | +plt.show() |
| 239 | + |
| 240 | +############################################################################### |
| 241 | +# Modifying the join style: |
| 242 | + |
| 243 | +fig, ax = plt.subplots() |
| 244 | +fig.suptitle('Marker JoinStyle', fontsize=14) |
| 245 | +fig.subplots_adjust(left=0.05) |
| 246 | + |
| 247 | +for y, join_style in enumerate(JoinStyle): |
| 248 | + ax.text(-0.5, y, join_style.name, **text_style) |
| 249 | + for x, theta in enumerate(angles): |
| 250 | + t = Affine2D().rotate_deg(theta) |
| 251 | + m = MarkerStyle('*', transform=t, joinstyle=join_style) |
| 252 | + ax.plot(x, y, marker=m, **marker_inner) |
| 253 | + ax.text(x, len(JoinStyle) - .5, f'{theta}°', ha='center') |
| 254 | +format_axes(ax) |
| 255 | + |
| 256 | +plt.show() |
0 commit comments