您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何使用matplotlib庫實(shí)現(xiàn)圖形局部數(shù)據(jù)放大顯示”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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()
#插入子坐標(biāo)系 axins = inset_axes(ax, width="40%", height="20%", loc=3, bbox_to_anchor=(0.3, 0.1, 2, 2), bbox_transform=ax.transAxes) #在子坐標(biāo)系中放入數(shù)據(jù) axins.plot(data1) axins.plot(data2)
#設(shè)置放大區(qū)間 zone_left = 150 zone_right = 170 # 坐標(biāo)軸的擴(kuò)展比例(根據(jù)實(shí)際數(shù)據(jù)調(diào)整) x_ratio = 0 # x軸顯示范圍的擴(kuò)展比例 y_ratio = 0.05 # y軸顯示范圍的擴(kuò)展比例 # 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 # 調(diào)整子坐標(biāo)系的顯示范圍 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為主圖上坐標(biāo),xy2為子坐標(biāo)系上坐標(biāo),axins為子坐標(biāo)系,ax為主坐標(biāo)系。 """ 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() #插入子坐標(biāo)系 axins = inset_axes(ax, width="20%", height="20%", loc=3, bbox_to_anchor=(0.3, 0.1, 2, 2), bbox_transform=ax.transAxes) #在子坐標(biāo)系中放入數(shù)據(jù) axins.plot(data1) axins.plot(data2) #設(shè)置放大區(qū)間 zone_left = 150 zone_right = 170 # 坐標(biāo)軸的擴(kuò)展比例(根據(jù)實(shí)際數(shù)據(jù)調(diào)整) x_ratio = 0 # x軸顯示范圍的擴(kuò)展比例 y_ratio = 0.05 # y軸顯示范圍的擴(kuò)展比例 # 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 # 調(diào)整子坐標(biāo)系的顯示范圍 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為主圖上坐標(biāo),xy2為子坐標(biāo)系上坐標(biāo),axins為子坐標(biāo)系,ax為主坐標(biāo)系。 """ 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庫實(shí)現(xiàn)圖形局部數(shù)據(jù)放大顯示”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。