python笔记35_3:数据可视化之堆积柱状图

链接:https://pan.baidu.com/s/15jQhGzVV9zgYMWxstk-FwA
提取码:fb0c

# -*- coding: utf-8 -*-

#堆积柱形图的绘制方法:

#什么是堆积柱形图?
#即把不同维度的柱子堆叠到一根柱子上(之前是平行的几根柱子),请执行代码观察效果

import numpy
import pandas
import matplotlib
from matplotlib import pyplot as plt

font = {
        'family':'SimHei'
        }
matplotlib.rc('font',**font)

data = pandas.read_csv("D:/workspaces/python/pythonStudy/35.csv")


#得到手机品牌与通信品牌的交叉表:
result = data.pivot_table(
                          values='月消费(元)',
                          index='手机品牌',
                          columns='通信品牌',
                          aggfunc=numpy.sum
                          )
index = numpy.arange(len(result))
#我们有三个通信品牌,所以定义三个颜色:
minColor = (42/256,87/256,141/256,1/3) 
midColor = (42/256,87/256,141/256,2/3)
maxColor = (42/256,87/256,141/256,3/3)

result = result.sort_values(
                            by='神州行',
                            ascending=False
                            )
#使用排列的方式,把数据堆叠放好,即为多维条形图
#第一个柱子的绘制和前面一样:
plt.bar(
        index,result['神州行'],
        color=maxColor
        )
#开始绘制其他的柱子,差别在这里:
plt.bar(
        index,result['动感地带'],
        bottom=result['神州行'], #通过bottom来设置这个柱子距离底部的高度
        color=midColor
        )
plt.bar(
        index,result['全球通'],
        bottom=result['神州行']+result['动感地带'],
        color = minColor
        )
plt.xticks(index,result.index)
plt.legend(['神州行','动感地带','全球通'])
plt.show()

#双向柱形图的绘图案例,请看下一个源码文件

运行结果:
运行结果

猜你喜欢

转载自blog.csdn.net/aiyo92/article/details/88388284
今日推荐