开始使用免费开始使用

提升可读性

您和同事决定,本次要展示的数据中,最重要的是对比最"适合市场"的州 Vermont 与最不适合的州 Texas。为此,请并排放置两个图表——一个条形图显示各州每个农贸市场对应的人数,另一个散点图在 x 轴显示人口数,在 y 轴显示市场数量。

通过为 Vermont 和 Texas 指定不同的颜色来突出结论。此外,请为 Texas 提供一条字号较大、易读的标注。

已提供向量 state_colors,它为 Vermont 和 Texas 指定了独特颜色,并将其他所有州设为灰色;同时还提供了描述 Texas 的标注文本 tx_message

本练习是课程的一部分

用 Python 提升数据可视化

查看课程

练习说明

  • 在条形图(ax1)中,将提供的颜色向量 state_colors 通过 sns.barplot()palette 参数进行映射。
  • 在散点图中,通过参数 c 将颜色向量映射到点上。
  • 将标注文字的字号设置为 15,以确保清晰可读。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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()
编辑并运行代码