Zooming and plotting a inset plot

6.1k Views Asked by At

I am trying to do a zoomed in inset plot like the image:

The first part of the code is working i.e. it is plotting the file. Only when I try to plot the zoomed part it gives the following error I have trying to figure it out but none of the posts have been really helpful.

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'

This is the code I have been using.

import numpy as np
import matplotlib as mpl
import pandas as pd
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from matplotlib.ticker import MaxNLocator
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset


file1 = 'inset_trial.dat'

data1 = pd.read_csv(file1, delimiter='\s+', header=None, engine='python')
data1.columns = ['x1',  'y1',   'xin',  'yin']

def fit_data():

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

    plt.subplot(111)
    mpl.rcParams['figure.dpi']=200
    plt.scatter(data1['x1'], data1['y1'],  marker='o', color='red', edgecolor='red', s=100)

    plt1 = zoomed_inset_axes(plt, 2.5,  loc=4 )
    plt1.plot(data1['xin'], data1['yin'])#,marker='o', color='blue', edgecolor='blue', s=100)
    plt1.axis([5.062645643, 6.482765605, 487.026819, 569.4313421])
    plt1.set_yticks([])
    plt1.set_xticks([])
    plt1.set_axis_bgcolor('none')
    axes = mark_inset(axins, axins_2, loc1=2, loc2=4, fc="none", ec="0.5")

    plt.minorticks_on()

plt.tick_params(axis='both',which='minor',length=5,width=2,labelsize=28)
    plt.tick_params(axis='both',which='major',length=8,width=2,labelsize=28)
    plt.tick_params(direction='out', length=8, width=3)
    plt.tick_params(labelsize=28) 

    plt.show()
    plt.close()

fit_data()

The sample data I have been trying to fit is

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 
1

There are 1 best solutions below

2
On

The comment by Thomas Kühn is correct. But by making that change your code will still fail to run. Below I've reproduced what I think is an example of what you want.

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']);