plt.figure()的使用,plt.plot(),plt.subplot(),plt.subplots()和图中图
参考:https://blog.csdn.net/m0_37362454/article/details/81511427
matplotlib官方文档:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html
1.figure语法及操作
plt.figure()是新建一个画布。如果有多个图依次可视化的时候,需要使用,否则所有的图都显示在同一个画布中了。
使用plt.figure()的目的是创建一个figure对象。
整个图形被视为图形对象。当我们想调整图形的大小以及在一个图形中添加多个轴对象时,有必要显式地使用plt.figure()。
# in order to modify the size
fig = plt.figure(figsize=(12,8))
# adding multiple Axes objects
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
plt.figure()的必要性:
这并不总是必要的,因为在创建scatter绘图时,figure是隐式创建的;但是,在您所示的情况下,图形是使用plt.figure显式创建的,因此图形将是特定大小,而不是默认大小。
# Create scatter plot here
plt.gcf().set_size_inches(10, 8)
另一种选择是在创建scatter图之后使用gcf获取当前图形,并回顾性地设置图形大小:
(1)figure语法说明
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
(2)例子:
import matplotlib.pyplot as plt
创建自定义图像
fig=plt.figure(figsize=(4,3),facecolor='blue')
plt.show()
legend(loc # Location code string, or tuple (see below).
# 图例所有figure位置。 labels # 标签名称。
prop # the font property.
# 字体参数
fontsize # the font size (used only if prop is not specified).
# 字号大小。
markerscale # the relative size of legend markers vs.
# original 图例标记与原始标记的相对大小
markerfirst # If True (default), marker is to left of the label.
# 如果为True,则图例标记位于图例标签的左侧
numpoints # the number of points in the legend for line.
# 为线条图图例条目创建的标记点数
scatterpoints # the number of points in the legend for scatter plot.
# 为散点图图例条目创建的标记点数
scatteryoffsets # a list of yoffsets for scatter symbols in legend.
# 为散点图图例条目创建的标记的垂直偏移量
frameon # If True, draw the legend on a patch (frame).
# 控制是否应在图例周围绘制框架
fancybox # If True, draw the frame with a round fancybox.
# 控制是否应在构成图例背景的FancyBboxPatch周围启用圆边
shadow # If True, draw a shadow behind legend.
# 控制是否在图例后面画一个阴影
framealpha # Transparency of the frame.
# 控制图例框架的 Alpha 透明度
edgecolor # Frame edgecolor.
facecolor # Frame facecolor.
ncol # number of columns.
# 设置图例分为n列展示
borderpad # the fractional whitespace inside the legend border.
# 图例边框的内边距
labelspacing # the vertical space between the legend entries.
# 图例条目之间的垂直间距
handlelength # the length of the legend handles.
# 图例句柄的长度
handleheight # the height of the legend handles.
# 图例句柄的高度
handletextpad # the pad between the legend handle and text.
# 图例句柄和文本之间的间距
borderaxespad # the pad between the axes and legend border.
# 轴与图例边框之间的距离
columnspacing # the spacing between columns.
# 列间距
title # the legend title.
# 图例标题
bbox_to_anchor # the bbox that the legend will be anchored.
# 指定图例在轴的位置
bbox_transform) # the transform for the bbox.
# transAxes if None.
legend()
legend(loc, ncol, **)
可参考:matplotlib 的 legend 官网:https://matplotlib.org/users/legend_guide.html



2.subplot创建单个子图
(1) subplot语法
subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)
subplot可以规划figure划分为n个子图,但每条subplot命令只会创建一个子图 ,参考下面例子。
(2)例子
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#作图1
plt.subplot(221)
plt.plot(x, x)
#作图2
plt.subplot(222)
plt.plot(x, -x)
#作图3
plt.subplot(223)
plt.plot(x, x ** 2)
plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
plt.subplot(224)
plt.plot(x, np.log(x))
plt.show() 
3.subplots创建多个子图
(1)subplots语法
subplots参数与subplots相似
(2)例子
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#划分子图
fig,axes=plt.subplots(2,2)
ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]
#作图1
ax1.plot(x, x)
#作图2
ax2.plot(x, -x)
#作图3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
ax4.plot(x, np.log(x))
plt.show() 
4.面向对象API:add_subplots与add_axes新增子图或区域
add_subplot与add_axes都是面对象figure编程的,pyplot api中没有此命令
(1)add_subplot新增子图
add_subplot的参数与subplots的相似
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#新建figure对象
fig=plt.figure()
#新建子图1
ax1=fig.add_subplot(2,2,1)
ax1.plot(x, x)
#新建子图3
ax3=fig.add_subplot(2,2,3)
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#新建子图4
ax4=fig.add_subplot(2,2,4)
ax4.plot(x, np.log(x))
plt.show()
可以用来做一些子图。。。图中图。。。
(2)add_axes新增子区域
add_axes为新增子区域,该区域可以座落在figure内任意位置,且该区域可任意设置大小
add_axes参数可参考官方文档:http://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure
import numpy as np
import matplotlib.pyplot as plt
#新建figure
fig = plt.figure()
# 定义数据
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
#新建区域ax1
#figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# 获得绘制的句柄
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, 'r')
ax1.set_title('area1')

