|
| 1 | +""" |
| 2 | +============================================== |
| 3 | +Mapping marker properties to multivariate data |
| 4 | +============================================== |
| 5 | +
|
| 6 | +This example shows how to use different properties of markers to plot |
| 7 | +multivariate datasets. Following example shows an illustrative case of |
| 8 | +plotting success of baseball throw as an smiley face with size mapped to |
| 9 | +the skill of thrower, rotation mapped to the take-off angle, and thrust |
| 10 | +to the color of the marker. |
| 11 | +""" |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +from matplotlib.markers import MarkerStyle |
| 16 | +from matplotlib.transforms import Affine2D |
| 17 | +from matplotlib.textpath import TextPath |
| 18 | +from matplotlib.colors import Normalize |
| 19 | + |
| 20 | +SUCCESS_SYMBOLS = [ |
| 21 | + TextPath((0,0), "☹"), |
| 22 | + TextPath((0,0), "😒"), |
| 23 | + TextPath((0,0), "☺"), |
| 24 | +] |
| 25 | + |
| 26 | +N = 25 |
| 27 | +np.random.seed(42) |
| 28 | +skills = np.random.uniform(5, 80, size=N) * 0.1 + 5 |
| 29 | +takeoff_angles = np.random.normal(0, 90, N) |
| 30 | +thrusts = np.random.uniform(size=N) |
| 31 | +successfull = np.random.randint(0, 3, size=N) |
| 32 | +positions = np.random.normal(size=(N, 2)) * 5 |
| 33 | +data = zip(skills, takeoff_angles, thrusts, successfull, positions) |
| 34 | + |
| 35 | +cmap = plt.cm.get_cmap("plasma") |
| 36 | +fig, ax = plt.subplots() |
| 37 | +fig.suptitle("Throwing success", size=14) |
| 38 | +for skill, takeoff, thrust, mood, pos in data: |
| 39 | + t = Affine2D().scale(skill).rotate_deg(takeoff) |
| 40 | + m = MarkerStyle(SUCCESS_SYMBOLS[mood], transform=t) |
| 41 | + ax.plot(pos[0], pos[1], marker=m, color=cmap(thrust)) |
| 42 | +fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap=cmap), |
| 43 | + ax=ax, label="Normalized Thrust [a.u.]") |
| 44 | +ax.set_xlabel("X position [m]") |
| 45 | +ax.set_ylabel("Y position [m]") |
0 commit comments