Day 20 [Python ML、资料视觉化] 折线图

设定jupyter notebook

import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
print("Setup Complete")
Setup Complete

读取资料

可以使用pd.read_csv来读取csv档案

# Path of the file to read
spotify_filepath = "./spotify.csv"

# Read the file into a variable spotify_data
spotify_data = pd.read_csv(spotify_filepath, index_col="Date", parse_dates=True)

查看资料

使用head()可以看到前5笔资料

spotify_data.head()

若要印出最後5笔资料,可以使用tail()

spotify_data.tail()

将资料转换成图表

现在我们已经有资料了,只需要一行就可以将资料转为图表

# Line chart showing daily global streams of each song
sns.lineplot(data=spotify_data)
<AxesSubplot:xlabel='Date'>

sns.lineplot代表说要使用line chart

data=spotify_data可以选择要使用的资料

若要设定图表的宽度及高度,或是图表的title,可以使用以下的方法

# 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 golbal streams of each song
sns.lineplot(data=spotify_data)
<AxesSubplot:title={'center':'Daily Global Streams of Popular Songs in 2017-2018'}, xlabel='Date'>


)

Plot a subset of the data

我们可以先去找出有那些column可以用

list(spotify_data.columns)
['Shape of You',
 'Despacito',
 'Something Just Like This',
 'HUMBLE.',
 'Unforgettable']

选择Shpae of YouDespacito来绘制line chart

# 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")
Text(0.5, 0, 'Date')


<<:  [从0到1] C#小乳牛 练成基础程序逻辑 Day 18 - do while回圈 作用域 break + continue

>>:  中阶魔法 - 执行环境与执行堆叠

JavaScript 之旅 (26):String.prototype.replaceAll()

本篇介绍 ES2021 (ES12) 提供的 String.prototype.replaceAl...

Day 25:53. Maximum Subarray (1)

今日题目 题目连结:53. Maximum Subarray 题目主题:Array, Divide ...

OAuth 2.0

Golang OAuth 2.0 在一开始的开赛目标就是希望可以完成golang + OAuth 2...

【Day 14】- 今天来实作一个键盘监听器

Agenda 资安宣言 测试环境与工具 技术原理与程序码 References 下期预告 资安宣言 ...

从零开始的8-bit迷宫探险【Level 29】让你的 App 与众不同!设计 Icon 及 LaunchScreen

回到村子後,山姆变成了斜杠青年,他将探险的故事写成了一本书。 书的封面印着山姆的肖像。 而书名就叫...