目录
柱形图:一种长方形的单位长度,根据数据大小回绘制的统计图,用来比较两个或以上的数据(时间或类别)
一、绘图函数
bar(left,right,width,color,bottom)
barh(bottom,width,height,color)
参数说明:
left:x轴的位置序列(一般用arrange产生一个序列)
height:y轴的数据序列
width:柱形图的宽度
color:柱形图填充颜色
bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)
# 绘制柱形图
# left:柱形图的x坐标
# height柱形图的高度,以0.0为基准
# width:柱形图的宽度,默认0.8
# facecolor:颜色
# edgecolor:边框颜色n
# bottom:表示底部从y轴的哪个刻度开始画
# yerr:应该是对应的数据的误差范围,加上这个参数,柱状图头部会有一个蓝色的范围标识,标出允许的误差范围,在水平柱状图中这个参数为xerr
二、案例
第一、单个柱形图
import pandas
import numpy
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# %matplotlib qt
# 设置不在交互命令行绘图,在弹出新的窗口进行绘图
data=pandas.read_csv(
'D:\\DATA\\pycase\\6.3\\data.csv'
)
# 根据通信品牌进行分组,去掉索引
result=data.groupby(
by=['手机品牌'],
as_index=False
)['月消费(元)'].agg({
'月消费':numpy.sum
})
# 字体设置
fontprop=font_manager.FontProperties(
fname='C:\\windows\\Fonts\\msyh.ttF' )
font={
'family':fontprop.get_name(),
'size':10
}
matplotlib.rc('font',**font)
# 绘制柱状图
# 定义柱状图的序列,从数据框中用size函数获取
index=numpy.arange(
result.月消费.size
)
plt.bar(index,result['月消费'])
# 优化点1,配置颜色
maincolor=(1,0,0,1)
plt.bar(
index,
result['月消费'],
color=maincolor
)
# 配置x周刻度
plt.xticks(index,result.手机品牌)
# 优化点 3,对数据排序后再进行绘图
sgb=result.sort_values(
by='月消费',
ascending=False
)
plt.bar(
index,
sgb.月消费,
color=maincolor
)
plt.xticks(index,sgb.手机品牌)
# 横向柱形图
plt.barh(
index,
sgb.月消费,
color=(0,0,1,1)
)
plt.yticks(index,sgb.手机品牌)
第二个、多个柱形图
import pandas
import numpy
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# %matplotlib qt
# 设置不在交互命令行绘图,在弹出新的窗口进行绘图
data=pandas.read_csv(
'D:\\DATA\\pycase\\6.3\\data.csv'
)
# 根据通信品牌进行分组,去掉索引
result=data.pivot_table(
values='月消费(元)',
index='手机品牌',
columns='通信品牌',
aggfunc=numpy.sum
)
# 字体设置
fontprop=font_manager.FontProperties(
fname='C:\\windows\\Fonts\\msyh.ttF' )
font={
'family':fontprop.get_name(),
'size':10
}
matplotlib.rc('font',**font)
# 绘制柱状图
# 定义柱状图的序列,从数据框中用size函数获取
index=numpy.arange(len(result))
# 多种颜色区分
minColor=(0.2,0.4,0.7,0.3)
midColor=(0.2,0.4,0.7,0.6)
maxColor=(0.2,0.4,0.7,0.9)
# 对数据框中的内容进行排序
result=result.sort_values(
by="神州行",
ascending=False
)
# 绘制多维数组条形图,把数组排列放好,即为多为条形图
# 通过INdex和width,进行设置柱形图的横坐标
plt.bar(
index,
result['神州行'],
color=minColor,
width=1/4
)
plt.bar(
index+1/4,
result['动感地带'],
color=midColor,
width=1/4
)
plt.bar(
index+2/4,
result['全球通'],
color=maxColor,
width=1/4
)
# 设置x坐标轴以及标签
plt.xticks(index+1/3,result.index)
# 标注类别标签,顺序对应,上边的颜色表示
plt.legend(['全球通','动感地带','神州行'])
第三、堆积柱形图
import pandas
import numpy
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# %matplotlib qt
# 设置不在交互命令行绘图,在弹出新的窗口进行绘图
data=pandas.read_csv(
'D:\\DATA\\pycase\\6.3\\data.csv'
)
# 根据通信品牌进行分组,去掉索引
result=data.pivot_table(
values='月消费(元)',
index='手机品牌',
columns='通信品牌',
aggfunc=numpy.sum
)
# 字体设置
fontprop=font_manager.FontProperties(
fname='C:\\windows\\Fonts\\msyh.ttF' )
font={
'family':fontprop.get_name(),
'size':10
}
matplotlib.rc('font',**font)
# 绘制柱状图
# 定义柱状图的序列,从数据框中用size函数获取
index=numpy.arange(len(result))
# 多种颜色区分
minColor=(0,0,0.7,1)
midColor=(0.7,0,0,1)
maxColor=(0,0.7,0,1)
# 对数据框中的内容进行排序
result=result.sort_values(
by="神州行",
ascending=False
)
# 使用排列的方式,把数据堆叠放好
plt.bar(
index,
result['神州行'],
bottom=0,
color=maxColor
)
plt.bar(
index,
result['动感地带'],
bottom=result['神州行'],
color=midColor
)
plt.bar(
index,
result['全球通'],
bottom=result['动感地带']+result['神州行'],
color=minColor
)
# 设置x坐标轴以及标签
plt.xticks(index+1/3,result.index)
# 标注类别标签,顺序对应,上边的颜色表示
plt.legend(['全球通','动感地带','神州行'])
第四、双向柱形图
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 6 17:49:00 2018
@author: admin
"""
import pandas
import numpy
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# %matplotlib qt
# 设置不在交互命令行绘图,在弹出新的窗口进行绘图
data=pandas.read_csv(
'D:\\DATA\\pycase\\6.3\\data.csv'
)
# 根据通信品牌进行分组,去掉索引
result=data.pivot_table(
values='月消费(元)',
index='手机品牌',
columns='通信品牌',
aggfunc=numpy.sum
)
# 字体设置
fontprop=font_manager.FontProperties(
fname='C:\\windows\\Fonts\\msyh.ttF' )
font={
'family':fontprop.get_name(),
'size':10
}
matplotlib.rc('font',**font)
# 绘制柱状图
# 定义柱状图的序列,从数据框中用size函数获取
index=numpy.arange(len(result))
# 多种颜色区分
minColor=(0,0,0.7,1)
midColor=(0.7,0,0,1)
maxColor=(0,0.7,0,1)
# 对数据框中的内容进行排序
result=result.sort_values(
by="神州行",
ascending=False
)
# 使用排列的方式,barh设置双向柱形图
plt.barh(
index,
result['动感地带'],
color=minColor
)
plt.barh(
index,
-result['神州行'],
color=maxColor
)
# 设置x坐标轴以及标签
plt.yticks(index,result.index)
# 标注类别标签,顺序对应,上边的颜色表示
plt.legend(['全球通','动感地带','神州行'])