CommencerCommencer gratuitement

Using a plot as a legend

One interesting thread of investigation in the farmer's market data is a state's "market friendliness" and specifically, the outliers. One way to look at this is by using the ratio of farmer's markets to people by state. You could directly look at the ratio; however, a ratio throws away the raw information about a state's population and the number of markets. A large state with a high ratio could be more interesting than a small one.

You can show both the ratio and raw numbers by drawing two plots, one of the ratio and the other of the market number to population scatter plot. To help simplify your now dense visualization, you can use the bar plot as a legend; calling out interesting states by matching the colors of their bars and scatter points.

Cet exercice fait partie du cours

Improving Your Data Visualizations in Python

Afficher le cours

Instructions

  • Set up two plots side-by-side using plt.subplots().
  • Map the column is_selected to the color of both the bar and scatter plot.
  • Disable dodge on the bar plot, so the bars are full height.
  • Remove the legends in both plots.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Set up two side-by-side plots
f, (ax1, ax2) = plt.subplots(____, ____, figsize = (15, 15))

# Map the column for selected states to the bar color
sns.barplot('people_per_market', 'state', hue = '____',
            # Disable dodge so bars are full size
            dodge = ____, 
            data = markets_by_state, ax = ax1)

# Map selected states to point color
sns.scatterplot('log_pop', 'log_markets', hue = '____', 
                data = markets_by_state, ax = ax2, s = 100)

# Remove the legend for both plots
ax1.____()
ax2.____() 
plt.show() 
Modifier et exécuter le code