当前位置:网站首页>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()
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)
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)
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)
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 ” 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 !
边栏推荐
- 智云健康上市在即:长期亏损,美年健康俞熔已退出,未来难言乐观
- Renren mall locates the file according to the route
- [nailing scenario capability package] company / Park Digital canteen
- Windows11 windows security center cannot open Windows Defender cannot open
- Winget: the "Winget" item cannot be recognized as the name of cmdlet, function, script file or runnable program. Win11 Winget cannot be used to solve this problem
- Mathematical analysis_ Notes_ Chapter 4: continuous function classes and other function classes
- Summary of digital image processing knowledge points
- Canoe learning notes (3)
- On merging and sorting
- Bear market guide | some essential lessons and specific survival rules
猜你喜欢
数字图像处理知识点总结概述
Various special effect cases of Experiment 3
js禁用浏览器 pdf 打印、下载功能(pdf.js 禁用打印下载、功能)
PHP Chinese word segmentation API, Harbin Institute of technology ltpcloud, naturallanguageprocessing, free, best practices!
XMIND to excel test case
Apache uses setenvif to identify and release the CDN traffic according to the request header, intercept the DDoS traffic, pay attention to the security issues during CDN deployment, and bypass the CDN
Processing of limit operator in Presto
实验三的各种特效案例
MySQL is slow to add indexes_ Why is your SQL so slow? Why is your MySQL index invalid?
Win11无法删除文件夹怎么办?Win11无法删除文件夹的解决方法
随机推荐
Summary of several methods for FPS calculation
How to write an infinite loop
Presto中Limit算子的处理过程
ASP. Net core uses function switches to control Route Access (Continued) yyds dry inventory
HNU数据库系统概论 ODBC
Local Yum source production
Solve the parameter problem that laravels cannot receive wechat callback
Beginner to embedded development
Win11无法删除文件夹怎么办?Win11无法删除文件夹的解决方法
mysql 通过sql 修改多表增加多个字段
Understand which C standards are there & understand the C compilation pipeline
On merging and sorting
Website judges network connection disconnection, JS judges network connection disconnection, best practice
挖财证券开户安全嘛?
Renren mall locates the file according to the route
leetcode: 49. 字母异位词分组
Jmeter- (III) create user test cases for interface testing
Preliminary solution of i/o in socket programming
JS disable the browser PDF printing and downloading functions (pdf.js disable the printing and downloading functions)
js 限制鼠标移动范围