Day 21 [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来读取档案

# Path of the file to read
flight_filepath = "./flight_delays.csv"

# Read the file into a variable flight_data
flight_data = pd.read_csv(flight_filepath, index_col="Month")

因为month这个column并不是日期的格式,因此在读取的时候,不用加上parse_dates=True

显示资料

因为资料集很小,可以只用一行就印出全部的资料

# Print the data
flight_data

长条图

将Airline code NK的班机做成bar chart

# Set the width and height of the figure
plt.figure(figsize=(10,6))

# Add title
plt.title("Average Arrival Delay for Spirit Airlines Flights, by Month")

# Bar chart showing average arrival delay for Spirit Airlines Flights by month
sns.barplot(x=flight_data.index, y=flight_data['NK'])

# Add label for vertical axis
plt.ylabel("Arrival delay (minutes)")
Text(0, 0.5, 'Arrival delay (minutes)')

在bar chart中,有几个主要的components

  • sns.barplot - 告诉Notebook要创造一个bar chart
  • x=flight_data.index - 选择index当作x轴的资料
  • y=flight_data['NK'] - 要用什麽资料来当作bar chart的高度
  • **Important Note:**若x已经设定index,index是用month,因此y轴不能使用flight_data['month']

热度图

下方的程序码是将flight_data的资料做成heatmap

# Set the width and height of the figure
plt.figure(figsize=(14,7))

# Add title
plt.title("Average Arrival Delay for Each Airline, by Month")

# Heatmap showing average arrival delay for each airline by month
sns.heatmap(data=flight_data, annot=True)

# Add label for horizontal axis
plt.xlabel("Airline")
Text(0.5, 42.0, 'Airline')

heatmap也包含三个组件

  • sns.heatmap - 告诉notebook要创建heatmap
  • data=flight_data - 告诉heatmap要使用甚麽资料
  • annot=True - 将值显示在heatmap中,若没有使用的话,只看的到颜色,看不到数值

<<:  Day19 - 写出更有品质的程序码,信 eslint 得永生

>>:  Day 19 : 笔记篇 06 — 结合资讯处理流程,让笔记的 Metadata 变得更详细

[Day18] 第十八章-API资料如何呈现在前端的页面上(blade跟view呈现)

前言 昨天我们算是把api service完成了 那我们今天来写一些简单的前端 以及使用balde的...

Day09,我也好想用用看Terraform

正文 今天要来开机器,先去下载server版的Ubuntu 20.04 iso,因为以前自己做环境练...

[iT铁人赛Day20]JAVA学习心得

做完了这几天的JAVA分享。。。我说是分享啦,因为我没有厉害到可以教别人 恩,所以做完分享之後,我也...

Day5 休息一日思考下一步

前面四篇讲的 slice 基础都是当天看当天吸收,消化过後出在铁人赛的文章上。这样安排非常紧凑的,但...

Day29:刷起来! leetcode

leetcode可以说是工程师的试炼场,收集了许多公司的面试考题,可说是题海无涯,那麽就开始刷题之...