|
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,98 @@ 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 | +# All markers can be modified by a user transform in MarkerStyle constructor. |
| 170 | +# Supplied transform is combined with the default transforms needed for |
| 171 | +# selected marker shape (e.g. caret up, caret down). Following example shows |
| 172 | +# how user supplied rotation applies to several marker shapes. |
| 173 | + |
| 174 | +common_style = {k: v for k, v in filled_marker_style.items() if v != 'marker'} |
| 175 | +angles = [0, 10, 20, 30, 45, 60, 90] |
| 176 | + |
| 177 | +fig, ax = plt.subplots() |
| 178 | +fig.suptitle('Rotated markers', fontsize=14) |
| 179 | + |
| 180 | +ax.text(-0.5, 0, 'Filled marker', **text_style) |
| 181 | +for x, theta in enumerate(angles): |
| 182 | + t = Affine2D().rotate_deg(theta) |
| 183 | + ax.plot(x, 0, marker=MarkerStyle('o', 'left', t), **common_style) |
| 184 | + |
| 185 | +ax.text(-0.5, 1, 'Un-filled marker', **text_style) |
| 186 | +for x, theta in enumerate(angles): |
| 187 | + t = Affine2D().rotate_deg(theta) |
| 188 | + ax.plot(x, 1, marker=MarkerStyle('1', 'left', t), **common_style) |
| 189 | + |
| 190 | +ax.text(-0.5, 2, 'Equation marker', **text_style) |
| 191 | +for x, theta in enumerate(angles): |
| 192 | + t = Affine2D().rotate_deg(theta) |
| 193 | + eq = r'$\frac{1}{x}$' |
| 194 | + ax.plot(x, 2, marker=MarkerStyle(eq, 'left', t), **common_style) |
| 195 | + |
| 196 | +for x, theta in enumerate(angles): |
| 197 | + ax.text(x, 2.5, f"{theta}°", horizontalalignment="center") |
| 198 | +format_axes(ax) |
| 199 | + |
| 200 | +plt.show() |
| 201 | + |
| 202 | +############################################################################### |
| 203 | +# Setting marker cap style and join style |
| 204 | +# ======================================= |
| 205 | +# |
| 206 | +# All markers have predefined cap style and join style, but this can be |
| 207 | +# overriden during creation of MarkerStyle. Follwing example show how to |
| 208 | +# change the cap style and how different styles look. |
| 209 | + |
| 210 | +from matplotlib.markers import JoinStyle, CapStyle |
| 211 | + |
| 212 | +marker_inner = dict(markersize=35, |
| 213 | + markerfacecolor='tab:blue', |
| 214 | + markerfacecoloralt='lightsteelblue', |
| 215 | + markeredgecolor='brown', |
| 216 | + markeredgewidth=8, |
| 217 | + ) |
| 218 | + |
| 219 | +marker_outer = dict(markersize=35, |
| 220 | + markerfacecolor='tab:blue', |
| 221 | + markerfacecoloralt='lightsteelblue', |
| 222 | + markeredgecolor='white', |
| 223 | + markeredgewidth=1, |
| 224 | + ) |
| 225 | + |
| 226 | +fig, ax = plt.subplots() |
| 227 | +fig.suptitle('Marker CapStyle', fontsize=14) |
| 228 | +fig.subplots_adjust(left=0.1) |
| 229 | + |
| 230 | +for y, cap_style in enumerate([None, *CapStyle]): |
| 231 | + ax.text(-0.5, y, cap_style.name, **text_style) |
| 232 | + for x, theta in enumerate(angles): |
| 233 | + t = Affine2D().rotate_deg(theta) |
| 234 | + m = MarkerStyle('1', transform=t, capstyle=cap_style) |
| 235 | + ax.plot(x, y, marker=m, **marker_inner) |
| 236 | + ax.plot(x, y, marker=m, **marker_outer) |
| 237 | + ax.text(x, len(CapStyle) - .5, f'{theta}°', ha='center') |
| 238 | +format_axes(ax) |
| 239 | +plt.show() |
| 240 | + |
| 241 | +############################################################################### |
| 242 | +# Follwing example show how to change the join style and how different styles |
| 243 | +# look. |
| 244 | + |
| 245 | +fig, ax = plt.subplots() |
| 246 | +fig.suptitle('Marker JoinStyle', fontsize=14) |
| 247 | +fig.subplots_adjust(left=0.05) |
| 248 | + |
| 249 | +for y, join_style in enumerate(JoinStyle): |
| 250 | + ax.text(-0.5, y, join_style.name, **text_style) |
| 251 | + for x, theta in enumerate(angles): |
| 252 | + t = Affine2D().rotate_deg(theta) |
| 253 | + m = MarkerStyle('*', transform=t, joinstyle=join_style) |
| 254 | + ax.plot(x, y, marker=m, **marker_inner) |
| 255 | + ax.text(x, len(JoinStyle) - .5, f'{theta}°', ha='center') |
| 256 | +format_axes(ax) |
| 257 | + |
| 258 | +plt.show() |
0 commit comments