Matplotlib 缩放和打印插入图

Matplotlib 缩放和打印插入图,matplotlib,jupyter-notebook,scatter-plot,subplot,insets,Matplotlib,Jupyter Notebook,Scatter Plot,Subplot,Insets,我正在尝试进行放大插图,如图所示: 代码的第一部分正在工作,即正在打印文件。 只有当我尝试绘制缩放的部分时,才会出现以下错误 我一直在努力想办法,但没有一篇帖子真的有帮助 AttributeError Traceback (most recent call last) <ipython-input-41-46879fbc5ce6> in <module>() 44 plt.close()

我正在尝试进行放大插图,如图所示:

代码的第一部分正在工作,即正在打印文件。 只有当我尝试绘制缩放的部分时,才会出现以下错误 我一直在努力想办法,但没有一篇帖子真的有帮助

AttributeError                            Traceback (most recent call last)
<ipython-input-41-46879fbc5ce6> in <module>()
     44     plt.close()
     45 
---> 46 fit_data()

<ipython-input-41-46879fbc5ce6> in fit_data()
     16     #axins.xaxis.set_major_locator(MaxNLocator(nbins=1, prune='lower'))
     17 
---> 18     plt1 = zoomed_inset_axes(plt, 2.5,  loc=4 )
     19     plt1.plot(data1['pm'], data1['Dis(pc)'])#,marker='o', color='red', edgecolor='red', s=100)
     20     plt1.axis([5.062645643, 6.482765605, 487.026819, 569.4313421])

~/anaconda3/lib/python3.6/site-packages/mpl_toolkits/axes_grid1/inset_locator.py in zoomed_inset_axes(parent_axes, zoom, loc, bbox_to_anchor, bbox_transform, axes_class, axes_kwargs, borderpad)
    529 
    530     if axes_kwargs is None:
--> 531         inset_axes = axes_class(parent_axes.figure, parent_axes.get_position())
    532     else:
    533         inset_axes = axes_class(parent_axes.figure, parent_axes.get_position(),

AttributeError: module 'matplotlib.pyplot' has no attribute 'get_position'
我一直试图拟合的样本数据是

797.3266855 9.518953577 487.026819  6.41595323
457.3328822 9.408619701 493.8012816 6.352140859
321.4279994 10.99152002 505.8109589 6.482765605
643.1595144 11.33567151 515.0500793 5.689992589
897.9396964 7.098272377 523.5118663 5.062645643
658.5927932 8.401072532 526.8570713 5.951114622
885.8478465 9.59502937  537.6740407 6.123622699
569.4313421 5.913067314 563.2567733 6.089519297
419.540411  31.7279367  569.4313421 5.913067314
386.0084504 13.82448229     
487.026819  6.41595323      
790.5056852 14.17210085     
736.5781168 4.142827023     
927.9643155 13.42713535     
106.249016  49.12866299     
678.4950877 3.174864242     
108.0434865 60.24915209     
809.8782024 8.371119015     
692.3002948 7.215181213     
915.4764187 15.4360679      
874.5699615 8.706973258     
962.0774108 3.223371528     
401.4037586 31.03032051     
671.4700933 11.1975808      
834.7473745 15.30785654 
作者的评论是正确的。但是,通过进行更改,您的代码仍将无法运行。下面我复制了我认为是你想要的一个例子。我希望有帮助

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset

csv_data = [
[797.3266855, 9.518953577, 487.026819, 6.41595323],
[457.3328822, 9.408619701, 493.8012816, 6.352140859],
[321.4279994, 10.99152002, 505.8109589, 6.482765605],
[643.1595144, 11.33567151, 515.0500793, 5.689992589],
[897.9396964, 7.098272377, 523.5118663, 5.062645643],
[658.5927932, 8.401072532, 526.8570713, 5.951114622],
[885.8478465, 9.59502937, 537.6740407, 6.123622699],
[569.4313421, 5.913067314, 563.2567733, 6.089519297],
[419.540411 , 31.7279367, 569.4313421, 5.913067314]
]

data1 = pd.DataFrame(csv_data, columns=['x1',  'y1',   'xin',  'yin'])

fig = plt.figure(1,figsize=(8,8))

plt.subplot(111)
plt.scatter(data1['x1'], data1['y1'])
plt1 = zoomed_inset_axes(plt.gca(), 2.5)
plt1.plot(data1['xin'], data1['yin']);

我想你想做
缩放的插入轴(plt.gca(),2.5,loc=4)
而不是
缩放的插入轴(plt,2.5,loc=4)
@ThomasKühn,我很感激我们如何使用r在多行(四行)图中的r中做这件事?缩放图形的一部分并将其作为图形中精确给定的插入。如果你能在这方面帮助我,我真的很感激!谢谢,代码是有效的。但是放大的区域具有标签和标题,而不是实际帧。以及如何获得从区域到缩放图的直线,如图所示?我使用此方法获得从区域到缩放图的直线,如图标记插图(plt,plt1,loc1=1,loc2=2,fc=“none”,ec=“0.5”)所示,出现以下错误AttributeError:模块“matplotlib.pyplot”没有属性“transData”
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset

csv_data = [
[797.3266855, 9.518953577, 487.026819, 6.41595323],
[457.3328822, 9.408619701, 493.8012816, 6.352140859],
[321.4279994, 10.99152002, 505.8109589, 6.482765605],
[643.1595144, 11.33567151, 515.0500793, 5.689992589],
[897.9396964, 7.098272377, 523.5118663, 5.062645643],
[658.5927932, 8.401072532, 526.8570713, 5.951114622],
[885.8478465, 9.59502937, 537.6740407, 6.123622699],
[569.4313421, 5.913067314, 563.2567733, 6.089519297],
[419.540411 , 31.7279367, 569.4313421, 5.913067314]
]

data1 = pd.DataFrame(csv_data, columns=['x1',  'y1',   'xin',  'yin'])

fig = plt.figure(1,figsize=(8,8))

plt.subplot(111)
plt.scatter(data1['x1'], data1['y1'])
plt1 = zoomed_inset_axes(plt.gca(), 2.5)
plt1.plot(data1['xin'], data1['yin']);