在本教程中,您将学习足够的 Python,以创建看起来很专业的 线性图表 。然后,在下面的练习中,您将把您的新技能用于处理真实世界的数据集。
准备好笔记本
我们从设置编码环境开始。
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
选择数据集
本教程的数据集跟踪音乐流媒体服务Spotify 上的全球每日流量。我们关注2017年和2018年的五首流行歌曲:
-
"Shape of You", by Ed Sheeran (链接)
-
"Despacito", by Luis Fonzi (链接)
-
"Something Just Like This", by The Chainsmokers and Coldplay (链接)
-
"HUMBLE.", by Kendrick Lamar (链接)
-
"Unforgettable", by French Montana (链接)
请注意,第一个出现的日期是2017年1月6日,对应的发行日期"The Shape of You", by Ed Sheeran。并且,使用这个表格,你可以看到 "The Shape of You" 在发布当天在全球范围内流传了12,287,078次。注意,其他歌曲在第一行中缺少值,因为它们直到后来才发布!
加载数据
正如您在上一个教程中学到的,我们使用pd.read_csv
命令加载数据集。
# Path of the file to read
spotify_filepath = "../input/spotify.csv"
# Read the file into a variable spotify_data
spotify_data = pd.read_csv(spotify_filepath, index_col="Date", parse_dates=True)
运行上面两行代码的最终结果是,我们现在可以通过使用spotify_data
访问数据集。
检查数据
我们可以使用前面教程中学到的head
命令打印数据集的前_5_行。
# Print the first 5 rows of the data
spotify_data.head()
Shape of You | Despacito | Something Just Like This | HUMBLE. | Unforgettable | |
---|---|---|---|---|---|
Date | |||||
2017-01-06 | 12287078 | NaN | NaN | NaN | NaN |
2017-01-07 | 13190270 | NaN | NaN | NaN | NaN |
2017-01-08 | 13099919 | NaN | NaN | NaN | NaN |
2017-01-09 | 14506351 | NaN | NaN | NaN | NaN |
2017-01-10 | 14275628 | NaN | NaN | NaN | NaN |
现在检查前五行是否与上面的数据集图像(与我们在Excel中看到它的样子一致)。
空条目将显示为
NaN
,它是"Not a Number"的缩写。
我们还可以通过只做一个小的更改(其中.head()
变成.tail()
):
# Print the last five rows of the data
spotify_data.tail()
Shape of You | Despacito | Something Just Like This | HUMBLE. | Unforgettable | |
---|---|---|---|---|---|
Date | |||||
2018-01-05 | 4492978 | 3450315.0 | 2408365.0 | 2685857.0 | 2869783.0 |
2018-01-06 | 4416476 | 3394284.0 | 2188035.0 | 2559044.0 | 2743748.0 |
2018-01-07 | 4009104 | 3020789.0 | 1908129.0 | 2350985.0 | 2441045.0 |
2018-01-08 | 4135505 | 2755266.0 | 2023251.0 | 2523265.0 | 2622693.0 |
2018-01-09 | 4168506 | 2791601.0 | 2058016.0 | 2727678.0 | 2627334.0 |
谢天谢地,一切看起来都很正常,每首歌每天都有数百万条全球流量,我们可以继续绘制数据了!
数据绘制
现在数据集已经加载到笔记本中,我们只需要一行代码就可以制作一个线性图表!
# Line chart showing daily global streams of each song
sns.lineplot(data=spotify_data)
如上所述,代码行相对较短,有两个主要组成部分:
sns.lineplot
告诉笔记本我们要创建一个折线图。- 本课程中学习到的每个命令都以
sns
开头,这表示该命令来自seaborn包。例如,我们使用sns.lineplot
制作折线图。很快,您将学习到我们使用sns.barplot
和sns.heatmap
分别制作条形图和热力图。 data=spotify_data
选择将用于创建图表的数据。
请注意,创建折线图时始终使用相同的格式,使用新数据集唯一更改的是数据集的名称。因此,如果您使用名为financial_data
的不同数据集,则代码行将如下所示:
sns.lineplot(data=financial_data)
有时我们想要修改其他细节,例如图形的大小和标题。每个选项都可以轻松地用一行代码设置。
# Set the width and height of the figure
plt.figure(figsize=(14,6))
# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")
# Line chart showing daily global streams of each song
sns.lineplot(data=spotify_data)
第一行代码将图形的大小设置为 14
英寸(宽度)乘以 6
英寸(高度)。要设置 any figure 的大小,只需复制出现的代码行即可。然后,如果您想使用自定义大小,请将提供的 14
和 6
值更改为所需的宽度和高度。
第二行代码设置图的标题。请注意,标题必须 总是 用引号("..."
)括起来!
画出数据的子集
到目前为止,您已经了解了如何为数据集中的 每一 列 绘制一条线。在本节中,您将学习如何绘制列的一个 子集 。
我们将从打印所有列的名称开始。这是通过一行代码完成的,可以通过交换数据集的名称(在本例中是spotify_data
)来适应任何数据集。
list(spotify_data.columns)
['Shape of You',
'Despacito',
'Something Just Like This',
'HUMBLE.',
'Unforgettable']
在下一个代码单元格中,我们绘制与数据集中前两列对应的行。
# Set the width and height of the figure
plt.figure(figsize=(14,6))
# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")
# Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")
# Line chart showing daily global streams of 'Despacito'
sns.lineplot(data=spotify_data['Despacito'], label="Despacito")
# Add label for horizontal axis
plt.xlabel("Date")
前两行代码设置了图形的标题和大小(应该非常熟悉!)。
接下来的两行代码分别向折线图添加了一条线。例如,考虑第一行,它添加了“Shape of You”的线:
# Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")
这行代码看起来非常类似于我们在绘制数据集中的每条线时使用的代码,但它有一些关键的区别:
- 不是设置
data=spotify_data
,而是设置data=spotify_data['Shape of You']
。一般来说,要绘制单个列,我们使用这种格式,将列的名称放在单引号中,并将其括在方括号中。(为了确保您正确指定了列的名称,可以使用您学习的命令打印所有列名的列表。) - 我们还添加了
label="Shape of You"
,以使该线出现在图例中并设置其对应的标签。
最后一行代码修改了水平轴(或x轴)的标签,所需标签用引号括起来("..."
)。
标题:Kaggle数据可视化(二)线性图表
作者:Departure
地址:https://www.unreachablecity.club/articles/2023/04/19/1681917612980.html
Comments | 0 条评论