首頁 > 軟體

使用matplotlib庫實現圖形區域性資料放大顯示的實踐

2022-02-18 10:03:15

一、繪製總體圖形

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庫實現圖形區域性資料放大顯示的實踐的文章就介紹到這了,更多相關matplotlib 圖形區域性資料放大顯示內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com