使用 countplot() 为所有交易所绘制 IPO 时间线
要对数据集中每个类别的观测数量进行基础可视化,通常可使用 seaborn 的 countplot() 函数:
seaborn.countplot(x=None, hue=None, data=None, ...)
参数 x 是 data(要绘制的 DataFrame)中变量的列名。hue 用颜色表示另一个分类变量。这只是该函数接受的众多参数中的三个可选参数;完整列表请参见 seaborn 的文档。
现在用这个工具来比较三个交易所的 IPO 活动时间线。pandas 已以 pd 导入,matplotlib.pyplot 以 plt 导入,seaborn 以 sns 导入,工作区中已提供包含参考列 'Exchange' 的 listings DataFrame。
本练习是课程的一部分
在 Python 中导入与管理金融数据
练习说明
- 将
listings过滤为仅包含 2000 年之后的 IPO 年份。 - 将
'IPO Year'列中的数据转换为整数。 - 使用
sns.countplot()绘制listings,其中x设为'IPO Year',hue设为'Exchange'。 - 将
xticks()旋转 45 度并显示结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Select IPOs after 2000
listings = listings[____[____] > ____]
# Convert IPO Year to integer
listings['IPO Year'] = ____[____].____(____)
# Create a countplot
sns.countplot(x=____, hue=____, data=____)
# Rotate xticks and show result
plt.xticks(rotation=45)
# Show the plot
plt.show()