Home > other >  matplotlib plot not showing if called as a function:
matplotlib plot not showing if called as a function:

Time:08-07

I have the following code which sort of works in that when the function is called I get the three plots that I have hard-coded show up:

def Function(n):
    def f_x(n):
        Code
        return output_0, output_1
    def x_t(n):
        Code
        return output_0, output_1
    def z(n):
        Code
        return output_0, output_1, output_2

    plt.subplot(221)
    plt.title("f(x)")
    plt.xlabel(x)
    plt.ylabel(y)
    plt.scatter(x_list,f_x(n)[1])
 
    plt.subplot(223)
    plt.title("t(x)")
    plt.xlabel(x)
    plt.ylabel(t),
    plt.scatter(x_t(n)[0],x_t(n)[0])
  
    plt.subplot(222)
    plt.title("z(t)")
    plt.xlabel("t")
    plt.ylabel("z(t)")
    plt.scatter(x_t(n)[0], z(n)[2])

    plt.tight_layout()

but I don't want to get the plot every time I run the function to get my values (which is useless, most of the time, anyways), so I did the following:

def Function(n):
    def f_x(n):
        Code
        return output_0, output_1
    def x_t(n):
        Code
        return output_0, output_1
    def z(n):
        Code
        return output_0, output_1, output_2

    def plot(n): 
        def f_plot(n):
            plt.subplot(221)
            plt.title("f(x)")
            plt.xlabel(x)
            plt.ylabel(y)
            plt.scatter(x_list,f_x(n)[1])
 
            plt.subplot(223)
            plt.title("t(x)")
            plt.xlabel(x)
            plt.ylabel(t),
            plt.scatter(x_t(n)[0],x_t(n)[0])
  
            plt.subplot(222)
            plt.title("z(t)")
            plt.xlabel("t")
            plt.ylabel("z(t)")
            plt.scatter(x_t(n)[0], z(t)(n))

            plt.tight_layout()
        return f_plot(n)


    return plot(n), output_2_1, output_2_2, output_3

Unfortunately, what i wrote above doesn't work and I don't know what the issue is. By the way, I also tried to add an extra bit of code to sort of force publish the graph but that didn't work either:

print(Function(n)[0])

Also tried,

plt.show()

The first one shows the plot both with and without plt.show() while the second one doesn't both with and without it. One more thing, I checked each subfunction within the main function to see whether those functions are problematic byt the following codes:

len(x_t(n)[i]), type (x_t(n)[i], etc. To make sure the outputs are:

  1. Lists
  2. Dimentionally equally for each domain and co-domain.

Fortunately, they were all good, and, hence, I narrowed down the issue to matplotlib code.

CodePudding user response:

I'm a bit at a loss, but you could try rewriting it like this, using the object-oriented matplotlib interface. This means you won't have any weird variable scoping issues, since instead of explicitly creating the Figure in the background, you explicitly move it out of the nested function. The output may look a little different with the subplots call, but I assume you can adjust once you have anything visible.

def Function(n):
    def f_x(n):
        Code
        return output_0, output_1
    def x_t(n):
        Code
        return output_0, output_1
    def z(n):
        Code
        return output_0, output_1, output_2

    def plot(n): 
        def f_plot(n):
            fig, axes = plt.subplots(2, 2)

            ax = axes[0,0]
            ax.set_title("f(x)")
            ax.set_xlabel(x)
            ax.set_ylabel(y)
            ax.scatter(x_list,f_x(n)[1])  # I assume x_list is defined somewhere
 
            ax = axes[0, 1]
            ax.set_title("t(x)")
            ax.set_xlabel(x)
            ax.set_ylabel(t),
            ax.scatter(x_t(n)[0],x_t(n)[0])
  
            ax = axes[1, 0]
            ax.set_title("z(t)")
            ax.set_xlabel("t")
            ax.set_ylabel("z(t)")
            ax.scatter(x_t(n)[0], z(t)(n))

            fig.tight_layout()
            return fig
        return f_plot(n)


    return plot(n), output_2_1, output_2_2, output_3

fig, output1, output2, output3 = Function(n)
fig.show()

CodePudding user response:

I don't really understand why to define a function inside other functions, maybe you intended you use class?

Then the modified code would look like this:

import matplotlib.pyplot as plt
class Function():
    def __init__(self):
        pass
    def f_x(n,self):
        Code
        return output_0, output_1
    def x_t(n,self):
        Code
        return output_0, output_1
    def z(n,self):
        Code
        return output_0, output_1, output_2

    def f_plot(n,self):
        plt.subplot(221)
        plt.title("f(x)")
        plt.xlabel(x)
        plt.ylabel(y)
        plt.scatter(x_list,self.f_x(n)[1])
 
        plt.subplot(223)
        plt.title("t(x)")
        plt.xlabel(x)
        plt.ylabel(t),
        plt.scatter(self.x_t(n)[0],self.x_t(n)[0])
  
        plt.subplot(222)
        plt.title("z(t)")
        plt.xlabel("t")
        plt.ylabel("z(t)")
        plt.scatter(self.x_t(n)[0], self.z(t)(n))

        plt.tight_layout()

Seeing you defined f_plot() inside plot() I'm also assuming you want to plot multiple other graphs. If so, then you could define a plot() Class inside the Function() Class (I forgot if that works though, if it doesn't work just define another function in the Function class, but I remember it should work)

To call the functions, you would need to initialise the Function() Class like:

function = Function()

And then to call the other functions you would need to do something like:

function.f_x(n) #You would input something here!
function.x_t(n)
function.z(n)
function.f_plot(n)

Again, it could be a bug with your IDE or something, you could also try saving the graph somewhere in your device using plt.savefig("FileName.png")

  • Related