Python中局部放大图案例
生活随笔
收集整理的這篇文章主要介紹了
Python中局部放大图案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例子一:
先上完整代碼和效果圖:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes from matplotlib.patches import ConnectionPatchMAX_EPISODES = 10000 x_axis_data = [] for l in range(MAX_EPISODES):x_axis_data.append(l)reward_demaddpg5=[] reward_demaddpg10=[] for l in range(MAX_EPISODES):reward_demaddpg5.append(l**1.5)for l in range(MAX_EPISODES):reward_demaddpg10.append(l**1.6)fig, ax = plt.subplots(1, 1) ax.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, label='$5*10^{-6}$') # ax.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, label='$5*10^{-7}$') # ax.plot(x_axis_data, reward_demaddpg25, color='#B84D37', alpha=0.8, label='$1*10^{-7}$') ax.legend(loc="best") ax.set_xlabel('Episodes') ax.set_ylabel('Total reward')# axins = inset_axes(ax, width="40%", height="30%", loc='lower left', # bbox_to_anchor=(0.3, 0.1, 1, 1), # bbox_transform=ax.transAxes)axins = ax.inset_axes((0.2, 0.5, 0.4, 0.3)) axins.plot(x_axis_data, reward_demaddpg5, color='#4169E1', alpha=0.8, label='$1*10^{-5}$') axins.plot(x_axis_data, reward_demaddpg10, color='#848484', alpha=0.8, label='$5*10^{-6}$')# 設置放大區間 對應橫坐標 zone_left = 9000 zone_right =9999 #!最右側不能越界!!!!# 坐標軸的擴展比例(根據實際數據調整) 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((reward_demaddpg5[zone_left:zone_right], reward_demaddpg10[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,"black")# 畫兩條線 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)plt.show()導入庫:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes from matplotlib.patches import ConnectionPatch橫坐標設置:
MAX_EPISODES = 300 #自己定 x_axis_data = [] for l in range(MAX_EPISODES):x_axis_data.append(l)#reward_demaddpg[]儲存的是執行demaddpg算法后所獲得的結果。 其中fig, ax = plt.subplots(a,b)用來控制子圖個數:a為行數,b為列數。嵌入局部放大圖的坐標系:兩種寫法
axins = inset_axes(ax, width="40%", height="30%", loc='lower left',bbox_to_anchor=(0.3, 0.1, 1, 1), bbox_transform=ax.transAxes)- ax:父坐標系
- width, height:子坐標系的寬度和高度(百分比形式或者浮點數個數)
- loc:子坐標系的位置
- bbox_to_anchor:邊界框,四元數組(x0, y0, width, height)
- bbox_transform:從父坐標系到子坐標系的幾何映射
- axins:子坐標系
?另外有一種更加簡潔的子坐標系嵌入方法:更好理解
axins = ax.inset_axes((0.2, 0.2, 0.4, 0.3))?設置放大區間,調整子坐標系的顯示范圍
# 設置放大區間 zone_left = 100 #小心越界 zone_right = 150# 坐標軸的擴展比例(根據實際數據調整) 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((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# 調整子坐標系的顯示范圍 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,"black")# 畫兩條線 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)參考鏈接:Python中 Matplotlib局部放大圖的畫法_wulishinian的博客-CSDN博客_python 局部放大
方案二:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import mark_inset from mpl_toolkits.axes_grid1.inset_locator import inset_axes# 準備數據 x = np.linspace(-0.1*np.pi, 2*np.pi, 30) y_1 = np.sinc(x)+0.7 y_2 = np.tanh(x) y_3 = np.exp(-np.sinc(x))# 繪圖 fig, ax = plt.subplots(1, 1, figsize=(6, 4)) ax.plot(x, y_1, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C0')ax.plot(x, y_2, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C3')ax.plot(x, y_3, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C2')ax.legend(labels=["y_1", "y_2","y_3"], ncol=3)# 嵌入繪制局部放大圖的坐標系 axins = inset_axes(ax, width="40%", height="30%",loc='lower left',bbox_to_anchor=(0.5, 0.1, 1, 1),bbox_transform=ax.transAxes)# 在子坐標系中繪制原始數據 axins.plot(x, y_1, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C0')axins.plot(x, y_2, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C3')axins.plot(x, y_3, color='k', linestyle=':', linewidth=1,marker='o', markersize=5,markeredgecolor='black', markerfacecolor='C2')# 設置放大區間 zone_left = 11 zone_right = 12# 坐標軸的擴展比例(根據實際數據調整) x_ratio = 0.5 # x軸顯示范圍的擴展比例 y_ratio = 0.5 # y軸顯示范圍的擴展比例# X軸的顯示范圍 xlim0 = x[zone_left]-(x[zone_right]-x[zone_left])*x_ratio xlim1 = x[zone_right]+(x[zone_right]-x[zone_left])*x_ratio# Y軸的顯示范圍 y = np.hstack((y_1[zone_left:zone_right], y_2[zone_left:zone_right], y_3[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)# 建立父坐標系與子坐標系的連接線 # loc1 loc2: 坐標系的四個角 # 1 (右上) 2 (左上) 3(左下) 4(右下) mark_inset(ax, axins, loc1=3, loc2=1, fc="none", ec='k', lw=1)# 顯示 plt.show()參考鏈接:【Matplotlib】 局部放大圖 - 知乎
總結
以上是生活随笔為你收集整理的Python中局部放大图案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3个步骤,轻松画好页面流程图
- 下一篇: 解决ovirt虚拟机使用FCP瘦分配安装