当前位置:网站首页>How to use Matplotlib library to realize enlarged display of graphic local data

How to use Matplotlib library to realize enlarged display of graphic local data

2022-06-25 21:47:00 Yisu cloud

How to use matplotlib Library to achieve graphic local data amplification display

This article introduces “ How to use matplotlib Library to achieve graphic local data amplification display ” Knowledge about , During the operation of the actual case , Many people will encounter such difficulties , Next, let Xiaobian lead you to learn how to deal with these situations ! I hope you will read carefully , Be able to learn !

One 、 Draw overall graph

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()

 How to use matplotlib Library to achieve graphic local data amplification display

Two 、 Insert local sub coordinate system

# Insert sub coordinate system 
axins = inset_axes(ax, width="40%", height="20%", loc=3,
                   bbox_to_anchor=(0.3, 0.1, 2, 2),
                   bbox_transform=ax.transAxes)
# Put data in the sub coordinate system 
axins.plot(data1)
axins.plot(data2)

 How to use matplotlib Library to achieve graphic local data amplification display

3、 ... and 、 Limit the data range of local sub coordinate system

# Set the amplification range 
zone_left = 150
zone_right = 170
#  The expansion scale of the axis ( Adjust according to the actual data )
x_ratio = 0  # x The expansion ratio of the axis display range 
y_ratio = 0.05  # y The expansion ratio of the axis display range 

# X Axis display range 
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 Axis display range 
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

#  Adjust the display range of the sub coordinate system 
axins.set_xlim(xlim0, xlim1)
axins.set_ylim(ylim0, ylim1)

(-198439.93763, -134649.56637000002)

 How to use matplotlib Library to achieve graphic local data amplification display

Four 、 Add boxes and connecting lines

#  Draw a box in the original picture 
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")

#  Draw two lines 
# The first line 
xy = (xlim0,ylim0)
xy2 = (xlim0,ylim1)
"""
xy Coordinates on the main graph ,xy2 Is the coordinate on the sub coordinate system ,axins Is a sub coordinate system ,ax Primary coordinate system .
"""
con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
        axesA=axins,axesB=ax)

axins.add_artist(con)
# The second line 
xy = (xlim1,ylim0)
xy2 = (xlim1,ylim1)
con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
        axesA=axins,axesB=ax)
axins.add_artist(con)

 How to use matplotlib Library to achieve graphic local data amplification display

5、 ... and 、 Overall implementation code

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()

# Insert sub coordinate system 
axins = inset_axes(ax, width="20%", height="20%", loc=3,
                   bbox_to_anchor=(0.3, 0.1, 2, 2),
                   bbox_transform=ax.transAxes)
# Put data in the sub coordinate system 
axins.plot(data1)
axins.plot(data2)

# Set the amplification range 
zone_left = 150
zone_right = 170
#  The expansion scale of the axis ( Adjust according to the actual data )
x_ratio = 0  # x The expansion ratio of the axis display range 
y_ratio = 0.05  # y The expansion ratio of the axis display range 

# X Axis display range 
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 Axis display range 
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

#  Adjust the display range of the sub coordinate system 
axins.set_xlim(xlim0, xlim1)
axins.set_ylim(ylim0, ylim1)


#  Draw a box in the original picture 
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")

#  Draw two lines 
#  The first line 
xy = (xlim0,ylim0)
xy2 = (xlim0,ylim1)
"""
xy Coordinates on the main graph ,xy2 Is the coordinate on the sub coordinate system ,axins Is a sub coordinate system ,ax Primary coordinate system .
"""
con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
        axesA=axins,axesB=ax)

axins.add_artist(con)
#  The second line 
xy = (xlim1,ylim0)
xy2 = (xlim1,ylim1)
con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
        axesA=axins,axesB=ax)
axins.add_artist(con)

 How to use matplotlib Library to achieve graphic local data amplification display

“ How to use matplotlib Library to achieve graphic local data amplification display ” That's all for , Thanks for reading . If you want to know more about the industry, you can pay attention to Yisu cloud website , Xiaobian will output more high-quality practical articles for you !

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181237112612.html