In contrast experiments, in addition to the comparison of the overall shape, in many cases, it is necessary to enlarge the data of a certain part to observe the finer contrast effect.

tool

The Python Matplotlib library function

steps

1. Import the dependent libraries

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.patches import ConnectionPatchCopy the code

2. Prepare data

Reward_demaddpg [] stores 300 reward results obtained after executing demaddpg algorithm.

So the x-coordinate is set to:

MAX_EPISODES = 300
x_axis_data = []
for l in range(MAX_EPISODES):
    x_axis_data.append(l)Copy the code

The 5 comparative experimental results exist in 5 arrays, which respectively represent different learning rates set in Demaddpg algorithm:

3. Draw the main drawing

Plot (x_axIS_data, reward_demaddpg5, color='#4169E1', alpha=0.8, Label ='$1*10^{-5}$') ax.plot(x_axis_data, reward_demaddpg10, color='#848484', alpha=0.8, Plot (x_axis_data, reward_demaddpg15, color='#FF774A', alpha=0.8, Label ='$1*10^{-6}$') ax.plot(x_axis_data, reward_demaddpG20, color='#575B20', alpha=0.8, Plot (x_axis_data, reward_demaddPG25, color='#B84D37', alpha=0.8, label='$1*10^{-7}$') ax.legend(loc="lower right") ax.set_xlabel('Episodes') ax.set_ylabel('Total reward')Copy the code

In FIG. Ax = PLT. Subplots (a,b) are used to control the number of subplots: A is the number of rows and B is the number of columns.

The renderings are as follows:

4. Embed the coordinate system of the local enlarged image

Axins = inset_axes(ax, width=" axes ", height=" axes ", LOc ='lower left', bbox_to_anchor=(0.3, 0.1, 1, 1), bbox_transform=ax.transAxes)Copy the code

The parameters are described as follows:

  • Ax: parent coordinate system
  • Width, height: The width and height of the sub-coordinate system (in percentage or floating point numbers)
  • Loc: position of the subcoordinate system
  • Bbox_to_anchor: Boundary box, quad (x0, y0, width, height)
  • Bbox_transform: Geometric mapping from parent to child coordinates
  • Axins: subcoordinate system

Fixed the width and height of the coordinate system as well as the boundary box. Set loC to upper left, lower left, upper right (default), lower right and middle respectively. The effect picture is as follows:

The renderings are as follows:

In addition, there is a more concise subcoordinate system embedding method:

Axins = ax.inset_axes(0.2, 0.2, 0.4, 0.3)Copy the code

Ax is the parent coordinate system, and the following four parameters are also (x0, y0, width, height). With x0=0.2*x and y0=0.2*y in the parent coordinate system as the starting point at the lower left corner, a sub-coordinate system with width of 0.2x and height of 0.3Y is embedded, where X and y are the coordinate axes of the parent coordinate system respectively. The effect is shown below:

5. Draw the original data in the sub-coordinate system

Axins.plot (x_axis_data, reward_demaddpg5, color='#4169E1', alpha=0.8, label='$1*10^{-5}$') axins.plot(x_axis_data, reward_demaddpg5, color='#4169E1', alpha=0.8, label='$1*10^{-5}$') Reward_demaddpg10, color='#848484', alpha=0.8, label='$5*10^{-6}$') axins.plot(x_axis_data, reward_demaddpg15, Plot (x_axis_data, reward_demaddpG20, color='#575B20', alpha=0.8, label='$1*10^{-6}$') axins.plot(x_axis_data, reward_demaddpG20, color='#575B20', alpha=0.8, Label = '$5 * 10 ^ {7} $') axins. The plot (x_axis_data reward_demaddpg25, color =' # B84D37, alpha = 0.8, label = '$1 * 10 ^ {7} $')Copy the code

The effect is as follows:

6. Set the zoom range and adjust the display range of the sub-coordinate system

Zone_left = 100; zone_right = 150; xratio = 0; xratio = 0.05 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((reward_demaddpg5[zone_left:zone_right], reward_demaddpg10[zone_left:zone_right], reward_demaddpg15[zone_left:zone_right],reward_demaddpg20[zone_left:zone_right], reward_demaddpg25[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 subcoordinate system axins.set_xlim(xlim0, xlim1) axins.set_ylim(ylim0, ylim1)Copy the code

The effect is as follows:

7. Establish the connection line between the parent coordinate system and the child coordinate system

Tx0 = xlim0 TX1 = xlim1 ty0 = ylim0 TY1 = ylim1 sx = [tx0,tx1,tx1,tx0,tx0] sy = [ty0,ty0,ty1, ty0] Ax.plot (sx,sy,"black") # Draw two lines xy = (xlim0,ylim0) xy2 = (xlim0,ylim1) 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)Copy the code

Draw boxes

Using pyplot(x,y), you can easily specify colors, line widths, and so on.

Draw lines across subgraphs

There is a ConnectionPatch type available for Matplotlib. patches, which can be used to draw lines between one or more subdiagrams.

con = ConnectionPatch(xyA,xyB,coordsA,coordsB, axesA,axesB)Copy the code

Here xyA is the point in the subgraph, xyB is the point in the main graph, coordsA and coordsB default value “data”, do not change, then axesA to add the subgraph, axesB is the main graph to connect.

axins.add_artist(con)Copy the code

Finally, add the connection line to the subgraph. Note that this is a subgraph, not a master graph.

The renderings are as follows:

The above is the local magnification of the drawing method, the final box and line drawing method is more than one, here is just to find a relatively simple and direct one. I hope it helps.

Original link: juejin.cn/post/684490…

Wenyuan network, only for the use of learning, if there is infringement please contact delete.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials

Follow the Python circle and get good articles delivered daily.