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-饼图与布局(第四天)的更多相关文章

  1. Python可视化库-Matplotlib使用总结

    在做完数据分析后,有时候需要将分析结果一目了然地展示出来,此时便离不开Python可视化工具,Matplotlib是Python中的一个2D绘图工具,是另外一个绘图工具seaborn的基础包 先总结下 ...

  2. Python数据可视化库-Matplotlib(一)

    今天我们来学习一下python的数据可视化库,Matplotlib,是一个Python的2D绘图库 通过这个库,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率图,条形图,错误图,散点图等等 废 ...

  3. python的数据可视化库 matplotlib 和 pyecharts

    Matplotlib大家都很熟悉    不谈. ---------------------------------------------------------------------------- ...

  4. Python可视化库Matplotlib的使用

    一.导入数据 import pandas as pd unrate = pd.read_csv('unrate.csv') unrate['DATE'] = pd.to_datetime(unrate ...

  5. 数据分析处理库pandas及可视化库Matplotlib

    一.读取文件 1)读取文件内容 import pandas info = pandas.read_csv('1.csv',encoding='gbk') # 获取文件信息 print(info) pr ...

  6. Python数据可视化库-Matplotlib(二)

    我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...

  7. python可视化库 Matplotlib 01 figure的详细用法

    1.上一章绘制一幅最简单的图像,这一章介绍figure的详细用法,figure用于生成图像窗口的方法,并可以设置一些参数 2.先看此次生成的图像: 3.代码(代码中有详细的注释) # -*- enco ...

  8. python可视化库 Matplotlib 00 画制简单图像

    1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...

  9. 机器学习-数据可视化神器matplotlib学习之路(四)

    今天画一下3D图像,首先的另外引用一个包 from mpl_toolkits.mplot3d import Axes3D,接下来画一个球体,首先来看看球体的参数方程吧 (0≤θ≤2π,0≤φ≤π) 然 ...

随机推荐

  1. LeetCode 48

    这种方法首先对原数组取其转置矩阵,然后把每行的数字翻转可得到结果,如下所示(其中蓝色数字表示翻转轴): 1  2  3  1  4  7  7  4  1 4  5  6 -->  2  5   ...

  2. HTML <select> 标签

    定义和用法 select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中的可用选项. HTML 4.01 与 HTML ...

  3. 本地RUN Page时报无法显示该网页

    经检查,是我本地安装了浏览器广告屏蔽插件引起的,关闭该插件即可.

  4. MyBatis Generator配置文件context元素的defaultModelType属性

    MyBatis Generator配置文件context元素的defaultModelType属性 MyBatis Generator配置文件context元素有一个defaultModelType属 ...

  5. IOS-线程(GCD)

    一.GCD的使用 // // IBController3.m // IBCoder1 // // Created by Bowen on 2018/4/25. // Copyright © 2018年 ...

  6. phpstudy2017版本的nginx 支持laravel 5.X配置

    之前做开发和学习一直用phpstudy的mysql服务,确实很方便,开箱即用.QQ群交流:697028234 现在分享一下最新版本的phpstudy2017 laravel环境配置. 最新版的phps ...

  7. 论integer是地址传递还是值传递(转)

    原文链接:http://blog.csdn.net/witsmakemen/article/details/46874717 论integer是地址传递还是值传递 Integer 作为传参的时候是地址 ...

  8. Virtualbox安装Windows 8.1遇到0x000000C4错误解决办法 - 转

    想要尝试一下 Windows 8.1 系统,又不愿意在电脑上直接安装,虚拟机提供了很好的平台.因为平时工作需要,其实电脑上装的虚拟机还是不少的,每天都要开着几个虚拟机一起用.多一个不多,于是尝试在自己 ...

  9. New Concept English Two 27 73

    新概念一:对应小学水平,重在口语和基础.是不可多得的学习教材,全本144课,可以给孩子(hello world 级别的我)学半年.新概念二:对应高中水平,重在听力和场景对话,共96课,学后,听懂歪果仁 ...

  10. Windows下MySQL多实例运行

    1.正常安装Windows版的MySQL,例如安装在C:\Program Files\MySQL文件夹里: 2.按照常规配置好MySQL: (注:5.6版本的 data文件夹与 my.ini文件在C: ...