Coo*_*eep 2 python visualization matplotlib data-analysis seaborn
我认为它们看起来都一样,但必须有所区别。
它们全部以一列作为输入,并且y轴具有所有图的计数。
这些绘图函数pyplot.hist,seaborn.countplot并且seaborn.displot都是辅助工具绘制一个单变量的频率。根据此变量的性质,它们可能或多或少适合于可视化。
可以对连续变量x进行直方图显示频率分布。
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)*100
hist, edges = np.histogram(x, bins=np.arange(0,101,10))
plt.bar(edges[:-1], hist, align="edge", ec="k", width=np.diff(edges))
plt.show()
Run Code Online (Sandbox Code Playgroud)
使用pyplot.hist或可以达到相同的目的seaborn.distplot。
plt.hist(x, bins=np.arange(0,101,10), ec="k")
Run Code Online (Sandbox Code Playgroud)
要么
sns.distplot(x, bins=np.arange(0,101,10), kde=False, hist_kws=dict(ec="k"))
Run Code Online (Sandbox Code Playgroud)
distplot包装pyplot.hist,但除其他功能外,还可以显示内核密度估计值。
对于离散变量,直方图可能适用也可能不合适。如果使用a numpy.histogram,则垃圾箱必须恰好在预期的离散观测值之间。
x1 = np.random.randint(1,11,100)
hist, edges = np.histogram(x1, bins=np.arange(1,12)-0.5)
plt.bar(edges[:-1], hist, align="edge", ec="k", width=np.diff(edges))
plt.xticks(np.arange(1,11))
Run Code Online (Sandbox Code Playgroud)
取而代之的是x,
u, counts = np.unique(x1, return_counts=True)
plt.bar(u, counts, align="center", ec="k", width=1)
plt.xticks(u)
Run Code Online (Sandbox Code Playgroud)
结果与上述相同。主要区别在于并非所有可能的观察都被占用的情况。说5甚至不是您的数据的一部分。直方图方法仍然可以显示它,尽管它不是唯一元素的一部分。
x2 = np.random.choice([1,2,3,4,6,7,8,9,10], size=100)
plt.subplot(1,2,1)
plt.title("histogram")
hist, edges = np.histogram(x2, bins=np.arange(1,12)-0.5)
plt.bar(edges[:-1], hist, align="edge", ec="k", width=np.diff(edges))
plt.xticks(np.arange(1,11))
plt.subplot(1,2,2)
plt.title("counts")
u, counts = np.unique(x2, return_counts=True)
plt.bar(u.astype(str), counts, align="center", ec="k", width=1)
Run Code Online (Sandbox Code Playgroud)
后者是做什么的seaborn.countplot。
sns.countplot(x2, color="C0")
Run Code Online (Sandbox Code Playgroud)
因此,它适用于离散或分类变量。
的所有功能pyplot.hist,seaborn.countplot并且seaborn.displot如果手动绘制例如柱状图被认为过于繁琐充当包装用于matplotlib柱状图,并且可以使用。
对于连续变量,可以使用pyplot.hist或seaborn.distplot。对于离散变量,a seaborn.countplot更方便。
| 归档时间: |
|
| 查看次数: |
2719 次 |
| 最近记录: |