iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python 数据可视化之Seaborn详解
  • 191
分享到

Python 数据可视化之Seaborn详解

2024-04-02 19:04:59 191人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录安装散点图线图条形图直方图总结安装 要安装 seaborn,请在终端中输入以下命令。 pip install seaborn Seaborn 建立在 Matplotlib

安装

要安装 seaborn,请在终端中输入以下命令。

pip install seaborn

image.png

Seaborn 建立在 Matplotlib 之上,因此它也可以与 Matplotlib 一起使用。一起使用 Matplotlib 和 Seaborn 是一个非常简单的过程。我们只需要像之前一样调用 Seaborn Plotting 函数,然后就可以使用 Matplotlib 的自定义函数了。

注意: Seaborn 加载了提示、虹膜等数据集,但在本教程中,我们将使用 pandas 加载这些数据集。

例子:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# 读取数据库
data = pd.read_csv("tips.csv")
# 画线图
sns.lineplot(x="sex", y="total_bill", data=data)
# 使用 Matplotlib 设置标题
plt.title('Title using Matplotlib Function')
plt.show()

输出:

image.png

散点图

散点图是使用scatterplot() 方法绘制的。这类似于 Matplotlib,但需要额外的参数数据。


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据库
data = pd.read_csv("tips.csv")
sns.scatterplot(x='day', y='tip', data=data,)
plt.show()

输出:

image.png

你会发现在使用 Matplotlib 时,如果你想根据sex为这个图的每个点着色会很困难。 但在散点图中,它可以在色调参数的帮助下完成。

例子:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据库
data = pd.read_csv("tips.csv")
sns.scatterplot(x='day', y='tip', data=data,
			hue='sex')
plt.show()

输出:

image.png

线图

Seaborn 中的 Line Plot 使用 lineplot() 方法绘制。 在这种情况下,我们也可以只传递 data 参数。

示例:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# 读取数据库
data = pd.read_csv("tips.csv")
sns.lineplot(x='day', y='tip', data=data)
plt.show()

输出:

image.png

示例 2:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# 读取数据库
data = pd.read_csv("tips.csv")
# 仅使用数据属性
sns.lineplot(data=data.drop(['total_bill'], axis=1))
plt.show()

输出:

image.png

条形图

Seaborn 中的条形图可以使用barplot()方法.

例子:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# 读取数据库
data = pd.read_csv("tips.csv")
sns.barplot(x='day',y='tip', data=data,
			hue='sex')
plt.show()

输出:

image.png

直方图

Seaborn 中的直方图可以使用histplot() 函数绘制。

例子:


# 导包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# 读取数据库
data = pd.read_csv("tips.csv")
sns.histplot(x='total_bill', data=data, kde=True, hue='sex')
plt.show()

输出:

image.png

在浏览完所有这些绘图后,您一定已经注意到,使用 Seaborn 自定义绘图比使用 Matplotlib 容易得多。 它也是基于 matplotlib 构建的,那么我们也可以在使用 Seaborn 时使用 matplotlib 函数。下一节我们继续谈第三个库——Bokeh

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: Python 数据可视化之Seaborn详解

本文链接: https://www.lsjlt.com/news/156138.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作