|
| 1 | +""" |
| 2 | +Dot plot with several variables |
| 3 | +=============================== |
| 4 | +
|
| 5 | +_thumb: .3, .3 |
| 6 | +""" |
| 7 | +import seaborn as sns |
| 8 | +sns.set(style="whitegrid") |
| 9 | + |
| 10 | +# Load the dataset |
| 11 | +crashes = sns.load_dataset("car_crashes") |
| 12 | + |
| 13 | +# Make the PairGrid |
| 14 | +g = sns.PairGrid(crashes.sort("total", ascending=False), |
| 15 | + x_vars=crashes.columns[:-3], y_vars=["abbrev"], |
| 16 | + size=10, aspect=.25) |
| 17 | + |
| 18 | +# Draw a dot plot using the stripplot function |
| 19 | +g.map(sns.stripplot, size=10, orient="h", |
| 20 | + palette="Reds_r", edgecolor="gray") |
| 21 | + |
| 22 | +# Use the same x axis limits on all columns and add better labels |
| 23 | +g.set(xlim=(0, 25), xlabel="Crashes", ylabel="") |
| 24 | + |
| 25 | +# Use semantically meaningful titles for the columns |
| 26 | +titles = ["Total crashes", "Speeding crashes", "Alcohol crashes", |
| 27 | + "Not distracted crashes", "No previous crashes"] |
| 28 | + |
| 29 | +for ax, title in zip(g.axes.flat, titles): |
| 30 | + |
| 31 | + # Set a different title for each axes |
| 32 | + ax.set(title=title) |
| 33 | + |
| 34 | + # Make the grid horizontal instead of vertical |
| 35 | + ax.xaxis.grid(False) |
| 36 | + ax.yaxis.grid(True) |
| 37 | + |
| 38 | +sns.despine(left=True, bottom=True) |
0 commit comments