清理背景
在探索各州农贸市场商品销售的模式时,您注意到有几个州很突出。North Dakota 和 New Mexico 在各州中,按销售某种商品的农贸市场占比来看,经常排在靠后的位置;而 Vermont 几乎总是名列前茅。您希望既能呈现各州商品销售的总体模式,又能突出这些特别有意思的州。
您准备绘制一个散点图:以各州销售某种商品的市场占比为度量,展示该商品的销售情况。为突出这些有意思的州,您将连接每个州的点以成线。为得到简洁清爽的图形,您把背景简化为一组用于定位的网格。
本练习是课程的一部分
用 Python 提升数据可视化
练习说明
- 将图表背景设置为白色并带有网格线。
- 在散点图和折线图中,分别用正在销售的
'good'编码 x 轴,用'prop selling'编码 y 轴。 - 从图中移除所有边框。请记住,默认情况下,
sns.despine()只会移除上边和右边的边框线(spines)!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()