seaborn和matplotlib的一些用法

seaborn的一些用法

1.sns.distplot()

能够画出对应数据的概率密度图,初步判断数据分布情况

import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定中文字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号

col = "df对应的列名"
f = pd.read_csv("tsv文件.tsv", sep="\t", encoding="utf-8")
sns.distplot(f[col], rug=True, hist=True, kde=True, color="g")
plt.show()

运行结果
在这里插入图片描述

x轴为对应出现的各种数据,y为对应数据出现的概率,因此大致来看,我这组数据可能是幂律分布,不过还没有进行验证。

2.实现重叠图

即散点图折线图在同一张图中,共享y轴

# coding:utf-8
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定中文字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号

x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [5, 6, 7, 8, 9]
fig = plt.figure(figsize=(15, 8), dpi=80)
ax1 = fig.add_subplot(111)
ax1.scatter(x, y1)
ax2 = ax1.twiny()  # 共享y轴
ax2.plot(x, y2, color="r")
ax1.set_title("标题")
ax1.set_xlabel("x轴")
ax1.set_ylabel("y轴")
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_52785473/article/details/123311855
今日推荐