可视化库-Matplotlib-饼图与布局(第四天)
1。 画出一个基本的饼图,通过plt.pie()
m = 51212
f = 40742 m_perc = m / (m+f)
f_perc = f / (m+f) colors = ['navy', 'lightcoral']
labels = ['Male', 'Famale'] plt.figure(figsize=(8, 8))
# autopct 表示的是使用百分号表示, explode=[0, 0.05]表示两个饼的间隔
paches, text, autotext = plt.pie([m_perc, f_perc], colors=colors, labels=labels, autopct='%1.1f%%', explode=[0, 0.05]) # 对文本添加尺寸大小 text表示外圈文本
for text in text + autotext:
text.set_fontsize(20)
# 饼图内部的字体颜色
for text in autotext:
text.set_color('white')
plt.show()

2. 设置子图布局,通过plt.subplot2grid((3, 3), (0, 0))
ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (1, 0))
ax3 = plt.subplot2grid((3, 3), (0, 2), rowspan=3)
ax4 = plt.subplot2grid((3, 3), (2, 0), colspan=2)
ax5 = plt.subplot2grid((3, 3), (0, 1), rowspan=2)
plt.show()

3. 在一个大图里面嵌套一个小图, 通过添加一个坐标系来完成 fig.add_axes([left, bottom, width, height])
x = np.linspace(0, 10, 1000)
y2 = np.sin(x**2)
y1 = x ** 2 fig, ax1 = plt.subplots()
left, bottom, width, height = [0.22, 0.45, 0.3, 0.35] ax2 = fig.add_axes([left, bottom, width, height]) ax1.plot(x, y1)
ax2.plot(x, y2)
plt.show()

4. insert_axes在第一个图的基础上加入第二个图,这里也做了对条形图的高度加上了文本注释
from mpl_toolkits.axes_grid1.inset_locator import inset_axes top10_arrivals_countries = ['CANADA','MEXICO','UNITED\nKINGDOM',\
'JAPAN','CHINA','GERMANY','SOUTH\nKOREA',\
'FRANCE','BRAZIL','AUSTRALIA']
top10_arrivals_values = [16.625687, 15.378026, 3.934508, 2.999718,\
2.618737, 1.769498, 1.628563, 1.419409,\
1.393710, 1.136974]
arrivals_countries = ['WESTERN\nEUROPE','ASIA','SOUTH\nAMERICA',\
'OCEANIA','CARIBBEAN','MIDDLE\nEAST',\
'CENTRAL\nAMERICA','EASTERN\nEUROPE','AFRICA']
arrivals_percent = [36.9,30.4,13.8,4.4,4.0,3.6,2.9,2.6,1.5] # 在每一个条形图上进行文本的添加
def add_text(resc):
for bar in resc:
height = bar.get_height()
# ax1.text 设置文本标签
ax1.text(bar.get_x() + bar.get_width()/2 , height+0.1, '%s'%height, fontsize=18, ha='center', va='bottom') # 条形图的绘制
fig, ax1 = plt.subplots(figsize=(20, 12))
vbar = ax1.bar(np.arange(len(top10_arrivals_values)), top10_arrivals_values)
plt.xticks(np.arange(len(top10_arrivals_values)), top10_arrivals_countries, fontsize=18)
# 在第一个坐标系的基础上构造第二个坐标系
ax2 = inset_axes(ax1, width=6, height=6, loc=5)
explode = (0.08, 0.08, 0.05, 0.05,0.05,0.05,0.05,0.05,0.05)
puaches, text, autotext = ax2.pie(arrivals_percent, explode=explode, labels=arrivals_countries, autopct='%1.1f%%')
for text in text + autotext:
text.set_fontsize(18) for text in autotext:
text.set_color('white')
# 去除边框的操作
for spines in ax1.spines.values():
spines.set_visible(False) add_text(vbar)
plt.show()

5. 通过一些三角形图案,画出一个猫的形状
import numpy as np
from matplotlib.patches import Circle, Wedge, Polygon, Ellipse
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt fig, ax = plt.subplots() patches = [] # Full and ring sectors drawn by Wedge((x,y),r,deg1,deg2)
leftstripe = Wedge((.46, .5), .15, 90,100) # Full sector by default
midstripe = Wedge((.5,.5), .15, 85,95)
rightstripe = Wedge((.54,.5), .15, 80,90)
lefteye = Wedge((.36, .46), .06, 0, 360, width=0.03) # Ring sector drawn when width <1
righteye = Wedge((.63, .46), .06, 0, 360, width=0.03)
nose = Wedge((.5, .32), .08, 75,105, width=0.03)
mouthleft = Wedge((.44, .4), .08, 240,320, width=0.01)
mouthright = Wedge((.56, .4), .08, 220,300, width=0.01)
patches += [leftstripe,midstripe,rightstripe,lefteye,righteye,nose,mouthleft,mouthright] # Circles
leftiris = Circle((.36,.46),0.04)
rightiris = Circle((.63,.46),0.04)
patches += [leftiris,rightiris] # Polygons drawn by passing coordinates of vertices
leftear = Polygon([[.2,.6],[.3,.8],[.4,.64]], True)
rightear = Polygon([[.6,.64],[.7,.8],[.8,.6]], True)
topleftwhisker = Polygon([[.01,.4],[.18,.38],[.17,.42]], True)
bottomleftwhisker = Polygon([[.01,.3],[.18,.32],[.2,.28]], True)
toprightwhisker = Polygon([[.99,.41],[.82,.39],[.82,.43]], True)
bottomrightwhisker = Polygon([[.99,.31],[.82,.33],[.81,.29]], True)
patches+=[leftear,rightear,topleftwhisker,bottomleftwhisker,toprightwhisker,bottomrightwhisker] body = Ellipse((0.5,-0.18),0.6,0.8)
patches.append(body) # Draw the patches
colors = 100*np.random.rand(len(patches)) # set random colors
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.array(colors))
ax.add_collection(p) # Show the figure
plt.show()

