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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:34:32+00:00 2026-06-17T22:34:32+00:00

I have a class with methods to build some plots. I try to display

  • 0

I have a class with methods to build some plots. I try to display different plots on one figure. The properties (title, legend…) of the figure are always overwritten by the last plot. I expected that if I have return in my method the behaviour would be different to the method without it, but it seems to not be true.

I would like to figure out what difference makes to have return. The code to illustrate my question is:

import matplotlib.pyplot as plt
import numpy as np

class myClass1(object):
    def __init__(self):
        self.x = np.random.random(100)
        self.y = np.random.random(100)

    def plotNReturn1(self):
        plt.plot(self.x,self.y,'-*',label='randNxy')
        plt.title('Plot No Return1')
        plt.legend(numpoints = 1)
    def plotNReturn2(self):
        plt.plot(self.y,self.x,'-x',label='randNzw')
        plt.title('Plot No Return2')
        plt.legend(numpoints = 2)

    def plotWReturn1(self):
        fig = plt.plot(self.x,self.y,'-*',label='randWxy')
        fig = plt.title('Plot With Return1')
        fig = plt.legend(numpoints = 1)
        return fig
    def plotWReturn2(self):
        fig = plt.plot(self.y,self.x,'-x',label='randWzw')
        fig = plt.title('Plot With Return2')
        plt.legend(numpoints = 3)
        return fig


if __name__=='__main__':
    f = myClass1()
    p = plt.figure()

    p1 = p.add_subplot(122)
    p1 = f.plotWReturn1()
    p1 = f.plotWReturn2()
    print 'method with return: %s: ' % type(p1)

    p2 = p.add_subplot(121)
    p2 = f.plotNReturn1()
    p2 = f.plotNReturn2()
    print 'method without return: %s: ' % type(p2)

    plt.show()

The only difference I noticed is the type of the output, but I don’t know what it means in practice.

 method with return: <class 'matplotlib.text.Text'>: 
 method without return: <type 'NoneType'>: 

Is it only about “pythonic” practice or is there anything practical to use any of the style?

  • 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-17T22:34:33+00:00Added an answer on June 17, 2026 at 10:34 pm

    Returning a value has only a direct effect for the caller, in this case your __main__ block. If you don’t need to reuse some value computed by a function, in your case assigned to p1 or p2, the return doesn’t have any impact on behaviour.

    Also, series of assignments like

    p1 = call1()
    p1 = call2()
    p1 = call3()
    

    are indicators of bad code style, because only the last value assigned to p1 is going to be available after them.

    Anyway, I think you want to plot on subplots, as opposed to the main plot, like so:

    import matplotlib.pyplot as plt
    import numpy as np
    
    class myClass1(object):
        def __init__(self):
            self.x = np.random.random(100)
            self.y = np.random.random(100)
    
        def plotNReturn1(self, subplot):
            subplot.plot(self.x,self.y,'-*',label='randNxy')
            subplot.set_title('Plot No Return1')
            subplot.legend(numpoints = 1)
        def plotNReturn2(self, subplot):
            subplot.plot(self.y,self.x,'-x',label='randNzw')
            subplot.set_title('Plot No Return2')
            subplot.legend(numpoints = 2)
    
    
    if __name__=='__main__':
        f = myClass1()
        p = plt.figure()
    
        p1 = p.add_subplot(122)
        f.plotNReturn2(p1)
    
        p2 = p.add_subplot(121)
        f.plotNReturn2(p2)
    
        plt.show()
    

    Here, subplot is passed to each function, so data should be plotted on it, instead of replacing what you’ve plotted before.

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

Sidebar

Related Questions

I have create a database class and packaged some methods. However, once build the
I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden
I have some class structure for the project I build for my company. At
I have a class with only class methods (utility stuff), so my interface is
I have certain PHP class methods that access external variables. These variables are not
I have a class with many methods. How can I modify my methods so
I have - class A { // contains certain set() and get() methods }
Hi I have a class with multiple methods in which I require synchronized blocks
I'm have a class with two methods, A QuickSort() and a ReverseArray(). When I
If I have child class,the child class inherits all methods from the parent,but how

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.