我们先来看一个结果图

看到这个图,我个人的思路是

1 设置标题

import numpy as np
import matplotlib.pyplot as plt plt.title('Scores by group and gender')

2 x坐标的间隔设置和文字设置

N = 13
ind = np.arange(N) #[ 0 1 2 3 4 5 6 7 8 9 10 11 12]
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10', 'G11', 'G12', 'G13'))

3 y坐标的文字设置和间隔设置

plt.yticks(np.arange(0, 81, 20)) #0到81 间隔20
plt.ylabel('Scores')

4 开始绘制主题条形图

Bottom = (52, 49, 48, 47, 44, 43, 41, 41, 40, 38, 36, 31, 29)
Center = (38, 40, 45, 42, 48, 51, 53, 54, 57, 59, 57, 64, 62)
Top = (10, 11, 7, 11, 8, 6, 6, 5, 3, 3, 7, 5, 9) d = []
for i in range(0, len(Bottom)):
sum = Bottom[i] + Center[i]
d.append(sum) width = 0.35 # 设置条形图一个长条的宽度 p1 = plt.bar(ind, Bottom, width, color='blue')
p2 = plt.bar(ind, Center, width, bottom=Bottom,color='green') #在p1的基础上绘制,底部数据就是p1的数据
p3 = plt.bar(ind, Top, width, bottom=d,color='red') #在p1和p2的基础上绘制,底部数据就是p1和p2

5 设置legend区分三部分数据

plt.legend((p1[0], p2[0], p3[0]), ('Bottom', 'Center', 'Top'),loc = 3)  #loc=3 表示lower left 也就是底部最左

loc的设置参数

'best'         : 0, (only implemented for axes legends)(自适应方式)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,

6 绘制出图形

plt.show()

7 最终代码为:

import numpy as np
import matplotlib.pyplot as plt plt.title('Scores by group and gender') N = 13
ind = np.arange(N) #[ 0 1 2 3 4 5 6 7 8 9 10 11 12]
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10', 'G11', 'G12', 'G13')) plt.ylabel('Scores')
plt.yticks(np.arange(0, 81, 20)) Bottom = (52, 49, 48, 47, 44, 43, 41, 41, 40, 38, 36, 31, 29)
Center = (38, 40, 45, 42, 48, 51, 53, 54, 57, 59, 57, 64, 62)
Top = (10, 11, 7, 11, 8, 6, 6, 5, 3, 3, 7, 5, 9) d = []
for i in range(0, len(Bottom)):
sum = Bottom[i] + Center[i]
d.append(sum) width = 0.35 # 设置条形图一个长条的宽度
p1 = plt.bar(ind, Bottom, width, color='blue')
p2 = plt.bar(ind, Center, width, bottom=Bottom,color='green')
p3 = plt.bar(ind, Top, width, bottom=d,color='red') plt.legend((p1[0], p2[0], p3[0]), ('Bottom', 'Center', 'Top'),loc = 3) plt.show()

python之 matplotlib模块之绘制堆叠柱状图的更多相关文章

  1. Python使用matplotlib模块绘制多条折线图、散点图

    用matplotlib模块 #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:折线图.散点图测试 ''' import rando ...

  2. python之 matplotlib模块之基本三图形(直线,曲线,直方图,饼图)

    matplotlib模块是python中一个强大的绘图模块 安装 pip  install matplotlib 首先我们来画一个简单的图来感受它的神奇 import numpy as np impo ...

  3. 为python安装matplotlib模块

    matplotlib是python中强大的画图模块. 首先确保已经安装python,然后用pip来安装matplotlib模块. 进入到cmd窗口下,执行python -m pip install - ...

  4. Python中matplotlib模块解析

    用Matplotlib绘制二维图像的最简单方法是: 1.  导入模块 导入matplotlib的子模块 import matplotlib.pyplot as plt import numpy as ...

  5. Python之matplotlib模块安装

    numpy 1.下载安装 源代码 http://sourceforge.net/projects/numpy/files/NumPy/ 安装 python2.7 setup.py install 2. ...

  6. python: 使用matplotlib的pyplot绘制图表

    工作中需要观察数据的变化趋势,用python写了一段小程序来用显示简单图表,分享出来方便有同样需求的人,matplotlib是个很不错的库. #!encode=utf8 from matplotlib ...

  7. Python的matplotlib模块的使用-Github仓库

    import matplotlib.pyplot as plt import numpy as np import requests url='https://api.github.com/searc ...

  8. Python 使用matplotlib模块模拟掷骰子

    掷骰子 骰子类 # die.py 骰子类模块 from random import randint class Die(): """骰子类""&quo ...

  9. python导入matplotlib模块出错

    我的系统是linux mint.用新立得软件包安装了numpy和matplotlib.在导入matplotlib.pyplot时出错.说是没有python3-tk包. 于是就在shell中装了一下. ...

随机推荐

  1. Oracle 中 not exists (select 'X' ...) 的含义

    select a.col1,a.col2 from temp1 a where not exists (select 'X' from temp2 b where b.col2 = a.col1);s ...

  2. XML大作业

    XML大作业 共两题,均于实验上机完成 第一题:在xml文档中使用DTD 第二题:掌握使用xsl显示xml文件的基本方法 第一题: 一.实验目的 (1)通过本实验,使学生能够了解并掌握XML DTD的 ...

  3. [CF1045A] Last chance

    题目:Last chance 传送门:http://codeforces.com/contest/1045/problem/A 分析: 1)有$n$个敌方飞船,己方有$m$个武器,有三种类型. 2)$ ...

  4. HDU4089 Activation(概率DP+处理环迭代式子)

    题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后(概 ...

  5. E:\Postgresql\pgAdmin4_binaryPath

    e Path to the directory containing the EDB Advanced Server utility programs (pg_dump, pg_restore etc ...

  6. Mybatis 批量插入和更新小例

    SpringBoot配置Mybatis前文有博文,数据库mysql: package com.example.demo.biz.dto; public class User { private int ...

  7. delphi 按钮 2 行

    http://bbs.csdn.net/topics/390230792 回复于: 2015-06-01 21:11:02 最简单的办法:------------------------以下是转载的, ...

  8. git统计提交次数

    git log --since="Oct 27 9:16:10 2017 +0800"  --pretty=oneline | wc -l

  9. 让gitlab暴露node-exporter供外部prometheus使用

    花了两天部署了一套监控服务 prometheus+node-exporter+grafana,公司的gitlab服务器准备部署node-exporter的时候突然发现gitlab已经有了这些服务, 也 ...

  10. LOJ 2183 / SDOI2015 序列统计 (DP+矩阵快速幂)

    题面 传送门 分析 考虑容斥原理,用总的方案数-不含质数的方案数 设\(dp1[i][j]\)表示前i个数,和取模p为j的方案数, \(dp2[i][j]\)表示前i个数,和取模p为j的方案数,且所有 ...