直方图:
实验:
实验数据集用的是Iris数据集,鸢尾花数据集代表3种鸢尾花(Setosa,Versicolour和Virginica),具有4个属性:萼片长度,萼片宽度,花瓣长度和花瓣宽度。
# Import numpy
import numpy as np
import matplotlib.pyplot as plt
# Compute number of data points: n_data
n_data = len(versicolor_petal_length)
# Number of bins is the square root of number of data points: n_bins
n_bins = np.sqrt(n_data)
# Convert number of bins to integer: n_bins
n_bins= int(n_bins)
# Plot the histogram
plt.hist(versicolor_petal_length,bins=n_bins)
# Label axes
_ = plt.xlabel('petal length (cm)')
_ = plt.ylabel('count')
# Show histogram
plt.show()
bins:指定直方图条形的个数
10个bin,是matplotlib的默认设置。
“平方根规则”是用于选择箱数的常用经验法则:指定直方图条形的个数=样本数的平方根。
热力图:
# Create bee swarm plot with Seaborn's default settings
species = df['species']
petal = df['petal length (cm)']
print(petal)
_ = sns.swarmplot(x='species',y='petal length (cm)',data=df)
# Label the axes
_ = plt.xlabel('species')
_ = plt.ylabel("petal lengths")
# Show the plot
plt.show()
绘制所有数据:经验分布函数
Empirical cumulative distribution function (ECDF)
对被测变量的某个值而言,该值的分布函数值表示所有观测样本中小于或等于该值的样本所占的比例。
频数近似概率,样本推断总体
经典统计推断主要的思想就是用样本来推断总体的状态,因为总体是未知的,我们只能通过多次试验的样本(即实际值)来推断总体。经验分布函数是在这一思想下的一种方法,通过样本分布函数来估计总体的分布函数。
参考资料:
实验:
构建ecdf函数
def ecdf(data):
"""Compute ECDF for a one-dimensional array of measurements."""
# Number of data points: n
n = len(np.sort(data))
# x-data for the ECDF: x
x = np.sort(data)
# y-data for the ECDF: y
y = np.arange(1, n+1) / n
return x, y
使用ecdf()
函数来计算鸢尾花的花瓣长度的ECDF 。x轴为不同的样本值,y轴为频率
# Compute ECDF for versicolor data: x_vers, y_vers
x_vers, y_vers = ecdf(versicolor_petal_length)
# Generate plot
_ = plt.plot(x_vers,y_vers,marker='.',linestyle='none')
# Label the axes
_ = plt.xlabel('percent of versicolor petal length')
_ = plt.ylabel('ECDF')
plt.margins(0.02)
# Display the plot
plt.show()
ECDF还允许比较两个或多个分布(尽管如果分布太多,则图将变得混乱)。
为所有三种虹膜种类的花瓣长度绘制ECDF:
# Compute ECDFs
x_vers1,y_vers1 = ecdf(setosa_petal_length)
x_vers2,y_vers2 = ecdf(versicolor_petal_length)
x_vers3,y_vers3 = ecdf(virginica_petal_length)
plt.plot(x_vers1,y_vers1,marker='.',linestyle='none')
plt.plot(x_vers2,y_vers2,marker='.',linestyle='none')
plt.plot(x_vers3,y_vers3,marker='.',linestyle='none')
# Plot all ECDFs on the same plot
_ = plt.xlabel('percent of petal length')
_ = plt.ylabel('ECDF')
plt.margins(0.02)
# Annotate the plot
plt.legend(('setosa', 'versicolor', 'virginica'), loc='lower right')
_ = plt.xlabel('petal length (cm)')
_ = plt.ylabel('ECDF')
# Display the plot
plt.show()
一些思考:
1.如何选择图表?
下面摘自这里:https://zhuanlan.zhihu.com/p/67106690
① 按应用场景选择
② 按数据关系选择
3.一些常见问题:
Q: 柱形图和条形图都可以表示分类比较,那两者在使用上有何差异呢?
A: 当所比较项目的标签文本比较长时,柱形图的横轴下的标签会出现重叠或者倾斜,且占用空间大,影响阅读者的目光移动。所以在表示分类时,如项目数量较少,使用柱形图或条形图均可,如项目_数量较多_,则建议使用_条形图_。
Q: 柱形图和折线图都可以表示时间序列的趋势,如何选择?
A: 一般来说,建议使用折线图反映趋势变化。柱形图强调各数据点值之间的差异,折线图则强调起伏变化的趋势;柱形图更适于表现离散型的时间序列,_折线图适合表现连续型的时间序列_。所以当时间序列的_数据点较少时,可以使用柱形图_,而当数据点较多时,则建议使用折线图。
Q: 面积图和折线图都可以表示时间序列的趋势,两者之间如何选择?
A: 当只展示一个度量数据的趋势时,两者完全等价,都可以使用,通常使用折线图更多。
但是,当在大型会议室展示数据时,即读图人离图表可能较远的情况,使用面积图能让后排的人看的更清楚。当比较多个度量数据的趋势时,建议使用折线图。如使用面积图,则存在数据序列之间相互遮挡的情况,除了靠近横轴的那个数据序列外,很难观察出其他数据序列的变化趋势。
Q: 是不是应该避免使用饼图,能不用就不用?
A: 从精确比较数据的角度来说,条形图的确更易于比较数据点之间的差异,但每种图表都有它的长处和适用场景,饼图能给我们一种整体和构成的印象,适用于表达“占比”——看到饼图就让想起100%,这个特点是条形图所没有的。
其他参考资料:
33种经典图表类型总结,轻松玩转数据可视化https://www.jianshu.com/p/28c4b43c396d
活用这23种图表,让你的数据分析胜人一筹 — 推荐收藏https://flashgene.com/archives/41233.html
数据可视化图表,你选对了吗?https://zhuanlan.zhihu.com/p/67106690
数据分析图的十大错误,你占了几个?http://www.woshipm.com/data-analysis/794756.html
4. matplotlib.pyplot.plot()调用参数
plot函数的一般的调用形式:
#单条线:
plot([x], y, [fmt], data=None, **kwargs)
#多条线一起画
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
可选参数[fmt] 是一个字符串来定义图的基本属性如:颜色(color),点型(marker),线型(linestyle),具体形式 fmt = '[color][marker][line]',fmt接收的是每个属性的单个字母缩写