数据可视化【从编程小白到画图大拿】:3.柱状图与热力图

参考来源:Vitu.AI

在上一篇中你已经学会了如何画折线图,接下来我们来学习柱状图和热力图

设置你的Notebook

我们还是老样子在开头先设置一下

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
print("设置完成")

选择数据集

在本篇中我们将使用来自于美国交通部关于航班延迟的数据

点击这里 下载 数据集

用excel打开如下:

my picture

我们可以看到每个cell代表每家航空公司在2015年每个月的平均延迟时间(分钟),负数代表航班提前到达,例如美国航空(AA)在一月平均延误7分钟

我们再把csv文件上传到vitu的数据集空间里

my picture

接下来我们用pandas来加载这个文件:

# 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")

你会注意到这次我们的read_csv代码和之前的篇章有一点不同,这次的数据Month不再是dates所以我们不必再加parse_dates=True

是时候来检验一下数据了

由于这次的数据比较小,所以我们可以来看看整个数据集

# Print the data
flight_data

柱状图

比如我们想画一个柱状图显示一下按照月的Spirit Airlines(NK)的平均延迟时间

# 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 (in minutes)")

在这里插入图片描述
这句话创造了一个柱状图

sns.barplot(x=flight_data.index, y=flight_data[‘NK’])

里面有这么几个点

  • sns.barplot - 这个告诉notebook我们想画一个柱状图
  • x=flight_data.index - 这个表示x坐标使用的是flight_data这一列的索引,在这里也就是月份
  • y=flight_data[‘NK’] - 这个便是y坐标使用的是NK的延迟数据

热力图

本篇还有另外一个图要介绍

我们会在一下的代码里创造一个热力图来看flight_data,每个cell都是一个根据实际值有色的cell

# 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")

在这里插入图片描述

图中可以直观的看到哪个航空公司的延迟最严重,NK!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vitu.AI

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值