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 7770751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:29:08+00:00 2026-06-01T16:29:08+00:00

I am righteously confused with the coding paradigms offered by matplotlib. I am using

  • 0

I am righteously confused with the coding paradigms offered by matplotlib. I am using code like this below to plot some data:

fig=plt.figure(figsize=fig_size)  # plt=pyplot defined above
axes1 = fig.add_subplot(111)
axes1.plot(temp, depth, 'k-')
axes1.set_ylim(-600,0)
axes1.set_ylabel(r'Depth $(m)$')
axes1.set_xlim(0,80)
axes1.set_xlabel(r'Temperature (\textcelsius)')
axes1.set_xticks(np.arange(0,100,20))
axes1.grid(True)
plt.savefig(savedir + 'plot.svg', transparent=True)

I’d rather use mpl’s object-oriented style than the pylab convenience functions. So question is if I only want to plot one curve, non-interactively, am I using the right figure creation style? (lines 1 & 2). It seems like a lot of separate calls are needed to format the axis labels and so on.

  • 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-01T16:29:09+00:00Added an answer on June 1, 2026 at 4:29 pm

    What you’re doing looks fine. (And I agree, it’s much cleaner to only use pyplot for figure creation and use the OO API for everything else.)

    If you’d prefer to make the figure and axes in one call, use plt.subplots.

    Also, I find it’s a bit cleaner to use fig.savefig instead of plt.savefig. It won’t matter in this case, but that way you avoid having to worry about which figure is “active” in the state-machine interface.

    For one last thing, you could set the x and y limits with a single call to axes1.axis(...). This is purely a matter of preference. set_xlim and set_ylim are arguably a more readable way of doing it.

    The “setters” and “getters” are annoying, but date from when python didn’t have properties, if I recall correctly. They’ve been kept as the main methods partly for backwards compatibility, and partly so that “matlab-isms” like plt.setp are easier to write. In fact, if you wanted you could do

    plt.setp(ax, xlabel='Xlabel', ylabel='Ylabel', xticks=range(0, 100, 20))
    

    This avoids having to do three separate calls to set the xlabel, ylabel, and xticks. However, I personally tend to avoid it. I find it’s better to be slightly more verbose in most cases. If you find it cleaner or more convenient, though, there’s nothing wrong with using setp.

    As an example of how I’d write it:

    import matplotlib.pyplot as plt
    import numpy as np
    
    depth = np.linspace(-600, 0, 30)
    temp = (4 * np.random.random(depth.size)).cumsum()
    
    fig, ax = plt.subplots() 
    ax.plot(temp, depth, 'k-')
    ax.axis([0, 80, -600, 0])
    ax.set_ylabel(r'Depth $(m)$')
    ax.set_xlabel(r'Temperature $(^{\circ}C)$')
    ax.set_xticks(np.arange(0, 100, 20))
    ax.grid(True)
    
    fig.savefig('plot.svg', transparent=True)
    plt.show()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi can anyone tell me how to make this javascript code show full screen
I have this method which return an array of string : private static string[]
Switch(some case) { case 1: // compute something ... return something; break; case 2:
I'm trying to solve problem 14 on project euler, and my initial brute force
I'm an old (but not too old) Java programmer, that decided to learn C++.
You and I want to be the expert on computer programming or website design,
When assigning a default default-value to a field (here false to a bool), FxCop
Currently building a WordPress theme and the wpautop function is quite a pain to
I have a question about righteous way of programming in Python... Maybe there can

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.