Why won't my matplotlib appear?

So I made a custom module that uses matplotlib and when I try to use that module, it doesn’t show. When I put the code in the same code as the module then it runs, how do I fix this?

Module code

import numpy as np
import time
from matplotlib.patches import Rectangle, Circle, Polygon

plt.ion()

fig, ax = plt.subplots()

def set_map():
    print("map")
    ax.clear()
    ax.plot([0, 64], [0, 0], color="k")
    ax.plot([0, 0], [0, 64], color="k")
    ax.plot([0, 64], [64, 64], color="k")
    ax.plot([64, 64], [0, 64], color="k")
    
    ax.set_aspect('equal', adjustable='box')
    plt.axis("off")
    plt.draw()
    plt.pause(0.01)
    
def start(init, update, draw):
	
	init()
	
	while True:
		draw()
		update()
		time.sleep(0.1)
	plt.show()
	

Code


def init():
	global x
	x = 12
	
def update():
	global x
	x = 12
def draw():
	print("draw")
	plg.set_map()
	
plg.start(init, update, draw)```

I'm using pydroid on android if that matter

Well, the code won’t work as written because in the module code plt is referred to without being defined and in the main code plg isn’t defined.

I think the problem you’re seeing is that in start, plt.show() is after the infinite loop.

Hi,

I noticed that you have imported these two modules without actually using them in your script:

import numpy as np
from matplotlib.patches import Rectangle, Circle, Polygon

If you will not be using them, then there is no reason to have them clutter your script.

Based on your script, what you really want is this:

import matplotlib.pyplot as plt

This allows you to create this (among other use cases in your script):

fig, ax = plt.subplots()

Another discrepancy is that you have x defined as a global variable within two functions yet it is not being used outside of them. When you define a variable as global within functions it implies that they are being used / modified in the global namespace (outside the functions).

You should also define the functions init, update, and draw before the start function.

Plt thing is I just copied so badly so the
import matplotlib.pyplot as plt
And the plt.show being part of the loop doesn’t help. The thing is when I run the code it just outputs draw and map, but doesn’t show the graph, so Im guessing it’s showing the wrong window or smth, cause the code works if I put it in the same code as the module

Temporary variables and I was planning to use the modules later