条形图(垂直)绘制:关键字(bar)虽然饼图可以很好地表达离散型变量在各水平上的差异,但其不擅长对比差异不大或水平值过多的离散型变量,因为饼图是通过各扇形面积的大小来比价差异的,面
条形图(垂直)绘制:关键字(bar)
虽然饼图可以很好地表达离散型变量在各水平上的差异,但其不擅长对比差异不大或水平值过多的离散型变量,因为饼图是通过各扇形面积的大小来比价差异的,面积的比较有时并不直观;
对于条形图而言,对比的是柱形的高低,柱体越高,代表的数值越大,反之亦然;
bar(x, height, https://img2.php1.cn/3cdc5/3c66/9f3/18d891735a11b769.png" alt="" loading="lazy">
绘制图表的关键性代码: 1.指定条形图x轴的刻度值2.指定条形图y轴的数值
plt.bar(x = range(GDP.shape[0],height = GDP.GDP)
![](https://img2.php1.cn/3cdc5/3c66/9f3/c095081b69695a89.png)
3.指定条形图x轴的刻度标签tick_label = GDP.Province
![](https://img2.php1.cn/3cdc5/3c66/9f3/68360e926e83e970.png)
4.指定条形图填充色 (不重要) color = 'steelblue'
plt.bar(x = range(GDP.shape[0]), # 指定条形图x轴的刻度值
height = GDP.GDP, # 指定条形图y轴的数值
tick_label = GDP.Province# 指定条形图x轴的刻度标签
, color = 'steelblue'# 指定条形图的填充色
)
5.添加y轴标签
plt.ylabel('GDP(万亿)')
![](https://img2.php1.cn/3cdc5/3c66/9f3/24a31cb853a9f3fc.png)
6.添加标题plt.title('2017年度6个省份GDP分布')
![](https://img2.php1.cn/3cdc5/3c66/9f3/e447e32432e6ed83.png)
7.为每个图形添加数值标签
for x,y in enumerate(GDP.GDP):
plt.text(x,y+0.1,'%s' %round(y,1),ha='center')
![](https://img2.php1.cn/3cdc5/3c66/9f3/4189106d29d38df8.png)
条形图(水平)绘制:关键字(barh)
1.读取数据 并做升序排序
![](https://img2.php1.cn/3cdc5/3c66/9f3/6788aaf09ac90831.png)
2.绘制条形图基本形状
![](https://img2.php1.cn/3cdc5/3c66/9f3/01e03dbd72f2054a.png)
3.写入y轴城市数据 并指定颜色
![](https://img2.php1.cn/3cdc5/3c66/9f3/bbdc2a966f686d4f.png)
4.添加x轴标签
![](https://img2.php1.cn/3cdc5/3c66/9f3/cc47c52e614da7cb.png)
5.添加标题:
![](https://img2.php1.cn/3cdc5/3c66/9f3/4dc6656a069e6f87.png)
6.写入数据
条形图(交叉)绘制:关键字(bar)
1.读取数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/638c4250e1ee9376.png)
2.创建透视表
![](https://img2.php1.cn/3cdc5/3c66/9f3/15d1b36eea812cad.png)
3.对数据集降序排序
![](https://img2.php1.cn/3cdc5/3c66/9f3/2ad4756ee89e7bb1.png)
4.绘制基本图形
![](https://img2.php1.cn/3cdc5/3c66/9f3/41eba16584e236b4.png)
5.设置x轴标签角度以及颜色
![](https://img2.php1.cn/3cdc5/3c66/9f3/d19f33cb76f42308.png)
6.设置柱形宽度以及标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/948b62687b03843e.png)
7.添加y轴数据 并删除x轴标签数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/67b2655bb4cf65a1.png)
![](https://img2.php1.cn/3cdc5/3c66/9f3/56f2da6d5758920d.png)
直方图绘制:关键字(hist)
1.读入数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/f19cf5dfe92c80b3.png)
2.检查年龄是否有缺失 如果有缺失,将无法绘制直方图
![](https://img2.php1.cn/3cdc5/3c66/9f3/6053e8684b1939ea.png)
3.为缺失数据添加平均值
![](https://img2.php1.cn/3cdc5/3c66/9f3/a6cc224cf058ee2c.png)
4.绘制基本直方图
![](https://img2.php1.cn/3cdc5/3c66/9f3/506db587fdc36713.png)
5.添加填充色 边框色(不重要)
![](https://img2.php1.cn/3cdc5/3c66/9f3/d7bb0a2c1495d64e.png)
6.添加x轴标签和y轴标签以及标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/863a609646f3a472.png)
箱线图的绘制:关键字(boxplot)
参数说明:
六个参数解释:
1.最小值(下须值)
就是排在第一位的值
2.第一四分位数
第一四分位数(Q1)又称为“较小四分位数”,把一组值从小到大排列并分成四等分,处于三个分割点位置的得分就是四分位数,也就是样本所有数值从小到大排列后第25%的数字。
3.中位数
中位数(Q2),又称第二四分位数,等于该样本中所有数值由小到大排列后第50%的数字。
4.第三四分位数
第三四分位数(Q2),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。
5.四分位距
四分位距(InterQuartile Range,IQR) ,第三四分位数与第一四分位数的差距。
操作步骤:
1.读取数据:
![](https://img2.php1.cn/3cdc5/3c66/9f3/e128c09954abbcc8.png)
2.绘制基本箱线图:
![](https://img2.php1.cn/3cdc5/3c66/9f3/baea784a66c24b91.png)
3.以点的形式显示均值,自定义颜色填充盒型图,设置箱体属性
![](https://img2.php1.cn/3cdc5/3c66/9f3/50273a3df3505898.png)
4.设置异常点和均值点的属性
![](https://img2.php1.cn/3cdc5/3c66/9f3/e31b60e78be71758.png)
5.设置中位数属性,删除x轴中的1
![](https://img2.php1.cn/3cdc5/3c66/9f3/53133ba44945b64f.png)
6.添加标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/02c0fe03f4ce9685.png)
折线图绘制:关键字(plot)
1.读取数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/11a64b5d0644e5d1.png)
2.绘制基本折线图
![](https://img2.php1.cn/3cdc5/3c66/9f3/a9e7d5230d5c0302.png)
3.定义折线类型,宽度,颜色
![](https://img2.php1.cn/3cdc5/3c66/9f3/2bd202eb53f4dddd.png)
4.在图中添加圆点, 设置点的大小,点的颜色,以及点的边框色
![](https://img2.php1.cn/3cdc5/3c66/9f3/8c58d71ddcdbee2a.png)
5.获取图的坐标信息,设置日期显示格式
![](https://img2.php1.cn/3cdc5/3c66/9f3/351f01e939832bdd.png)
6.设置x轴每个刻度的间隔天数 并添加y轴标签以及标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/2d60c34b71ead160.png)
绘制两条折线图
![](https://img2.php1.cn/3cdc5/3c66/9f3/bfbc8294967c658c.png)
设置坐标信息并出图![](https://img2.php1.cn/3cdc5/3c66/9f3/4476b15877e06aef.png)
散点图的绘制:关键字scatter
1.读取数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/9619de5f0e5bd10e.png)
2.绘制散点图
![](https://img2.php1.cn/3cdc5/3c66/9f3/f4cf1cdd15e3f31d.png)
3.添加x轴和y轴的标签以及标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/f74cbef17246922e.png)
气泡图
气泡图与散点图大致一致,只需在绘制图表中加上s参数
![](https://img2.php1.cn/3cdc5/3c66/9f3/9683c337de164cf2.png)
热力图的绘制: 关键字(heatmap)
需要导入模块 seaborn 借助这个模块来实现热力图的绘制
1.读取数据
![](https://img2.php1.cn/3cdc5/3c66/9f3/49733cd2d60a2d26.png)
2.根据交易日期衍生出年份和月份字段
![](https://img2.php1.cn/3cdc5/3c66/9f3/41f45c5c2dd905b3.png)
3.统计每年各月份的销售总额 绘制出一张新表
![](https://img2.php1.cn/3cdc5/3c66/9f3/b057ff3d0c35eb92.png)
4.绘制热力图
![](https://img2.php1.cn/3cdc5/3c66/9f3/d52e6895755387d1.png)
5.添加标题
![](https://img2.php1.cn/3cdc5/3c66/9f3/91c73a4db2fe7b0f.png)
组合图绘制:
满足需求:
工作中往往会根据业务需求,将绘制的多个图形组合到一个大图框内,形成类似仪表板的效果
读取数据:
![](https://img2.php1.cn/3cdc5/3c66/9f3/c981cbb91914df3c.png)
完整代码:
# 读取数据
Prod_Trade = pd.read_excel(r'Prod_Trade.xlsx')
# 衍生出交易年份和月份字段
Prod_Trade['year'] = Prod_Trade.Date.dt.year
Prod_Trade['month'] = Prod_Trade.Date.dt.month
# 设置大图框的长和高
plt.figure(figsize = (15,6))
# 设置第一个子图的布局
ax1 = plt.subplot2grid(shape = (2,3), loc = (0,0))
# 统计2012年各订单等级的数量
Class_Counts = Prod_Trade.Order_Class[Prod_Trade.year == 2012].value_counts()
Class_Percent = Class_Counts/Class_Counts.sum()
# 绘制订单等级饼图
ax1.pie(x = Class_Percent.values, labels = Class_Percent.index, autopct = '%.1f%%')
# 添加标题
ax1.set_title('各等级订单比例')
# 设置第二个子图的布局
ax2 = plt.subplot2grid(shape = (2,3), loc = (0,2))
# 统计2012年每月销售额
Month_Sales = Prod_Trade[Prod_Trade.year == 2012].groupby(by = 'month').aggregate({'Sales':np.sum})
# 绘制销售额趋势图
Month_Sales.plot(title = '2012年各月销售趋势', ax = ax2, legend = False)
# 删除x轴标签
ax2.set_xlabel('')
# 设置第三个子图的布局
ax3 = plt.subplot2grid(shape = (2,3), loc = (0,1))
# 绘制各运输方式的成本箱线图
sns.boxplot(x = 'Transport', y = 'Trans_Cost', data = Prod_Trade, ax = ax3)
# 添加标题
ax3.set_title('各运输方式成本分布')
# 删除x轴标签
ax3.set_xlabel('')
# 修改y轴标签
ax3.set_ylabel('运输成本')
# 设置第四个子图的布局
ax4 = plt.subplot2grid(shape = (2,3), loc = (1,0))
# 2012年客单价分布直方图
sns.distplot(Prod_Trade.Sales[Prod_Trade.year == 2012], bins = 40, norm_hist = True, ax = ax4, hist_kws = {'color':'steelblue'}, kde_kws=({'linestyle':'--', 'color':'red'}))
# 添加标题
ax4.set_title('2012年客单价分布图')
# 修改x轴标签
ax4.set_xlabel('销售额')
# 设置第五个子图的布局
ax5=plt.subplot2grid(shape=(2,3),loc=(1,2))
Titanic = pd.read_csv('titanic_train.csv')
Titanic.dropna(subset=['Age'], inplace=True)
plt.hist(x = Titanic.Age, bins = 20, color = 'red',edgecolor = 'black' )
plt.xlabel('年龄')
plt.ylabel('频数')
plt.title('乘客年龄分布')
# 设置第六个子图的布局
ax6=plt.subplot2grid(shape=(2,3),loc=(1,1))
GDP = pd.read_excel(r'Province GDP 2017.xlsx')
GDP.sort_values(by = 'GDP',inplace = True)
plt.barh(y = range(GDP.shape[0]),
width = GDP.GDP,
tick_label = GDP.Province,
color = 'green',
)
plt.xlabel('GDP(万亿)')
plt.title('2017年度6个省份GDP分布')
for y,x in enumerate(GDP.GDP):
plt.text(x+0.1,y,'%s' % round(x,1),va='center')
# 调整子图之间的水平间距和高度间距
plt.subplots_adjust(hspace=0.6, wspace=0.3)
# 图形显示
plt.show()
![](https://img2.php1.cn/3cdc5/3c66/9f3/4701f0584b65d4f1.png)
可视化相关模块:
1.matplotlib
数据分析三剑客中的之一 也是使用最广泛的可视化模块
2.seaborn
![](https://img2.php1.cn/3cdc5/3c66/9f3/6c4ff741e600e35d.png)
网址:http://seaborn.pydata.org/
3.highcharts
网址:https://www.highcharts.com.cn/demo/highcharts
![](https://img2.php1.cn/3cdc5/3c66/9f3/ed42ccbb475d0f9b.png)
4.echarts
网址:https://echarts.apache.org/examples/zh/index.html
特点:可以通过python代码直接调用
![](https://img2.php1.cn/3cdc5/3c66/9f3/90d2b330bee33158.png)
5. ds.js
网址:https://observablehq.com/
![](https://img2.php1.cn/3cdc5/3c66/9f3/5ba119d3205b9845.png)