Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating inset in matplot lib

I have created a plot in matplot lib and I wish to add an inset to that plot. The data I wish to plot is kept inside of a dictionary that I use in other figures. I'm finding this data inside a loop which I then run this loop again for the subplot. Here is the relevant segment:

leg = []     
colors=['red','blue']
count = 0                     
for key in Xpr: #Xpr holds my data
    #skipping over what I don't want to plot
    if not key[0] == '5': continue 
    if key[1] == '0': continue
    if key[1] == 'a': continue
    leg.append(key)
    x = Xpr[key]
    y = Ypr[key] #Ypr holds the Y axis and is created when Xpr is created
    plt.scatter(x,y,color=colors[count],marker='.')
    count += 1

plt.xlabel(r'$z/\mu$')
plt.ylabel(r'$\rho(z)$')
plt.legend(leg)
plt.xlim(0,10)
#Now I wish to create the inset
a=plt.axes([0.7,0.7,0.8,0.8])
count = 0
for key in Xpr:
    break
    if not key[0] == '5': continue
    if key[1] == '0': continue
    if key[1] == 'a': continue
    leg.append(key)
    x = Xpr[key]
    y = Ypr[key]
    a.plot(x,y,color=colors[count])
    count += 1
plt.savefig('ion density 5per Un.pdf',format='pdf')

plt.cla()

The strange thing is that when I tried to move the inset position, I still get the previous insets (those from the previous run of the code). I even tried to comment out the a=axes([]) line without any apparent. I attach en example file. Why is it acting in that way? A figure of the crooked output

like image 751
Yotam Avatar asked Sep 07 '12 20:09

Yotam


People also ask

What is an inset axis?

Cover image by geralt on Pixabay. Inset axes are a fantastic (and often underutilised) tool in Matplotlib. They can be used to: Zoom in on specific parts of plots to show them in greater detail. Replace empty space in a plot with additional information.

How do you zoomed a portion of image and insert in the same plot in MatPlotLib?

MatPlotLib with Python Create x and y points, using numpy. To zoom a part of an image, we can make data for x and y points, in that range. Plot x and y points (Step 1), using the plot() method with lw=2, color red and label. Use the legend() method to place text for the plot, Main curve.

What is fig and ax in PLT?

fig : The matplotlib. pyplot. figure object to be used as a container for all the subplots. ax : A single object of the axes. Axes object if there is only one plot, or an array of axes.


1 Answers

The simple answer is you should use plt.clf() which clears the figure, not the current axes. There is also a break in the inset loop which means none of that code will ever run.

When you start to do more complicated things than use a single axes, it is worth switching over to using the OO interface to matplotlib. It may seem more complicated at first, but you no longer have to worry about the hidden state of pyplot. Your code can be re-written as

fig = plt.figure()
ax = fig.add_axes([.1,.1,.8,.8]) # main axes
colors=['red','blue']
for key  in Xpr: #Xpr holds my data
    #skipping over what I don't want to plot
    if not key[0] == '5': continue 
    if key[1] == '0': continue
    if key[1] == 'a': continue
    x = Xpr[key]
    y = Ypr[key] #Ypr holds the Y axis and is created when Xpr is created
    ax.scatter(x,y,color=colors[count],marker='.',label=key)
    count += 1

ax.set_xlabel(r'$z/\mu$')
ax.set_ylabel(r'$\rho(z)$')
ax.set_xlim(0,10)
leg = ax.legend()

#Now I wish to create the inset
ax_inset=fig.add_axes([0.7,0.7,0.3,0.3])
count =0
for key  in Xpr: #Xpr holds my data
    if not key[0] == '5': continue
    if key[1] == '0': continue
    if key[1] == 'a': continue
    x = Xpr[key]
    y = Ypr[key]
    ax_inset.plot(x,y,color=colors[count],label=key)
    count +=1

ax_inset.legend()
like image 179
tacaswell Avatar answered Sep 30 '22 12:09

tacaswell