可以使用matplotib库中的imshow函数对图像进行显示,同时使用xim和yim函数对图像的坐标轴进行限制,实现局部放大的效果。具体实现步骤可以参考以下代码:
# coding=UTF-8 import cv2 from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset import numpy as np import matplotlib.pyplot as plt from matplotlib import pyplot as plt from scipy.stats import norm from scipy.stats.distributions import chi2 from scipy.stats import ncx2 import math import pylab from matplotlib.patches import ConnectionPatch import numpy as np import pylab as pl # 为了画阴影图准备 x = np.arange(0,609014,1) # 6016是数据的个数 # plotting the lines y1 = 80 # 两条y轴线之间区域阴影的确认 y2 = -10 a = 1*x # x轴区域阴影的确认 # RMS def get_rms(records): return math.sqrt(sum([x ** 2 for x in records]) / len(records)) #画正方形连线 def draw_square(points): #:param points: 四个点的坐标,例如: [(x1, y1), (x2, y2), (x3, y3), (x4, y4)] pl.plot([point[0] for point in points], [point[1] for point in points], color='blue') def draw_square(points): # 假设points是一个包含四个点的列表,每个点是一个包含两个元素的列表或元组,例如: [(x1, y1), (x2, y2), (x3, y3), (x4, y4)] pylab.plot([point[0] for point in points], [point[1] for point in points], color='blue') pylab.plot([point[0] for point in points + [points[0]]], [point[1] for point in points + [points[0]]], color='blue') #从.txt中读取数据 的函数 def Position_error(fileName): inFile = open(fileName, 'r')#以只读方式打开某filename文件 #定义2个空的list,用来存放文件中的数据 t = [] diff_x = [] diff_y = [] diff_z = [] sum_xy = [] for line in inFile: trainingSet = line.split(',') t.append(float(trainingSet[0])-257181.0) diff_x.append((float(trainingSet[1]))) diff_y.append((float(trainingSet[2]))) diff_z.append((float(trainingSet[3]))) sum_xy.append(float(trainingSet[1]) + float(trainingSet[2]) ) return (t, diff_x, diff_y, diff_z,sum_xy) def PL_gnssinsmhc(fileName): inFile = open(fileName, 'r')#以只读方式打开某filename文件 #定义2个空的list,用来存放文件中的数据 t = [] PL = [] AL = [] for line in inFile: trainingSet = line.split(',') t.append(float(trainingSet[0])-257181.0) PL.append((float(trainingSet[1]))) AL.append((float(trainingSet[2]))) return (t, PL, AL) (t_e,diff_x,diff_y,diff_z,sum_xy) = Position_error('P_diff_enu_gnsssins_noF_noFDE.txt') (t, PL, AL) = PL_gnssinsmhc('PL_gnsssins_noF_noFDE.txt') plt.figure(figsize=(10,6.5)) pylab.plot(t_e, sum_xy,linestyle='-',label='PE', color="black",linewidth=1.5) pylab.plot(t, PL,linestyle='-',label='PL', color="blue",linewidth=1.5) pylab.plot(t, AL,linestyle='-',label='AL', color="red",linewidth=1.5) pylab.legend(loc='best',fontsize=20) # pylab.legend 放在 lot后面才能显示label, 并且设置标签的字体大小 pylab.ylabel("PL-PE-AL",fontsize=20,labelpad = 10.0) pylab.xticks(fontsize=20) # 设置【坐标】标签字体大小 pylab.yticks(fontsize=20) #pylab.xticks([]) # 不显示 X轴 #pylab.title('Pition error (m)',fontsize=20) #pylab.ylim([-5,20]) # y轴数据长度设定 # 四个点的坐标 points = [(0, 0), (1000, 0), (1000, 10), (0, 10)] # 定义四个点的坐标 # 调用函数画出正方形 draw_square(points) # 使用 annotate 绘制带箭头的线 pylab.annotate('', xy=(1163, 37.5), xytext=(1600, 13), arrowprops=dict(facecolor='yellow', shrink=0.05, width=2, headwidth=6)) # 创建放大区域 5 axins = zoomed_inset_axes(plt.gca(), zoom=6, loc='upper center') # 放大2倍,并放置在原图的右上角 ; 如 upper right,lower right,right,center,upper center,lower left,center right,lower center,upper left,center left axins.plot(t_e, sum_xy,color="black",linewidth=1.5) axins.plot(t, PL, color="blue",linewidth=1.5) axins.plot(t, AL, color="red",linewidth=1.5) # 设置放大区域的x和y轴限制,以显示您感兴趣的局部区域 axins.set_xlim(1500, 1570) axins.set_ylim(10, 15) # 隐藏局部放大图的x轴和y轴刻度 #axins.xaxis.set_tick_params(which='both', top=False, bottom=False, labelbottom=False) #axins.yaxis.set_tick_params(which='both', left=False, right=False, labelleft=False) # 添加一个矩形框在原图上标记放大区域的位置 # 注意:loc1和loc2是矩形框在原图上的对角线顶点位置(近似值),这里需要手动调整 mark_inset(plt.gca(), axins, loc1=2, loc2=4, fc="none", ec="1.5") pylab.show()#让绘制的图像在屏幕上显示出来
/home/jht/Figure_1.png