效果如下:
引入需要的模块:
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)