White non-gird lines on matplotlib plot

时间:2015-10-29 15:45:41

标签: python python-3.x matplotlib plot grid

I have been working on a project for my math class in which I am creating a Julia set generator. I had finally succeeded in generating it, but when I show the plot and save the image generated there are white lines all over it. They do not line up with x ticks or y ticks. When I view the saved image it had even more white lines. I have been searching to find what these might be, but I have found nothing.

import matplotlib.pyplot as plt
#Set up window with a name and size.
plt.figure("Julia Set Generator by Eric Kapilik", figsize=(7.0,7.0))
#Set Range of axes
plt.xlim([-2,2])
plt.ylim([-2,2])
#Add labels to axes
plt.xlabel("Real Numbers")
plt.ylabel("Imaginary Numbers")
plt.grid(b=False, which = "major", axis = "both")
plt.grid(b=False, which = "minor", axis = "both")

name = input("Save file as... \n")
#Ask for maximum amount of iterations to base colour 
#selection off of with fractions.
max = int(input("What is the maximum amount of iterations you wish to run?     "))

#Generate an array of colour names to be used to plot points. 
#Set seed of array.
colourArray = ["r"]
for colourNum in range (1, max):
    #if the place in array is between 0% and 25% then set the colour to red. 
    #Same method used for other three colours.
    if colourNum >= 0 and colourNum <= (max/4):
        colourArray.append("r") #red
    elif colourNum > (max/4) and colourNum <= (max/2):
        colourArray.append("y") #yellow
    elif colourNum > (max/2) and colourNum <= ((3*max)/4):
        colourArray.append("g") #green
    elif colourNum > ((3*max)/4) and colourNum <= max:
        colourArray.append("c") #cyan

#Get constant value of which the julia set is based off of.
#The real number component is plotted on the horizontal axis
#of a complex number grid so we will use x.
xConstant = float(input("Enter real number constant component: "))
#The imaginary nuber compenent of a complex number is plotted on the vertical axis, 
#so we will use y in our real number grid (for simplicity's sake).
yConstant = float(input("Enter imaginary number constant component: "))

#Title the graph based on the constatn complex number entered.
plt.title(str(xConstant) + " + " + str(yConstant) + "i")




#See the starting coordinates to be tested and conditions
xTest = float(-2)
yTest = float(2)
stop = False
i = 0
xPrevious = xTest
yPrevious = yTest

#Using an escape time algorith, determine the amout of iterations of the recursion
#are needed for the coordinate to be attarcted to infinity.
#Continue doing this while the y value of the coordinate being tested is less
#than or equal to -2.
while yTest >= -2:
    #We are following the recursive function of
    #f(Z-1) = Z^2 + C
    #Where Z is current coordinate, and C is the constant value.
    #Reminder: Both Z and C are actually complex numbers but in our case we
    #are using them both as real number coordinates on a real number grid.
    xCurrent = ((xPrevious**2) - (yPrevious**2)) + xConstant
    yCurrent = (2 * xPrevious * yPrevious) + yConstant

    #Points that surpass a circle of radius 2 with a centre point at the origin
    #are considered to indefinitely escape to infinity.
    #So when the radius of the recursive coordinate based off of the tested coordinate
    #becomes greater or equal to two we know it will be attaracted to infinity.
    radius = xCurrent**2 + yCurrent**2

    #"Is the point an escapee?"
    if radius >= 2:
        #Since the point has been defined as one that esacpes to infintity
        #it is considered an escapee, so set that to true.
        escapee = True

    #"Is the point a prisoner?"
    if i == max:
        #The point is considered a prisoner if max iterations is reached and 
        #the point is still within the circle of radius 2.
        #The testeed point will be considered a prisoner based off of the amount
        #of iterations we selected, it is possible that with more iterations
        #that we would find this to be an escapee.
        prisoner = True

    #If we have not defined what kind of point this is yet, then go to the next
    #iteration of the recursion. i is the number of iterations completed for
    #the test point.
    if escapee == False and prisoner == False:
        i = i + 1
        #Out with the old, in with the new. Set the current points to the previous
        #points for the next iteration.
        xPrevious = xCurrent
        yPrevious= yCurrent

    #If, however, we have defined the point, then colour it based off of
    #the amount of iterations using the array of colours generated at the
    #beginning to select the colour.
    if escapee == True or prisoner == True:
        #This sets the black points that are prisoners, this is the body
        #of the julia set.
        if i == max:
            colourPoint = "k,"
        else:
            #Colour the point and concatenate a ",", which means to plot this point
            #as a pixel.
            colourPoint = colourArray[i] + ","
        #Plot the point! (Most satisfying part)
        plt.plot(xTest, yTest, colourPoint)

        #Determine the percentage finished, to give user an idea of how the
        #renderig is going. (Not nessecary, but appreciable)
        percent = int(((yTest-2)/4) * (-100))
        print(str(percent) + "%")

#After setting a colour and plotting the point, jump to the next test coordinate.
        #Once the end of the line is reached, jump down one.
        if xTest >= 2:
            xTest = -2
            yTest = yTest - 0.01
        else:
            xTest= xTest + 0.01
#Reset the starting conditions.
        i = 0        
        escapee = False
        prisoner = False
        xPrevious = xTest
        yPrevious = yTest

#Show the beauty.
print("100%")
print("Wait for matplotlib to finish things up...\nWill take a minute...")
plt.show()
plt.savefig(name)

Plot that was generated.

Saved image, with even more white lines.

Any suggestions are massively appreciated. Thanks in advanced,

0 个答案:

没有答案