python绘制inset图

python绘制inset图(局部放大图):
1、Inset plots in Matplotlib:https://scipython.com/blog/inset-plots-in-matplotlib/
代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition,
                                                  mark_inset)

T, Cp = np.loadtxt('Ta-Cp.txt', unpack=True)
T_E, CV_E = np.loadtxt('Ta-CV_Einstein.txt', unpack=True)
T_D, CV_D = np.loadtxt('Ta-CV_Debye.txt', unpack=True)

fig, ax1 = plt.subplots()
T_E = np.arange(1,max(T)+1,1)
# The data.
ax1.plot(T, Cp, 'x', c='b', mew=2, alpha=0.8, label='Experiment')
# The Einstein fit.
ax1.plot(T_E, CV_E, c='m', lw=2, alpha=0.5, label='Einstein model')
ax1.set_xlabel(r'$T\,/\mathrm{K}$')
ax1.set_ylabel(r'$C_p\,/\mathrm{J\,K^{-1}\,mol^{-1}}$')
ax1.legend(loc=0)

# Create a set of inset Axes: these should fill the bounding box allocated to
# them.
ax2 = plt.axes([0,0,1,1])
# Manually set the position and relative size of the inset axes within ax1
ip = InsetPosition(ax1, [0.4,0.2,0.5,0.5])
ax2.set_axes_locator(ip)
# Mark the region corresponding to the inset axes on ax1 and draw lines
# in grey linking the two axes.
mark_inset(ax1, ax2, loc1=2, loc2=4, fc="none", ec='0.5')

# The data: only display for low temperature in the inset figure.
Tmax = max(T_D)
ax2.plot(T[T<=Tmax], Cp[T<=Tmax], 'x', c='b', mew=2, alpha=0.8,
         label='Experiment')
# The Einstein fit (not very good at low T).
ax2.plot(T_E[T_E<=Tmax], CV_E[T_E<=Tmax], c='m', lw=2, alpha=0.5,
         label='Einstein model')
# The Debye fit.
ax2.plot(T_D, CV_D, c='r', lw=2, alpha=0.5, label='Debye model')
ax2.legend(loc=0)

# Some ad hoc tweaks.
ax1.set_ylim(0,26)
ax2.set_yticks(np.arange(0,2,0.4))
ax2.set_xticklabels(ax2.get_xticks(), backgroundcolor='w')
ax2.tick_params(axis='x', which='major', pad=8)

plt.show()

代码来源:https://scipython.com/blog/inset-plots-in-matplotlib/

2、Zoom region inset Axes:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/zoom_inset_axes.html

3、Inset zoom:https://pranabdas.github.io/python-tutorial/matplotlib/inset-zoom/
代码:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.dpi"] = 150
plt.rcParams["figure.facecolor"]="white"

x = np.linspace(1, 10, num=1000)
y = x**2 + np.sin(100/x**2)

fig, ax = plt.subplots()
ax.plot(x, y)
axins = ax.inset_axes([0.15, 0.4, 0.4, 0.5])
axins.plot(x[:400], y[:400])
ax.indicate_inset_zoom(axins, linewidth=1)
axins.set_xticklabels('')
axins.set_yticklabels('')
plt.show()

结果:
在这里插入图片描述
4、各参数:
(1) Axes.inset_axes:https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.inset_axes.html

Axes.inset_axes(bounds, *, transform=None, zorder=5, **kwargs)

bounds[x0, y0, width, height]:Lower-left corner of inset Axes, and its width and height.

(2)matplotlib.axes.Axes.indicate_inset_zoom:https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.indicate_inset_zoom.html

5、Python 绘制局部放大图例子
https://blog.csdn.net/weixin_45826022/article/details/113486448

Matplotlib局部放大图画法:https://juejin.cn/post/6844904183548608520

局部放大折线图+阴影区间绘制:
https://blog.csdn.net/oax_knud/article/details/128015798

总结:
绘制局部放大图核心代码:

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
#设置局部放大图的位置
ax_inset = inset_axes(ax, width="40%", height="30%", loc='lower left',
                              bbox_to_anchor=(0.4, 0.55, 1, 1),
                              bbox_transform=ax.transAxes, )
 # 绘制图
 ax_inset.plot()
 # 主图和子图之间边界框连线绘制
 """
    loc1:其中一个连接线的位置 ,坐标系的四个角( 1 (右上) 2 (左上) 3(左下) 4(右下))
    loc2:另外一个连接线的位置
    """
 mark_inset(ax, ax_inset, loc1=3, loc2=4, fc="none", ec='k', lw=0.2)  # 1 (右上) 2 (左上) 3(左下) 4(右下)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值