Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use matplotlib Library to realize magnified display of Local data of Graphics

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use matplotlib library to achieve magnified display of local graphic data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Draw the overall figure import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1.inset_locator import inset_axesfrom matplotlib.patches import ConnectionPatchimport pandas as pdMAX_EPISODES = 300x_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 ()

2. Insert the local subcoordinate system # insert the subcoordinate 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 the data axins.plot (data1) axins.plot (data2) in the subcoordinate system

3. Limit the local subcoordinate system data range # set magnification interval zone_left = 150zone_right = 17 coordinate axis expansion ratio (adjusted according to actual data) x_ratio = expansion ratio of display range of x-axis y_ratio = expansion ratio of y-axis display range # display range of X-axis xlim0 = x axis _ data [zone _ left]-(x axis _ data [zone _ right]-x _ axis_ data [zone _ left]) * x_ratioxlim1 = x _ zones _ data [zone _ right] + (x _ zones _ data [zone _ right]-x _ zones _ data [zone _ left]) * the display range of the x_ratio# Y axis y = np.hstack ((Data1 [zone _ left:zone_right]) Data2 [zone _ left:zone_right]) ylim0 = np.min (y)-(np.max (y)-np.min (y)) * y_ratioylim1 = np.max (y) + (np.max (y)-np.min (y)) * y_ratio# adjusts the display range of the subcoordinate system axins.set_xlim (xlim0, xlim1) axins.set_ylim (ylim0, ylim1)

(- 198439.93763,-134649.56637000002)

4. Add boxes and connecting lines # draw the box tx0 = xlim0tx1 = xlim1ty0 = ylim0ty1 = ylim1sx = [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 is the coordinate on the main graph, xy2 is the coordinate on the sub-coordinate system, axins is the sub-coordinate system, and ax is the main coordinate system. " Con = ConnectionPatch (xyA=xy2,xyB=xy,coordsA= "data", coordsB= "data", axesA=axins,axesB=ax) axins.add_artist (con) # 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)

Overall implementation code import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1.inset_locator import inset_axesfrom matplotlib.patches import ConnectionPatchimport pandas as pdMAX_EPISODES = 300x_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 subcoordinate 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 axins.plot (data1) axins.plot (data2) # set magnification range zone_left = 150zone_right = 17 "axis expansion ratio (adjusted according to actual data) x_ratio = expansion ratio of x-axis display range y_ratio = 0.05y axis display range expansion ratio # X-axis display range Xlim0 = xroomaxisdata [zone _ left]-(xroomaxisdata [zone _ right]-xroomaxisdata [zone _ left]) * x_ratioxlim1 = xroomaxisdata [zone _ right] + (xroomaxisdata [zone _ right]-xroomaxisdata [zone _ left]) * display range of the x_ratio# Y axis y = np.hstack ((d ata1 [zone _ data]) Data2 [zone _ left:zone_right]) ylim0 = np.min (y)-(np.max (y)-np.min (y)) * y_ratioylim1 = np.max (y) + (np.max (y)-np.min (y)) * y_ratio# adjusts the display range of the subcoordinate system axins.set_xlim (xlim0, xlim1) axins.set_ylim (ylim0, ylim1) # the box tx0 = xlim0tx1 = xlim1ty0 = ylim0ty1 = ylim1sx = [tx0,tx1,tx1,tx0] in the original picture 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 is the coordinate on the main graph Xy2 is the coordinate of the sub-coordinate system, axins is the sub-coordinate system, and ax is the main coordinate system. Con = ConnectionPatch (xyA=xy2,xyB=xy,coordsA= "data", coordsB= "data", axesA=axins,axesB=ax) axins.add_artist (con) # 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)

This is the end of the introduction of "how to use the matplotlib library to realize the magnified display of local graphic data". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report