Aan de slagGa gratis aan de slag

Enhancing legibility

You and your colleagues have decided that the most important aspect of the data you want to show is the differences between the most "market-friendly" state, Vermont, and the least, Texas. To do this, put two plots side by side – a barplot showing the number of people per farmer's market in the state and a scatter plot showing the population on the x-axis and the number of markets on the y-axis.

Emphasize your findings by calling out Vermont and Texas by assigning them distinct colors. Also, provide a large and easy to read annotation for Texas.

Supplied is a vector state_colors that assigns Vermont and Texas unique colors and all other states gray along with the annotation describing Texas, tx_message.

Deze oefening maakt deel uit van de cursus

Improving Your Data Visualizations in Python

Cursus bekijken

Oefeninstructies

  • Map the supplied color vector state_colors to the bar plot (ax1) with the palette argument in sns.barplot().
  • Map the color vector to the scatter plot points with the c argument.
  • Make sure annotation text is legible by changing its size to 15.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Draw barplot w/ colors mapped to state_colors vector
sns.barplot('people_per_market', 'state', ____ = ____,
            data = markets_by_state, ax = ax1)

# Map state colors vector to the scatterplot as well
p = sns.scatterplot('population', 'num_markets', c = ____,
                    data = markets_by_state, s = 60, ax = ax2)

# Log the x and y scales of our scatter plot so it's easier to read
ax2.set(xscale = "____", yscale = '____')

# Increase annotation text size for legibility
ax2.annotate(tx_message, xy = (26956958,230), 
             xytext = (26956958, 450),ha = 'right', 
             size = ____, backgroundcolor = 'white',
             arrowprops = {'facecolor':'black', 'width': 3})
sns.set_style('whitegrid')
plt.show()
Code bewerken en uitvoeren