Issue
Since the code is a lot of lines, I shall first show what the issue is:
I defined a simple loop and am getting the appropriate results.
Here when I attempt to plot it using matplotlib, the range shown on the x-axis is different from the range I inputted. I want 0 to 100 with a step size of 5 but I am getting 0 to 17.5 with a step size of 2.5.
Is there any issue with just the way I have coded this? If not, here is the rest of the code, thank you!:
import random
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.colors import ListedColormap
import sys
import decimal
sys.setrecursionlimit(4000)
n = 10 # number of rows and columns in the grid
p = 0.9 # probability that each square is open
def gridMakerN(n):
grid = (np.random.rand(n,n) < p).astype(int)
mycolormap = ListedColormap(["grey","blue"])
#plt.imshow(grid, cmap=mycolormap)
return grid
# define an exception that we will raise if percolation is detected
class percolationException(Exception): pass
# query() looks for a path from (row, col) to the bottom of the grid
# recursive function: it calls itself to determine if a path exists
def query(row, col, grid, visited):
#print("Visiting square ", row, ",", col) <- This was previously part of the code
# mark row, col as visited
visited[row,col] = 1
# is row equal to the bottom row? If so, output "path found"
(numRows,numCols) = np.shape(grid)
if row == numRows - 1:
#print("PERCOLATION FOUND!!!") <- This was previously part of the code
raise percolationException
else:
# if square below is open and unvisited, then is there a path from that square?
if grid[row+1,col] == 1 and visited[row+1,col] == 0:
query(row+1, col, grid, visited)
# if square at left is open and unvisited, then is there a path from that square?
if col > 0 and grid[row, col-1] == 1 and visited[row, col-1] == 0:
query(row, col-1, grid, visited)
# if square at right is open and unvisited, then is there a path from that square?
if col+1 < numCols and grid[row, col+1] == 1 and visited[row, col+1] == 0:
query(row, col+1, grid, visited)
# if square above is open and unvisited, then is there a path from that square?
if row > 0 and grid[row-1, col] == 1 and visited[row-1, col] == 0:
query(row-1, col, grid, visited)
# driver function to manage the whole percolation detection process
def findPercolation(grid):
# create an empty visited matrix
(numRows, numCols) = np.shape(grid)
visited = np.zeros( (numRows, numCols) )
# look for a percolation path, starting at each open square in the top row
try:
for c in range(numCols): # consider all squares in the top row
if grid[0,c] == 1:
query(0, c, grid, visited)
except percolationException:
#print("percolationException occurred") <- This was previously part of the code
return 1 # <- Here I put 1 instead of "True"
else:
#print("percolation not found") <- This was previously part of the code
return 0 # <- Here I put 0 instead of "False"
def findPercolationFixedP(n):
return findPercolation(gridMakerN(n))
def percAvgFixedP(n):
iterations = 100
results = [] #Making an Empty List
for _ in range(iterations): #Repeat the Same Step x times
results.append(findPercolationFixedP(n))
#print(results)
#print(sum(results))
return sum(results)/iterations
def avgFixedPGraph():
results = []
for x in range(10,100,5):
results.append(percAvgFixedP(x))
plt.plot(results,"c")
plt.grid()
plt.show()
avgFixedPGraph()
Solution
When plot()
is only given one array:
plt.plot(results, "c")
that array is treated as the y
values, and the x
values default to a numeric range. In this case results
has 18 values, so it plots x
from 0 to 17.
To assign custom x
values, pass them in explicitly, e.g.:
x = range(10, 100, 5)
results = [percAvgFixedP(value) for value in x]
plt.plot(x, results, "c")
Answered By - tdy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.