我们在做数据分析的时候,难免会用到图像来表示你要展示的东西,接下来写一下demo来表示一下各种图:
以下默认所有的操作都先导入了numpy、pandas、matplotlib、seaborn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
数据源地址:github地址:https://github.com/mwaskom/seaborn-data
解压缩文件,拖入seaborn-data文件夹中
1、折线图
折线图可以用来表示数据随着时间变化的趋势
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]
- Matplotlib
plt.plot(x, y)
plt.show()
- Seaborn
df = pd.DataFrame({'x': x, 'y': y})
sns.lineplot(x="x", y="y", data=df)
plt.show()
2、直方图
直方图是比较常见的视图,它是把横坐标等分成了一定数量的小区间,然后在每个小区间内用矩形条(bars)展示该区间的数值
a = np.random.randn(100)
s = pd.Series(a)
- Matplotlib
plt.hist(s)
plt.show()
- Seaborn
sns.distplot(s, kde=False)
plt.show()
sns.distplot(s, kde=True)
plt.show()
3、垂直条形图
条形图可以帮我们查看类别的特征。在条形图中,长条形的长度表示类别的频数,宽度表示类别。
x = ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']
y = [5, 4, 8, 12, 7]
- Matplotlib
plt.bar(x, y)
plt.show()
<