Place a plot in a figure

Hello, I hope that someone would be willing to give a complete newbie some help on how to get MatPlot to plot the below code properly. I am not a scientist or programmer, I am just trying to do something way outside my abilities. I want to replace the print() function with Matplotlib and have the output be visible within the figure generated by Matplotlib.

Thank you anyone for your help in advance! Nick

Here is the code:

# Python 3.0+ and 2.5+
try:
    from functools import reduce
except:
    pass
 
 
def mandelbrot(a):
    return reduce(lambda z, _: z * z + a, range(50), 0)
 
def step(start, step, iterations):
    return (start + (i * step) for i in range(iterations))
 
rows = (("*" if abs(mandelbrot(complex(x, y))) < 2 else " "
        for x in step(-2.0, .0315, 80))
        for y in step(1, -.05, 41))
 
print("\n".join("".join(row) for row in rows))

Do you have a sample figure? the ax.text method will print text to a matplotlib figure, but since you always have either “*” or “_”, ax.scatter(marker='*') should also do the trick

Hello, thank you so much for your reply. I will post pictures of the two different figures for you shortly.

Hello story645,
I placed ax.scatter(marker=’*’) into program, but it returned that ax was not defined.

Here is an image,

Mandelbrot#2 is the image where I tried to use Matplot. I was not allowed to upload two images, but the program above will generate the image Thank you for your interest! Nick

Please show your code. It looks like you have a bunch of strings and they are showing up as tick labels.

Thank you for your reply, I have shown the original code at the top of this thread along with the disclaimer that I really don’t know what I am doing. I just would like to replace the print() function with the pyplt() function from Matplotlib. I understand this function a little bit better. The original code (not written by me) is also at the top of this thread. The code below is my poor attempt to use the pyplt() to plot the Mandelbrot series.

Python 3.0+ and 2.5+

try:
from functools import reduce
except:
pass
import matplotlib.pyplot as plt

def mandelbrot(a):
return reduce(lambda z, _: z * z + a, range(50), 0)

def step(start, step, iterations):
return (start + (i * step) for i in range(iterations))

rows = (("*" if abs(mandelbrot(complex(x, y))) < 2 else " " for x in step(-2.0, .0315, 80)) for y in step(1, -.05, 41))

print("\n".join("".join(row) for row in rows))

def mandelbrot(a):
return reduce(lambda z, _: z * z + a, range(50), 0)

def step(start, step, iterations):
return (start + (i * step) for i in range(iterations))

rows = (("*" if abs(mandelbrot(complex(x, y))) < 2 else " " for x in step(-2.0, .0315, 80)) for y in step(1, -.05, 41))

#print("\n".join("".join(row) for row in rows))

plt.plot("\n".join("".join(row) for row in rows)) plt.show()

plt.plot will not magically do something. If you are trying to learn something new, it is helpful to think about what you are trying to achieve and describe it, and then maybe do some googling. In this case, what do you want the result to look like? For what it is worth the Mandelbrot set is one of the most plotted things on the web - I would be shocked if you could not find a ready made example in matplotlib.

Thank you for your reply. Your are right it is. That is how I got into the quick sand, so to speak. This is the image that I would like to be able to plot and save using pyplt().

Any help would be very much appreciated! Nick