np.random.rand()返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1。
np.random.randn()返回一个或一组服从标准正态分布的随机样本值。
plt.legend()函数主要的作用就是给图加上图例,plt.legend([x,y,z])里面的参数使用的是list的的形式将图表的的名称喂给这个函数。
plt.legend()原文链接:https://blog.csdn.net/weixin_41950276/article/details/84259546
from matplotlib import pyplot as plt
import numpy as np train_x = np.linspace(-1, 1, 100)
train_y_1 = 2*train_x + np.random.rand(*train_x.shape)*0.3
train_y_2 = train_x**2+np.random.randn(*train_x.shape)*0.3 plt.scatter(train_x, train_y_1, c='red', marker='v' )
plt.scatter(train_x, train_y_2, c='blue', marker='o' )
plt.legend(["red","Blue"])
plt.show()

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np train_x = np.linspace(-1, 1, 100)
train_y_1 = 2*train_x + np.random.rand(*train_x.shape)*0.3
train_y_2 = train_x**2+np.random.randn(*train_x.shape)*0.3 plt.scatter(train_x, train_y_1, c='red', marker='v' )
plt.scatter(train_x, train_y_2, c='blue', marker='o' )
plt.legend(["red","Blue"])
plt.show()





plt.figure()的使用,plt.plot(),plt.subplot(),plt.subplots()和图中图的更多相关文章
- plt.figure()的使用
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/m0_37362454/article/d ...
- tensorflow_目标识别object_detection_api,RuntimeError: main thread is not in main loop,fig = plt.figure(frameon=False)_tkinter.TclError: no display name and no $DISPLAY environment variable
最近在使用目标识别api,但是报错了: File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/script_o ...
- subplot()一个窗口画多个图
import matplotlib.pyplot as plt plt.subplot(m,n,p) m,n表示一个窗口上显示m行n列 p表示正在处理第p个区域的部分(区域编号从左到右,从上到下) f ...
- python时间序列分析
题记:毕业一年多天天coding,好久没写paper了.在这动荡的日子里,也希望写点东西让自己静一静.恰好前段时间用python做了一点时间序列方面的东西,有一丁点心得体会想和大家 ...
- Subplot 多合一显示
1.均匀图中图 matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot. 使用import导入matplotlib.pyplot模块, 并简写成plt ...
- plot sin 动态配置rc settings
plot sin 动态配置rc settings 坐标轴颜色 线的颜色 绘图前景色 Code #!/usr/bin/env python # -*- coding: utf-8 -*- import ...
- matplotlib 的 subplot, axes and axis
fig = plt.figure('多图', (10, 10), dpi=80) #第一个指定窗口名称,第二个指定图片大小,创建一个figure对象 plt.subplot(222) #2*2的第二个 ...
- matplotlib subplot 多图合一
1:第一种方法 # method1: subplot2grid ################# ''' 第一个参数(3, 3) 是把图分成3行3列 第二个参数是位置 (0, 0)表示从0行0列开始 ...
- 4.8Python数据处理篇之Matplotlib系列(八)---Figure的学习
目录 目录 前言 (一)figure()方法的定义 (二)figure()方法的参数 (三)figure()方法的例子 1.多窗体绘图: 2.窗口得分别率 目录 前言 今天我们来学习一下plt.fig ...
随机推荐
- echarts中boundaryGap属性
boundaryGap:false boundaryGap:true 代码处: xAxis: { type: "category", data: ["06-01" ...
- 针对form表单赋值封装
1 (function ($){ 2 $.fn.extend({ 3 exajax:function(url,opts,convert){ 4 var ajaxParam = { 5 url:url, ...
- JavaEE期末复习知识点总结
JavaEE期末复习知识点总结 Java企业应用开发环境 Maven的基础概念 Maven是一个项目管理工具,可以对 Java 项目进行构建.依赖管理 Maven仓库 Maven 仓库是项目中依赖的第 ...
- python---从尾到头打印链表
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: # 返回从尾部到头部的列表值序 ...
- 帝国CMS后台采集关键字的方法
小伙伴们知道帝国CMS后台的采集功能是不能采集关键字的,那么老墨今天给大家说一个变通方法,让后台能采集关键字!方法如下: 1.系统设置--管理数据表--管理字段--增加字段(字段名:keywords字 ...
- 5.Docker容器学习之新手进阶使用
@ 原文地址:点击直达 学习参考:https://yeasy.gitbooks.io/docker_practice/repository/registry.html 0x00 前言简述 描述: 本章 ...
- 一致性Hash的原理与实现
应用场景 在了解一致性Hash之前,我们先了解一下一致性Hash适用于什么场景,能解决什么问题?这里先放一下我自己认为适用的场景.一致性Hash适用于服务器动态扩展且需要负载均衡的场景 试想以下场景, ...
- zabbix proxy cannot perform check now for itemid [xxxxx]: item is not in cache
情况 接上次做完容器部署proxy后,为其添加host进行添加任务. 发现一直没有数据,就到item里面执行 execute now. 然后过了几分钟回来一看,还是没有. Emmm,看下log吧. S ...
- BurpSuite与Xray多级代理实现联动扫描
Xray是长亭科技推出的,最经典的莫过于代理模式下的被动扫描,它使得整个过程可控且更加精细化: 代理模式下的基本架构为,扫描器作为中间人,首先原样转发流量,并返回服务器响应给浏览器等客户端,通讯两端都 ...
- 玩转LiteOS组件:玩转Librws
摘要:Librws是一个跨平台的websocket客户端,使用C语言编写. 本文分享自华为云社区<LiteOS组件尝鲜-玩转Librws>,作者: W922 . 本期小编为大家带来Lite ...