python之 matplotlib模块之绘制堆叠柱状图
我们先来看一个结果图

看到这个图,我个人的思路是
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模块之绘制堆叠柱状图的更多相关文章
- Python使用matplotlib模块绘制多条折线图、散点图
用matplotlib模块 #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:折线图.散点图测试 ''' import rando ...
- python之 matplotlib模块之基本三图形(直线,曲线,直方图,饼图)
matplotlib模块是python中一个强大的绘图模块 安装 pip install matplotlib 首先我们来画一个简单的图来感受它的神奇 import numpy as np impo ...
- 为python安装matplotlib模块
matplotlib是python中强大的画图模块. 首先确保已经安装python,然后用pip来安装matplotlib模块. 进入到cmd窗口下,执行python -m pip install - ...
- Python中matplotlib模块解析
用Matplotlib绘制二维图像的最简单方法是: 1. 导入模块 导入matplotlib的子模块 import matplotlib.pyplot as plt import numpy as ...
- Python之matplotlib模块安装
numpy 1.下载安装 源代码 http://sourceforge.net/projects/numpy/files/NumPy/ 安装 python2.7 setup.py install 2. ...
- python: 使用matplotlib的pyplot绘制图表
工作中需要观察数据的变化趋势,用python写了一段小程序来用显示简单图表,分享出来方便有同样需求的人,matplotlib是个很不错的库. #!encode=utf8 from matplotlib ...
- Python的matplotlib模块的使用-Github仓库
import matplotlib.pyplot as plt import numpy as np import requests url='https://api.github.com/searc ...
- Python 使用matplotlib模块模拟掷骰子
掷骰子 骰子类 # die.py 骰子类模块 from random import randint class Die(): """骰子类""&quo ...
- python导入matplotlib模块出错
我的系统是linux mint.用新立得软件包安装了numpy和matplotlib.在导入matplotlib.pyplot时出错.说是没有python3-tk包. 于是就在shell中装了一下. ...
随机推荐
- 对拍 & 随机数生成
用 Windows 批处理对拍: 1. 新建一个批处理(.bat),代码如下: :loop@echo off data_creator.exe force_solution.exe correct_s ...
- PADS 学习资料
PADS软件: PADS9.5_3in1.7z 链接: http://pan.baidu.com/s/1epO4Y 密码: zltl 打不开程序的博友,请看你的电脑有没有安装常用软件运行库 ...
- Oracle查询中文乱码
1.查询Oracle服务端字符集 SQL> select userenv('language') from dual ; USERENV('LANGUAGE') ---------------- ...
- tomcat 迁移到weblogic 问题
问题1: Caused by: java.lang.UnsupportedClassVersionError: com/audaque/datadiscovery/soap/service/impl/ ...
- ag-grid 表格中添加图片
ag-grid是一种非常好用的表格,网上搜索会有各种各样的基本用法,不过对于在ag-grid 表格中添加图片我没有找到方法,看了官方的文档,当然英文的自己也是靠网页翻译,最后发现有这么一个例子,我知道 ...
- Kubernetes tutorial - K8S 官方入门教程 中文翻译
官方教程,共 6 个小节.每一小节的第一部分是知识讲解,第二部分是在线测试环境的入口. kubectl 的命令手册 原文地址 1 创建集群 1.1 使用 Minikube 创建集群 Kubernete ...
- Vue过渡:JavaScript钩子
一 App.vue <template> <div id="app"> <button @click="show = !show" ...
- TensorFlow学习笔记3-从MNIST开始
TensorFlow学习笔记3-从MNIST开始学习softmax 本笔记内容为"从MNIST学习softmax regression算法的实现". 注意:由于我学习机器学习及之前 ...
- oracle三大范式
范式: 设计数据库定义的一个规则, 三大范式, 灵活运用, 人的思想是活的 一范式 1, 不存在冗余数据 同一个表中的记录不能有重复----所以主键(必须有) 2, 每个字段必须是不可再分的信息(列不 ...
- jmeter 把返回数据写到文件
jmeter如何把返回数据写入到文件 作者:WhoisTester 2015-10-20 20:11 1. 首先我们可以使用 regular expression extractor 正则表达式 ...