您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“如何使用matplotlib库实现图形局部数据放大显示”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes from matplotlib.patches import ConnectionPatch import pandas as pd MAX_EPISODES = 300 x_axis_data = [] for l in range(MAX_EPISODES): x_axis_data.append(l) fig, ax = plt.subplots(1, 1) data1 = pd.read_csv('./result/test_reward.csv')['test_reward'].values.tolist()[:MAX_EPISODES] data2 = pd.read_csv('./result/test_reward_att.csv')['test_reward_att'].values.tolist()[:MAX_EPISODES] ax.plot(data1,label="no att") ax.plot(data2,label = "att") ax.legend()
#插入子坐标系 axins = inset_axes(ax, width="40%", height="20%", loc=3, bbox_to_anchor=(0.3, 0.1, 2, 2), bbox_transform=ax.transAxes) #在子坐标系中放入数据 axins.plot(data1) axins.plot(data2)
#设置放大区间 zone_left = 150 zone_right = 170 # 坐标轴的扩展比例(根据实际数据调整) x_ratio = 0 # x轴显示范围的扩展比例 y_ratio = 0.05 # y轴显示范围的扩展比例 # X轴的显示范围 xlim0 = x_axis_data[zone_left]-(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio xlim1 = x_axis_data[zone_right]+(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio # Y轴的显示范围 y = np.hstack((data1[zone_left:zone_right], data2[zone_left:zone_right])) ylim0 = np.min(y)-(np.max(y)-np.min(y))*y_ratio ylim1 = np.max(y)+(np.max(y)-np.min(y))*y_ratio # 调整子坐标系的显示范围 axins.set_xlim(xlim0, xlim1) axins.set_ylim(ylim0, ylim1)
(-198439.93763, -134649.56637000002)
# 原图中画方框 tx0 = xlim0 tx1 = xlim1 ty0 = ylim0 ty1 = ylim1 sx = [tx0,tx1,tx1,tx0,tx0] sy = [ty0,ty0,ty1,ty1,ty0] ax.plot(sx,sy,"blue") # 画两条线 #第一条线 xy = (xlim0,ylim0) xy2 = (xlim0,ylim1) """ xy为主图上坐标,xy2为子坐标系上坐标,axins为子坐标系,ax为主坐标系。 """ con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data", axesA=axins,axesB=ax) axins.add_artist(con) #第二条线 xy = (xlim1,ylim0) xy2 = (xlim1,ylim1) con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data", axesA=axins,axesB=ax) axins.add_artist(con)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes from matplotlib.patches import ConnectionPatch import pandas as pd MAX_EPISODES = 300 x_axis_data = [] for l in range(MAX_EPISODES): x_axis_data.append(l) fig, ax = plt.subplots(1, 1) data1 = pd.read_csv('./result/test_reward.csv')['test_reward'].values.tolist()[:MAX_EPISODES] data2 = pd.read_csv('./result/test_reward_att.csv')['test_reward_att'].values.tolist()[:MAX_EPISODES] ax.plot(data1,label="no att") ax.plot(data2,label = "att") ax.legend() #插入子坐标系 axins = inset_axes(ax, width="20%", height="20%", loc=3, bbox_to_anchor=(0.3, 0.1, 2, 2), bbox_transform=ax.transAxes) #在子坐标系中放入数据 axins.plot(data1) axins.plot(data2) #设置放大区间 zone_left = 150 zone_right = 170 # 坐标轴的扩展比例(根据实际数据调整) x_ratio = 0 # x轴显示范围的扩展比例 y_ratio = 0.05 # y轴显示范围的扩展比例 # X轴的显示范围 xlim0 = x_axis_data[zone_left]-(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio xlim1 = x_axis_data[zone_right]+(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio # Y轴的显示范围 y = np.hstack((data1[zone_left:zone_right], data2[zone_left:zone_right])) ylim0 = np.min(y)-(np.max(y)-np.min(y))*y_ratio ylim1 = np.max(y)+(np.max(y)-np.min(y))*y_ratio # 调整子坐标系的显示范围 axins.set_xlim(xlim0, xlim1) axins.set_ylim(ylim0, ylim1) # 原图中画方框 tx0 = xlim0 tx1 = xlim1 ty0 = ylim0 ty1 = ylim1 sx = [tx0,tx1,tx1,tx0,tx0] sy = [ty0,ty0,ty1,ty1,ty0] ax.plot(sx,sy,"blue") # 画两条线 # 第一条线 xy = (xlim0,ylim0) xy2 = (xlim0,ylim1) """ xy为主图上坐标,xy2为子坐标系上坐标,axins为子坐标系,ax为主坐标系。 """ con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data", axesA=axins,axesB=ax) axins.add_artist(con) # 第二条线 xy = (xlim1,ylim0) xy2 = (xlim1,ylim1) con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data", axesA=axins,axesB=ax) axins.add_artist(con)
“如何使用matplotlib库实现图形局部数据放大显示”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。