Cleaning up the background
While exploring state-level patterns in goods sold at farmer's markets, a few states stood out to you. North Dakota and New Mexico routinely fell near the bottom of the states regarding their proportion of farmer's markets selling a given good. Whereas Vermont was always near the top. You want to present the general patterns in good sales by state, while also highlighting the states you found interesting.
You make a scatter plot of goods being sold by the proportion of markets that sell that good in a state. To highlight the interesting states, you draw a line between each of the state's points. To make a clean and minimal plot, you reduce the background to a simple set of orienting grids.
Este exercício faz parte do curso
Improving Your Data Visualizations in Python
Instruções do exercício
- Set the background of the plot to be white with gridlines.
- Encode the x and y-axes of the scatter and line plots with the
'good'
being sold and'prop selling'
, respectively. - Remove all of the borders from the plot. Remember, by default,
sns.despine()
only removes the top and right border lines (spines)!
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Set background to white with grid
sns.set_style('____')
plt.scatter('____','____', marker = '_', alpha = 0.7, data = goods_by_state)
# Draw lines across goods for highlighted states
highlighted = goods_by_state.query("state in ['New Mexico','North Dakota','Vermont']")
sns.lineplot('____','____', 'state', data = highlighted, legend = False)
# Draw state name at end of lines
last_rows = highlighted.groupby('state', as_index = False).agg('first')
for _,row in last_rows.iterrows():
plt.annotate(row['state'], (row['good'], row['prop selling']),
ha = 'right', xytext = (5,0), textcoords = 'offset pixels')
# Remove all borders
sns.____(____ = ____, ____ = ____)
plt.show()