python matplotlib fig = plt.figure() fig.add_subplot()
一、matplotlib.pyplot.figure()
Create a new figure, or activate an existing figure.
matplotlib官网
功能: 创建一个新的图形 或激活一个已有的图形
**注意: 若不添加描述,默认图形描述为figure1; **
形如plt.figure(‘新的图形’)为图形添加自定义描述
(1)figure语法说明
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
num: 图像编号或名称,数字为编号 ,字符串为名称
figsize: 指定figure的宽和高,单位为英寸;如figsize=(5,4)
dpi: Dots Per Inch 指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 。 若设置dpi=20, 则画面会非常模糊。(1英寸等于2.5cm,A4纸是 21*30cm的纸张)
facecolor: 背景颜色
edgecolor: 边框颜色
frameon: 是否显示边框
二、在figure中添加子图
没有add_subscatter(), add_subplot()意为添加子图并设置子图在整个figure中的位置,至于图的类型设置为下一步
add_subplot(nrows,ncols,sharex,sharey,subplot_kw)
三、简单示例
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 从本地字体集中导入‘宋体’,以免出现汉字乱码的情况
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)
# 在"figure1"中创建一个带有散点图
# sin和cos 同时存在一个图中,使用label参数区分;alpha:RGBA空间中用于控制不透明度的值,范围为0-1;
plt.scatter(np.arange(0, 5, 0.1), np.sin(np.arange(0, 5, 0.1)), c = 'Pink', label = 'sin', alpha = 0.7)
plt.scatter(np.arange(0, 5, 0.1), np.cos(np.arange(0, 5, 0.1)), c = 'Pink', label = 'cos')
# 添加一个新的figure
fig = plt.figure('新的画布')
# 在新的figure中创建两行两列的四子图, 并且标注位置
fig.add_subplot(2,2,1)
fig.add_subplot(2,2,2)
fig.add_subplot(2,2,3)
fig.add_subplot(2,2,4)
plt.show()
四、为图添加网格
plt.grid(color, alpha, linestyle, linewidth)
# -------左下角
fig.add_subplot(2,2,3)
# 类型为折线图
plt.plot(x, pow(x,2))
plt.grid(color = 'red', alpha=0.3, linestyle='--', linewidth=1)
linestyle = ‘–’ (网格为虚线) ; linewidth=1(默认)
linestyle = ‘–’ (网格为实线) ; linewidth=1(默认)
linestyle = ‘–’ (网格为虚线) ; linewidth=2 (线的宽度)
五、复杂示例
x = np.arange(0,10,0.1)
fig = plt.figure('Fig1',figsize=(5,5))
# 左上角图
fig.add_subplot(2,2,1)
plt.scatter(x, np.cos(x), c = 'Black', label = 'cos')
# 右上角图
fig.add_subplot(2,2,2)
plt.plot(x, np.cos(x), c = 'Black', label = 'cos')
# 左下角
fig.add_subplot(2,2,3,)
plt.plot(x, pow(x,2))
plt.grid(color = 'red', alpha=0.3, linestyle='--', linewidth=2)# 右下角,设置子图背景色为粉色
fig.add_subplot(2,2,4, facecolor='pink')
plt.plot(x, pow(x,2))
六、add_subplot() & subplot()
只有使用plt.legend()才能为每条线生成一个用于区分的图例。(无论是否又label选项都要调用plt.legend 来生成图例,若只设置了label, 不调用plt.legend(),则图例不显示。类似于HTML中标签)
subplot()的官方描述:
Add an Axes to the current figure or retrieve an existing Axes.
This is a wrapper of Figure.add_subplot which provides additional behavior when working with the implicit API .
译作:plt.subplot()是Figure.add_subplot ()的封装器,用于隐式API。
二者作用相同,只是使用不同
import matplotlib.pyplot as plt
Figure = plt.figure()Figure.add_subplot()
# 等价于
plt.subplotFigure.set_xlabel()
# 同样等价于
plt.xlabel()Figure.set_ylabel()
# 同样等价于
plt.ylabel()
七、添加图例
一条legend只对一个图有作用,且只对图中出现的label 有注释,注释显示在图内
1、语法:
plt.legend(loc = ’ ', shadow = True / False, fontsize = 'x-large)
2、参数
loc = ‘upper right’ 右下角
官方示例
import numpy as np
import matplotlib.pyplot as plt# Make some fake data.
a = b = np.arange(0, 3, .02)
c = np.exp(a)
d = c[::-1]# Create plots with pre-defined labels.
fig, ax = plt.subplots()
# k 是blacK的意思, --表示虚线, : 表示点线
ax.plot(a, c, 'k--', label='Model length')
ax.plot(a, d, 'k:', label='Data length')
ax.plot(a, c + d, 'k', label='Total message length')legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')# Put a nicer background color on the legend.
legend.get_frame().set_facecolor('C1')plt.show()
示例
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties# font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)x = np.arange(0,10,0.1)
fig = plt.figure('Fig1',figsize=(10,10))
# 左上角图
fig.add_subplot(2,2,1)
plt.plot(x, np.cos(x), 'k:', label='cos')
plt.legend(loc='upper center', shadow=True, fontsize='x-large')# 右上角图
fig.add_subplot(2,2,2)
plt.plot(x, np.cos(x), 'k:', label='cos')
plt.legend(loc='upper right', shadow=True, fontsize='x-large')# 左下角
fig.add_subplot(2,2,3,)
plt.plot(x, np.power(x,2), 'k--', label='pow')
plt.grid(color = 'red', alpha=0.3, linestyle='--', linewidth=2)
plt.legend(loc='upper center', shadow=True, fontsize='x-large')# 右下角,设置子图背景色为粉色
fig.add_subplot(2,2,4, facecolor='pink')
plt.plot(x, pow(x,2), 'k--', label='pow')
plt.legend(loc='upper center', shadow=True, fontsize='x-large')# plt.ylabel('y',fontproperties=font_set)
# plt.title(u'混合图',fontproperties=font_set)
plt.show()
python matplotlib fig = plt.figure() fig.add_subplot()相关推荐
- 【python日用】plt.figure()绘图
figure语法 def figure(num=None, # autoincrement if None, else integer from 1-Nfigsize=None, # defaults ...
- python matplotlib.pyplot plt.ioff()函数(关闭交互模式用于阻塞程序,不让图片关闭)
感觉ioff()函数就是个全局设置的东东,启动它,则图形绘制到最后窗口不会关闭(检测到有新的绘制就会更新窗口内容),否则因为开启了ion()交互模式,窗口绘制完就默认关闭 def ioff():&qu ...
- Python matplotlib 通过 plt.scatter画空心圆标记出特定的点
在用python画散点图的时候想标记出特定的点,比如在某些点的外围加个空心圆,一样可以通过plt.scatter实现 import matplotlib.pyplot as pltx = [[1, 3 ...
- python matplotlib pyplot plt.axhline()函数(绘制平行于x轴的水平参考线)
函数功能:绘制平行于x轴的水平参考线调用签名:plt.axhline(y=0.0, c="r", ls="--", lw=2)y:水平参考线的出发点c:参考线的 ...
- python matplotlib.pyplot() plt() 如何限制坐标的上下限?plt.xlim() plt.ylim()
不设置上下限,曲线都跑出关注区域了... 设置前(5次拟合) 设置后(5次拟合): 参考文章:matplotlib 设定坐标的上下限:matplotlib.pyplot.xlim
- python matplotlib.figure.Figure.add_subplot()方法的使用
官方文档 https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=add_subplot#matplotl ...
- python matplotlib:figure,add_subplot,subplot,subplots讲解实现
最近又用到了matplotlib 中画图的函数.总结几个常用的函数的作用于区别. from matplotlib import pyplot as plt 1.figure() 函数定义matplot ...
- python matplotlib绘图大全(散点图、柱状图、饼图、极坐标图、热量图、三维图以及热图)...
//2019.7.14晚 matplotlib七种常见图像输出编程大全 七种图形汇总输出如下: import numpy as np #导入数据结构nmupy模块 import matplotlib. ...
- python matplotlib画图遇到的问题——画多个子图
画多个子图 两种方法: 一种 用fig.add_subplot(),但是这种貌似无法达到下面另外一种的效果 fig=plt.figure() ax1=fig.add_subplot(221) ax2= ...
最新文章
- 原创数据结构算法Flash动画演示课件-Action Script(AS)脚本实现
- 5.5.4 函数内部属性
- EonerCMS——做一个仿桌面系统的CMS(三)
- java Web监听器导图详解
- 爬虫学习2----Pyquery
- 105.输出控制缓存
- [转]UBB代码解析规范
- Python数据结构与算法视频教程-王宁宁-专题视频课程
- 软件过程管理期末复习资料
- oracle 2的22次方,22的2次方(2的22的2次方等于多少)
- QQ浏览器不能播放视频怎么办?要如何解决
- 写在清明(2007年)
- 三、外码、关系的完整性约束、关系代数
- “科林明伦杯”哈尔滨理工大学第十届部分题解
- qqwry 纯真IP数据小工具 nali
- 处理数据时,对数据取对数的意义
- Python3.6 车牌识别代码源码
- php龙之战争游戏搭建教程,沙漠副本龙之战争 快速杀龙王4条经验
- 如何从API中获取数据(一)
- 鸿蒙和鲲鹏有什么不同,鲲鹏、昇腾、鸿蒙、云……带你零距离接触不一样的湘江鲲鹏...