from matplotlib.patches import ConnectionPatch
import numpy as np
def zone_and_linked(ax,axins,zone_left,zone_right,x,y,linked='bottom',
x_ratio=0.05,y_ratio=0.05):
"""缩放内嵌图形,并且进行连线
ax: 调用plt.subplots返回的画布。例如: fig,ax = plt.subplots(1,1)
axins: 内嵌图的画布。 例如 axins = ax.inset_axes((0.4,0.1,0.4,0.3))
zone_left: 要放大区域的横坐标左端点
zone_right: 要放大区域的横坐标右端点
x: X轴标签
y: 列表,所有y值
linked: 进行连线的位置,{'bottom','top','left','right'}
x_ratio: X轴缩放比例
y_ratio: Y轴缩放比例
"""
xlim_left = x[zone_left]-(x[zone_right]-x[zone_left])*x_ratio
xlim_right = x[zone_right]+(x[zone_right]-x[zone_left])*x_ratio
# y_data = np.hstack([yi[zone_left:zone_right] for yi in y])
y_data = y[zone_left:zone_right]
ylim_bottom = np.min(y_data)-(np.max(y_data)-np.min(y_data))*y_ratio
ylim_top = np.max(y_data)+(np.max(y_data)-np.min(y_data))*y_ratio
# ylim_bottom=ylim_bottom.astype(float)
axins.set_xlim(xlim_right, xlim_left)
axins.set_ylim(ylim_bottom[0], ylim_top[0])
ax.plot([xlim_left,xlim_right,xlim_right,xlim_left,xlim_left],[ylim_bottom,ylim_bottom,ylim_top,ylim_top,ylim_bottom],"black",linewidth=1)
if linked == 'bottom':
xyA_1, xyB_1 = (xlim_left,ylim_top), (xlim_left,ylim_bottom)
xyA_2, xyB_2 = (xlim_right,ylim_top), (xlim_right,ylim_bottom)
elif linked == 'top':
xyA_1, xyB_1 = (xlim_left,ylim_bottom), (xlim_left,ylim_top)
xyA_2, xyB_2 = (xlim_right,ylim_bottom), (xlim_right,ylim_top)
elif linked == 'left':
xyA_1, xyB_1 = (xlim_right,ylim_top), (xlim_left,ylim_top)
xyA_2, xyB_2 = (xlim_right,ylim_bottom), (xlim_left,ylim_bottom)
elif linked == 'right':
xyA_1, xyB_1 = (xlim_left,ylim_top), (xlim_right,ylim_top)
xyA_2, xyB_2 = (xlim_left,ylim_bottom), (xlim_right,ylim_bottom)
con = ConnectionPatch(xyA=xyA_1,xyB=xyB_1,coordsA="data",
coordsB="data",axesA=axins,axesB=ax)
axins.add_artist(con)
con = ConnectionPatch(xyA=xyA_2,xyB=xyB_2,coordsA="data",
coordsB="data",axesA=axins,axesB=ax)
axins.add_artist(con)
用于对IC曲线峰/谷值局部放大 be like↓