可视化库-Matplotlib-饼图与布局(第四天)的更多相关文章
- Python可视化库-Matplotlib使用总结
在做完数据分析后,有时候需要将分析结果一目了然地展示出来,此时便离不开Python可视化工具,Matplotlib是Python中的一个2D绘图工具,是另外一个绘图工具seaborn的基础包 先总结下 ...
- Python数据可视化库-Matplotlib(一)
今天我们来学习一下python的数据可视化库,Matplotlib,是一个Python的2D绘图库 通过这个库,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率图,条形图,错误图,散点图等等 废 ...
- python的数据可视化库 matplotlib 和 pyecharts
Matplotlib大家都很熟悉 不谈. ---------------------------------------------------------------------------- ...
- Python可视化库Matplotlib的使用
一.导入数据 import pandas as pd unrate = pd.read_csv('unrate.csv') unrate['DATE'] = pd.to_datetime(unrate ...
- 数据分析处理库pandas及可视化库Matplotlib
一.读取文件 1)读取文件内容 import pandas info = pandas.read_csv('1.csv',encoding='gbk') # 获取文件信息 print(info) pr ...
- Python数据可视化库-Matplotlib(二)
我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...
- python可视化库 Matplotlib 01 figure的详细用法
1.上一章绘制一幅最简单的图像,这一章介绍figure的详细用法,figure用于生成图像窗口的方法,并可以设置一些参数 2.先看此次生成的图像: 3.代码(代码中有详细的注释) # -*- enco ...
- python可视化库 Matplotlib 00 画制简单图像
1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...
- 机器学习-数据可视化神器matplotlib学习之路(四)
今天画一下3D图像,首先的另外引用一个包 from mpl_toolkits.mplot3d import Axes3D,接下来画一个球体,首先来看看球体的参数方程吧 (0≤θ≤2π,0≤φ≤π) 然 ...
随机推荐
- HDU 4540 线性DP
威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- 获得文件路径 _pgmptr, _makepath, _splitpath
#include <stdlib.h> #include <stdio.h> int main(void) { char path_buffer[_MAX_PATH]; cha ...
- History of programming language
1940之前 第一个编程语言比现代的计算机还早诞生.首先,这种语言是种编码(en:code). 于1801年发明的提花织布机(或称甲卡提花织布机,英文:en:Jacquard loom),运用打孔卡上 ...
- 关于MSSQL中IS NULL和IS NOT NULL的问题
在SQL语句中我们一般会避免写IS NULL和IS NOT NULL,因为这样优化器不会使用索引. 但经过一系列测试发现这句话并不完全对,因为有时候也会使用索引. 语句: select * from ...
- eclipse+PyDev遇到字符UTF-8的问题
今天配置eclipse+PyDev,在配置的时候出现了问题,如下: python and jpython require at least version 2.1 and iron python 2. ...
- SQL优化(3):使用explain了解SQL性能-part2
接上文,上文对type列用实例做了说明,本文对Extra列进行一些说明. Extra列 Using filesort 前文说,需要对所有的查询结果进行一次排序,例如当使用order by时.但是若查询 ...
- ubuntu 11.04 Gnome 恢复默认的任务栏面板
在Ubuntu(实际是GNOME) 中,桌面上默认的任务栏菜单面板是上下两栏,上面(Panel) 是系统菜单和通知区域下面是窗口存放切换区域,那么作为ubuntu用户来说,你总会使系统用起来更顺手更美 ...
- yaf 笔记(持续更新)
1.如果action不需要输出视图文件(如果ajax请求之类的),只需要在action里面加Yaf\DisPatcher::getInstance()->disableView(); 2.获取客 ...
- batchsize的选择
参考链接:http://www.zhihu.com/question/32673260 其中链接中提到,theano Python接口,支持RBM,DBN,MLP...等
- POJ3177 Redundant Paths【tarjan边双联通分量】
LINK 题目大意 给你一个有重边的无向图图,问你最少连接多少条边可以使得整个图双联通 思路 就是个边双的模板 注意判重边的时候只对父亲节点需要考虑 你就dfs的时候记录一下出现了多少条连向父亲的边就 ...