Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:04+00:00 2026-06-11T18:42:04+00:00

I have created a plot in matplot lib and I wish to add an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:42:05+00:00Added an answer on June 11, 2026 at 6:42 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a image plot for this data: sample p p.1 p.2 p.3
I've got a nice facet_wrap density plot that I have created with ggplot2 .
I have created an R script that reads certain data from a file, calls
I have created a graph using core plot. Graph is shown in on the
I have a data.frame called series_to_plot.df which I created by combining a number of
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a function that shows/hides different messages according to a combination of
I have some surface data that is generated by an external program as XYZ
I have created an image plot with ax = imshow() . ax is an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.