栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

matplotlib.pyplot 绘制图中图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib.pyplot 绘制图中图

效果如下:

引入需要的模块:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np
import math

 生成仿真数据:

def generate():
    x0,y0,y1=[],[],[]
    for i in np.arange(0,10,0.01):
        x0.append(i)
        y0.append(math.sin(i))
        y1.append(math.cos(i))
    return x0,y0,y1

绘图:

def plot_figure(x,y0,y1):
    fig=plt.figure()
    ax1=fig.add_subplot(1,1,1)
    ax1.plot(x,y0,color='r',label='sin')
    ax1.legend(loc='upper left')
    
    axins=inset_axes(ax1,width=1.5,height=1.5,loc='center right')
    axins.scatter(x,y1,color='g',label='cos')
    axins.legend(loc='upper right')
    plt.show()

完整代码如下:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np
import math
def generate():
    x0,y0,y1=[],[],[]
    for i in np.arange(0,10,0.01):
        x0.append(i)
        y0.append(math.sin(i))
        y1.append(math.cos(i))
    return x0,y0,y1
def plot_figure(x,y0,y1):
    fig=plt.figure()
    ax1=fig.add_subplot(1,1,1)
    ax1.plot(x,y0,color='r',label='sin')
    ax1.legend(loc='upper left')
    
    axins=inset_axes(ax1,width=1.5,height=1.5,loc='center right')
    axins.scatter(x,y1,color='g',label='cos')
    axins.legend(loc='upper right')
    plt.show()
if __name__=='__main__':
    x,y0,y1=generate()
    plot_figure(x,y0,y1)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号