# coding: utf-8
# ## hist 直方图
#
# 首先要理清楚一个概念,直方图与条形图。
# 直方图与条形图的区别:
#
# 条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的;
# 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的频数或频率,宽度则表示各组的组距,因此其高度与宽度均有意义。
#
# 由于分组数据具有连续性,直方图的各矩形通常是连续排列,而条形图则是分开排列。
#
# 条形图主要用于展示分类数据,而直方图则主要用于展示数据型数据。
#
# #概率分布直方图
# #高斯分布
# #均值为0
# mean = 0
# #标准差为1,反应数据集中还是分散的值
# sigma = 1
# x=mean+sigma*np.random.randn(10000)
# fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6))
# #第二个参数是柱子宽一些还是窄一些,越大越窄越密
# ax0.hist(x,40,normed=1,histtype='bar',facecolor='yellowgreen',alpha=0.75)
# ##pdf概率分布图,一万个数落在某个区间内的数有多少个
# ax0.set_title('pdf')
# ax1.hist(x,20,normed=1,histtype='bar',facecolor='pink',alpha=0.75,cumulative=True,rwidth=0.8)
# #cdf累计概率函数,cumulative累计。比如需要统计小于5的数的概率
# ax1.set_title("cdf")
# fig.subplots_adjust(hspace=0.4)
# plt.show()
# In[3]:
import pandas as pd
import matplotlib.pyplot as plt
import os
get_ipython().magic('matplotlib inline')
# In[4]:
#直方图是用面积表示整体数据的分布,每一个矩形的高度表示每一组的频数或频率,宽度则表示各组的组距
# In[8]:
#os.chdir('../Matplotlib/Matplotlib学习/参考资料_数据集')
f = open("../参考资料_数据集/titanic.csv")
df = pd.read_csv(f)
df.head()
# In[12]:
# 查看年龄分布
age = df["Age"].dropna()
fig,ax = plt.subplots(figsize=(8,5),dpi=80)
ax.hist(age)
#ax.hist(age,bins=20) #指定bins,可以调整组数
#ax.hist(age,range=(10,60),bins=10) #指定区间
# 官方pyplot
# matplotlib.pyplot: https://matplotlib.org/api/pyplot_api.html
# ## 饼图
# In[14]:
#泰坦尼克号中的男女比例
df_sex = df["Sex"].value_counts()
df_sex
# In[16]:
#ax.pie?
# In[18]:
fig,ax = plt.subplots(figsize=(5,5),dpi=80)
ax.pie(df_sex.values,labels=df_sex.index,startangle=90,autopct="%.f%%",pctdistance=0.3)
# startangle表示起始角度,90代表第一块从90度的位置开始逆时针转
# autopct表示圆里面的文本格式,在python里%操作符可用于格式化字符串操作,
# %.f表示浮点数,点的左边数字大小表示这个数字要占有多少个字节,点的右边数字大小表示小数位数,两个%表示字符"%"
# pctdistance表示百分比数据距离圆心的位置
# In[19]:
print("%100.4f" % 3.5)
# ## 箱线图
# https://blog.csdn.net/roguesir/article/details/78249864
Matplotlib学习_直方图&饼图&箱线图
最新推荐文章于 2024-05-15 10:37:34 发布