Python Matplotlib Graph Showing Incorrect Range in X axis - python

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()

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")

Related

How to find a tour in a graph, that visits every node once, considering the condition that every enclosed angle in the tour is larger than 90°?

The input is a undirected, unweighted graph with about 80 nodes (or to be more specific: the coordinates of the nodes in a txt file), in which a route is to be found that visits each node once, but does not use any vertex twice. Furthermore, the included angle between each two vertexes of the route should be greater than 90°. On the left side of the following picture you can see an angle that is not wanted in the route, in contrast to the angle on the right side:
Furthermore, start and end points of the route do not have to be identical. The route does not have to be the shortest one but should be as short as possible.
Here's what I've tried so far:
Considering there are 80 nodes in the graph it would be impossible to use a depth-first-search or backtracking algorithm because it would just take too long. Instead, I've implemented a greedy-algorithm, which always makes the best decision at the time of the decision. It works well for most of the examples, however, some are just impssoible to solve for that type of algorithm. Here's my whole code, which uses the "read_coordinates(file)" function to import the coordinates (x- and y-coordinates of a point line by line). After doing that, the function "greedy_approach(coordinates)" tries to find a route.
import matplotlib.pyplot as plt
import numpy as np
import math
import matplotlib.animation as animation
def read_coordinates(file):
with open(file, "r") as f:
lines = f.readlines()
coordinates = []
for line in lines:
x, y = map(float, line.strip().split())
coordinates.append((x, y))
return coordinates
def check_angle(v1, v2):
cos_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
if cos_angle > 1:
cos_angle = 1
elif cos_angle < -1:
cos_angle = -1
angle = math.acos(cos_angle)
return abs(angle) < math.pi / 2
def greedy_approach(coordinates):
all_visited = set(range(len(coordinates)))
overall_best_route = []
for start in range(len(coordinates)):
route = [coordinates[start]]
visited = {start}
unvisited = all_visited - visited
while unvisited:
best_route = None
best_distance = float("inf")
for i in unvisited:
point = coordinates[i]
# Vektor 1
v1 = np.array(route[-1]) - np.array(point)
if len(route) > 1:
# Vektor 2
v2 = np.array(route[-2]) - np.array(route[-1])
else:
v2 = np.array([1, 0])
if check_angle(v1, v2):
temp_route = route + [point]
temp_distance = route_length(temp_route)
if temp_distance < best_distance:
best_route = temp_route
best_distance = temp_distance
if not best_route:
break
route = best_route
visited.add(coordinates.index(route[-1]))
unvisited = all_visited - visited
if len(route) > len(overall_best_route):
overall_best_route = route
if len(visited) == len(coordinates):
return overall_best_route
return overall_best_route
# return None
def plot_route(coordinates, route):
x_coo = coordinates[:, 0]
y_coo = coordinates[:, 1]
x = [p[0] for p in route]
# x = [p[0] for p in route]
y = [p[1] for p in route]
fig, ax = plt.subplots()
ax.plot(x_coo, y_coo, 'o')
line, = ax.plot(x[:1], y[:1], '-')
def update(num):
num += 1
if num >= len(x) + 1:
ani.event_source.stop()
return line,
line.set_data(x[:num], y[:num])
return line,
ani = animation.FuncAnimation(fig, update, frames=len(x)+1, interval=100, blit=False)
ax.plot(x[0], y[0], 'go')
ax.plot(x[-1], y[-1], 'ro')
plt.show()
if __name__ == "__main__":
file = 'X:\coordinates6.txt'
coordinates = read_coordinates(file)
route = greedy_approach(coordinates)
plot_route(np.array(coordinates), route)
It goes without saying that you need to change the variable "file" accoring to the path, where you've saved the txt-file. In the following I've included the txt-file of such an "unsolveable" graph:
102.909291 60.107868
-89.453831 162.237392
64.943433 -119.784474
121.392544 56.694081
-107.196865 -77.792599
20.218290 88.031173
202.346980 -189.069699
143.114152 -135.866707
-144.887799 -73.495410
92.255820 -93.514104
-55.091518 198.826966
228.929427 82.624982
96.781707 141.370805
154.870684 140.327660
112.833346 -38.057607
14.005617 -14.015334
138.136997 -31.348808
73.689751 110.224271
100.006932 76.579303
120.906436 131.798810
21.067444 122.164599
49.091876 150.678826
85.043830 108.946389
-194.986965 101.363745
152.102728 -193.381252
238.583388 -133.143524
151.432196 121.427337
221.028639 -139.435079
-139.741580 57.936680
-72.565291 -24.281820
155.405344 -56.437901
58.019653 49.937906
277.821597 104.262606
19.765322 -99.236400
246.621634 101.705861
289.298882 56.051342
172.836936 59.184233
132.794476 135.681392
155.341949 -20.252779
134.692592 -102.152826
-97.391662 124.120512
245.415055 44.794067
255.134924 115.594915
83.005905 64.646774
245.020791 -167.448848
-102.699992 95.632069
-4.590656 -40.067226
-191.216327 -162.689024
210.186432 -127.403563
-51.343758 -57.654823
187.669263 -122.655771
121.661135 85.267672
46.674278 -193.090008
-189.988471 -98.043874
-175.118239 77.842636
-187.485329 -177.031237
56.716498 66.959624
-18.507391 -22.905270
-167.994506 138.195365
81.740403 10.276251
-19.310012 -131.810965
157.588994 -144.200765
40.327635 19.216022
-126.569816 -30.645224
150.526118 -88.230057
76.647124 -7.289705
231.944823 82.961057
58.716620 32.835930
-288.744132 -173.349893
-293.833463 -165.440105
-31.745416 -69.207960
175.677917 98.929343
216.825920 -152.024123
21.176627 -165.421555
-100.569041 140.808607
-90.160190 -25.200829
242.810288 -182.054289
-154.225945 -135.522059
102.223372 174.201904
64.559003 82.567627
I would really appreciate it if you could have a look into that problem :)
Every node has a potential edge to every other node. ( It might be possible to optimize things by specifying a cut-off distance so that two nodes that are far apart will not create an edge )
Now you need to split nodes so that you end up with nodes that only have two edges connecting them.
Now set a "cost" for each node which is 1 if the angle between the edges is more than the cut-off and "infinite" if smaller
Transform the nodes into edges ( with the cost ) and the edges into nodes.
Run the spanning tree algorithm on this graph.
This diagram shows how to split nodes so that they have only two ( real ) edges.
The red edges will have zero cost and be ignored when calculating the angle.

Python list not appending & variable not defined

I am having one small problem with my program and as I am very new to Python am struggling to find a solution. I have googled about a bit but nothing seemed to work from what I tried. My code is the following:
from PIL import Image
import numpy as np
from scipy import misc
from pandas import *
def maze_gen():
##Opens the Image
im_file = Image.open('15x15.png')
##Reads the image pixel information
arr = misc.imread('15x15.png')
##Sets the width, height and maze size variables
width = im_file.size[0]
height = im_file.size[1]
size = width * height
##Defines the mapping, start and end points array
map = np.zeros([width, height], dtype=np.int)
start_pos = np.empty(shape=[1,2])
end_pos = np.empty(shape=[1,2])
##Search image replacing white pixels with 1's in the mapping array
for x in range(width):
for y in range(height):
if 255 in arr[x,y]:
map[x,y] = 1
##Find the start and end locations
for x in range(width):
if map[0,x] > 0:
start_pos = 0,x
for x in range(width):
if map[height -1 ,x] > 0:
end_pos = height -1 ,x
return width, height, size, map, start_pos, end_pos
def check_neighbours(col, row):
width, height, size, map, start_pos, end_pos=maze_gen()
neighbours = list()
##Debugging to check cell_current values are correct
print('col =', col)
print('row =', row)
if (col >= 0 and col < height and row >= 0 and row < width):
neighbours.append((col, row))
print('Neighbours', neighbours, '\n')
if (len(neighbours) > 0):
return neighbours
else:
return None
def solver():
width, height, size, map, start_pos, end_pos=maze_gen()
##Sets cell_current to the starting position
cell_current = start_pos
path = list()
path.append((cell_current))
check_neighbours(cell_current[0]-1, cell_current[1]) ##Top neighbour
check_neighbours(cell_current[0], cell_current[1]+1) ##Right neighbour
check_neighbours(cell_current[0]+1, cell_current[1]) ##Bottom neighbour
check_neighbours(cell_current[0], cell_current[1]-1) ##Left neighbour
print('Neighbours in Solver', neighbours, '\n')
def debug():
width, height, size, map, start_pos, end_pos=maze_gen()
##Prints maze information for debugging
print ('Maze width:', width)
print ('Maze height:', height)
print ('Maze size:', size, '\n')
##Print Start and End points
print ('Start Point')
print (start_pos, '\n')
print ('End Point')
print (end_pos, '\n')
##Prints mapping array for debugging
print ('Mapping Array')
print (DataFrame(map), '\n')
if (__name__ == "__main__"):
solver()
The issues I am running into are the list neighbours isn't being added to with each successful run through of the if statement. Instead of getting the following readout:
col = -1 row = 1 Neighbours []
col = 0 row = 2 Neighbours [(0, 2)]
col = 1 row = 1 Neighbours [(1, 1)]
col = 0 row = 0 Neighbours [(0, 0)]
Any help on this problem would be amazing. Please do bare in mind that I am very new to python so any stupid mistakes or things I could change to make the code better please do say!
You need to make two changes in your code:
Make neighbours variable global.
Replace neighbours.append((col, row)) with neighbours.append([col, row]). This is because you can only append a variable or a list to an existing list.
Hope this helps.

K-Means clustering multidimensional data with a heatmap

I have been trying to implement k-means clustering with a heatmap, but have been unsuccessful.
Here is the initial data set:
https://raw.githubusercontent.com/gsprint23/cpts215/master/progassignments/files/simple.csv
And here is my code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math
import random
#%matplotlib inline
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
def chooseCenter(data, centers):
length = data.shape
cent = []
while len(cent) < centers :
x = random.randrange(0,length[0])
y = random.randrange(0,length[1])
if data.iloc[x][y] not in cent:
d = truncate(data.iloc[x][y],2)
cent.append(d)
return cent
def distance(val, center):
return math.sqrt((val- center)**2)
def getDistances(centers, data):
length = data.shape
dist = []
for i in range(length[0]):
for j in range(length[1]):
y = []
for k in range(len(centers)):
val = distance(data.iloc[i][j], centers[k])
y.append(truncate(val,3))
dist.append(y)
return dist
def findClosest(data, dist):
close = data.copy()
length = close.shape
indexes = []
for i in range(len(dist)):
pt = min(dist[i])
idx = dist[i].index(pt)
indexes.append(idx)
#print(indexes)
length = data.shape
n = np.array(indexes)
n = pd.DataFrame(np.reshape(n, (length[0],length[1])))
#reshape this data frame into the same shape as the data
#keep running the find closest until there is no change
#try heatmap on this?
#this should cluster it, but to make sure test it
#might need to do some tweaking to this
return n
# for i in range(length[0]):
# for j in range(length[1]):
# print('dist[i]', dist[j])
# pt = min(dist[j])
# print(pt)
# idx = dist[j].index(pt)
# close.iloc[i][j] = int(idx)
#return close
def computeNewCenter(data, close):
d = dict()
for i in range(len(close)):
for j in range(len(close[0])):
d[close.iloc[i][j]] = []
for i in range(len(data)):
for j in range(len(data[0])):
if close.iloc[i][j] in d:
d[close.iloc[i][j]].append(data.iloc[i][j])
newCenters = []
for key, value in d.items():
m = np.mean(value)
newCenters.append(truncate(m, 3))
return newCenters
# lst = [[] * numcenters]
# for i in range(len(close)):
# for j in range(len(close[0])):
# if close.iloc[i][j]
def main():
data = np.array(pd.read_csv('https://raw.githubusercontent.com/gsprint23/cpts215/master/progassignments/files/simple.csv', header=None))
data = data.T
#print(data)
df = pd.DataFrame(data[1:], columns=data[0], dtype=float).T
df = df.iloc[::-1]
# print(df)
# print(df.iloc[1][9])
# print(df)
# print(df.iloc[0][1])
# heatmap = plt.pcolor(df, cmap=plt.cm.bwr)
# plt.colorbar(heatmap)
c = chooseCenter(df, 3)
print(c)
#print(len(c))
dist = getDistances(c, df)
#print(dist)
y = findClosest(df, dist)
# q = []
# for i in range(len(c)):
# q.append([])
# #print(q)
j = computeNewCenter(df, y)
#print(j)
length = df.shape
oldFrame = pd.DataFrame(np.ndarray((length[0],length[1])))
oldFrame = oldFrame.fillna(0)
ct=0
while y.equals(oldFrame) == False:
ct+=1
oldFrame = y.copy()
c = computeNewCenter(df, oldFrame)
#print(c)
dist = getDistances(c, df)
#print(dist)
y = findClosest(df, dist)
#print(y)
#plt.pcolor(df, cmap=plt.cm.bwr)
l = []
for i in range(len(y)):
for j in range(len(y[0])):
if y.iloc[i][j] == 1:
l.append(df.iloc[i][j])
for i in range(len(y)):
for j in range(len(y[0])):
if y.iloc[i][j] == 2:
l.append(df.iloc[i][j])
for i in range(len(y)):
for j in range(len(y[0])):
if y.iloc[i][j] == 0:
l.append(df.iloc[i][j])
l = np.ndarray((length[0],length[1]))
l = pd.DataFrame(l)
print(l)
hm = plt.pcolor(l, cmap=plt.cm.bwr)
plt.colorbar(hm)
# print(y)
# print(c)
# print(ct)
#plt.pcolor(y, cmap=plt.cm.bwr)
if __name__ == '__main__':
main()
My line of thinking was this:
My current thought process was to first randomly choose the centers.
Then create a list of lists for each point for the distance to each center.
Find the index of the minimum distance for each point for each center.
Create a data frame of the same size as the data set and fill each index for each element with the index of the center the point is closest to.
Recompute the center by taking the mean of the points with the same center index
Repeat this process multiple times until the index data frame does not change.
Create a new data frame and add the points which have the same center point close together in the frame.
Then create the heatmap.
This did not seem to work though.
Just wondering, am I on the right track or am I completely off, and if I am on the right track which parts would I need to change in order to fix the issue. If not could you please point me on the right track.
Here is a comparison of the maps:
Here are the maps
The first one is the one my program generated while the second is the way it is supposed to look.
I know my problem lies in some part of the k-means clustering algorithm, and my guess is it is either in the reassignment stage where you reassign the points to the centroids and calculate the new centroids or in the stopping condition in that the algorithm does not run long enough. Also in the back of my head, something tells me that I am not doing this as efficiently as I could have and that I am missing something key. I have watched several videos on K-means clustering and understand it conceptually, I'm just having a hard time implementing it.

Moving window averages on unequal dimensions

TL;DR: Is there anyway I can get rid of my second for-loop?
I have a time series of points on a 2D-grid. To get rid of fast fluctuations of their position, I average the coordinates over a window of frames. Now in my case, it's possible for the points to cover a larger distance than usual. I don't want to include frames for a specific point, if it travels farther than the cut_off value.
In the first for-loop, I go over all frames and define the moving window. I then calculate the distances between the current frame and each frame in the moving window. After I grab only those positions from all frames, where both the x and y component did not travel farther than cut_off. Now I want to calculate the mean positions for every point from all these selected frames of the moving window (note: the number of selected frames can be smaller than n_window). This leads me to the second for-loop. Here I iterate over all points and actually grab the positions from the frames, in which the current point did not travel farther than cut_off. From these selected frames I calculate the mean value of the coordinates and use it as the new value for the current frame.
This very last for-loop slows down the whole processing. I can't come up with a better way to accomplish this calculation. Any suggestions?
MWE
Put in comments for clarification.
import numpy as np
# Generate a timeseries with 1000 frames, each
# containing 50 individual points defined by their
# x and y coordinates
n_frames = 1000
n_points = 50
n_coordinates = 2
timeseries = np.random.randint(-100, 100, [n_frames, n_points, n_coordinates])
# Set window size to 20 frames
n_window = 20
# Distance cut off
cut_off = 60
# Set up empty array to hold results
avg_data_store = np.zeros([n_frames, timeseries.shape[1], 2])
# Iterate over all frames
for frame in np.arange(0, n_frames):
# Set the frame according to the window size that we're looking at
t_before = int(frame - (n_window / 2))
t_after = int(frame + (n_window / 2))
# If we're trying to access frames below 0, set the lowest one to 0
if t_before < 0:
t_before = 0
# Trying to access frames that are not in the trajectory, set to last frame
if t_after > n_frames - 1:
t_after = n_frames - 1
# Grab x and y coordinates for all points in the corresponding window
pos_before = timeseries[t_before:frame]
pos_after = timeseries[frame + 1:t_after + 1]
pos_now = timeseries[frame]
# Calculate the distance between the current frame and the windows before/after
d_before = np.abs(pos_before - pos_now)
d_after = np.abs(pos_after - pos_now)
# Grab indices of frames+points, that are below the cut off
arg_before = np.argwhere(np.all(d_before < cut_off, axis=2))
arg_after = np.argwhere(np.all(d_after < cut_off, axis=2))
# Iterate over all points
for i in range(0, timeseries.shape[1]):
# Create temp array
temp_stack = pos_now[i]
# Grab all frames in which the current point did _not_
# travel farther than `cut_off`
all_before = arg_before[arg_before[:, 1] == i][:, 0]
all_after = arg_after[arg_after[:, 1] == i][:, 0]
# Grab the corresponding positions for this points in these frames
all_pos_before = pos_before[all_before, i]
all_pos_after = pos_after[all_after, i]
# If we have any frames for that point before / after
# stack them into the temp array
if all_pos_before.size > 0:
temp_stack = np.vstack([all_pos_before, temp_stack])
if all_pos_after.size > 0:
temp_stack = np.vstack([temp_stack, all_pos_after])
# Calculate the moving window average for the selection of frames
avg_data_store[frame, i] = temp_stack.mean(axis=0)
If you are fine with calculating the cutoff distance in x and y separately, you can use scipy.ndimage.generic_filter.
import numpy as np
from scipy.ndimage import generic_filter
def _mean(x, cutoff):
is_too_different = np.abs(x - x[len(x) / 2]) > cutoff
return np.mean(x[~is_too_different])
def _smooth(x, window_length=5, cutoff=1.):
return generic_filter(x, _mean, size=window_length, mode='nearest', extra_keywords=dict(cutoff=cutoff))
def smooth(arr, window_length=5, cutoff=1., axis=-1):
return np.apply_along_axis(_smooth, axis, arr, window_length=window_length, cutoff=cutoff)
# --------------------------------------------------------------------------------
def _simulate_movement_2d(T, fraction_is_jump=0.01):
# generate random velocities with a few "jumps"
velocity = np.random.randn(T, 2)
is_jump = np.random.rand(T) < fraction_is_jump
jump = 10 * np.random.randn(T, 2)
jump[~is_jump] = 0.
# pre-allocate position and momentum arrays
position = np.zeros((T,2))
momentum = np.zeros((T,2))
# initialise the first position
position[0] = np.random.randn(2)
# update position using velocity vector:
# smooth movement by not applying the velocity directly
# but rather by keeping track of the momentum
for ii in range(2,T):
momentum[ii] = 0.9 * momentum[ii-1] + 0.1 * velocity[ii-1]
position[ii] = position[ii-1] + momentum[ii] + jump[ii]
# add some measurement noise
noise = np.random.randn(T,2)
position += noise
return position
def demo(nframes=1000, npoints=3):
# create data
positions = np.array([_simulate_movement_2d(nframes) for ii in range(npoints)])
# format to (nframes, npoints, 2)
position = positions.transpose([1, 0, 2])
# smooth
smoothed = smooth(positions, window_length=11, cutoff=5., axis=1)
# plot
x, y = positions.T
xs, ys = smoothed.T
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1)
ax.plot(x, y, 'o')
ax.plot(xs, ys, 'k-', alpha=0.3, lw=2)
plt.show()
demo()

Changing line color

I'm trying to change the colour of a line on matplotlib subject to a condition.
Basically I take a rolling average and a rolling standard deviation. I plot the rolling average, but I would like to change the line colour if the standard deviation corresponding to that average is over the threshold I set. This is not the color of the whole line, just the bits that are over the threshol. Mostly my data is set using pandas
Alternatively I could shade it instead.
This link is useful, although I cannot figure out how to apply it to my situation.
http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb
EDIT COde: although, overly complicated for the question,
(I know the functions are too long at the moment)
def av_rel_track(rel_values):
#blade==0
avg_rel_track=[]
for i in range(0, int(nb)):
av_values=Series([])
rel_blade=rel_values[i]
rel_blade=rel_blade.fillna(0)
av_values=[]
for num in range(0, int (navg)):
av_values.append( np.nan)
#loops over each revolution(row)
for rev in range(int(navg),len(rel_blade)):
#select section to be number of averages long
N=rev-int(navg)+1
section=rel_blade.loc[N:rev]
#check section for five consecutive zeros
checker=check5(section)
#if there is five con zeros, av_value is zero
if checker==True:
av_value=0
else:
#finds the number of zeros in the section
nz=len (section)-len(section.nonzero()[0])
while nz>0:
#whilst there is a zero, extend average by one
N=N-1
if N<0:
break
new_val=rel_blade.ix[N]
section=rel_blade[N:rev+1]
#checks if new value is zero
if new_val!=0:
nz=nz-1
#checks extended section does not contain 5 consec zeros
checker=check5(section)
if checker==True:
av_value=0
else:
#sets av_value to 0if the range extends beyond the first value of rel_values
if N<0:
av_value=0
else:
#calculates the mean of the sctinon(not including nans)
section=zero_to_nan(section)
av_value=stats.nanmean(section)
av_values.append(av_value)
av_values=zero_to_nan(av_values)
rel_values["a%s" % i]=av_values
av_track=DataFrame({1:rel_values['a0'], 2:rel_values['a1'],3:rel_values['a2'],4:rel_values['a3'],5:rel_values['a4']})
return av_track
def sd_rel_track(rel_values):
for i in range(0, int(nb)):
sd_values=Series([])
rel_blade=rel_values[i]
rel_blade=rel_blade.fillna(0)
sd_values=[]
for num in range(0, int (navg)):
sd_values.append( np.nan)
#loops over each revolution(row)
for rev in range(int(navg),len(rel_blade)):
#select section to be number of averages long
N=rev-int(navg)+1
section=rel_blade.loc[N:rev]
#check section for five consecutive zeros
checker=check5(section)
#if there is five con zeros, av_value is zero
if checker==True:
sd_value=0
else:
#finds the number of zeros in the section
nz=len (section)-len(section.nonzero()[0])
while nz>0:
#whilst there is a zero, extend average by one
N=N-1
if N<0:
break
new_val=rel_blade.ix[N]
section=rel_blade[N:rev+1]
#checks if new value is zero
if new_val!=0:
nz=nz-1
#checks extended section does not contain 5 consec zeros
checker=check5(section)
if checker==True:
sd_value=0
else:
#sets av_value to 0if the range extends beyond the first value of rel_values
if N<0:
sd_value=0
else:
#calculates the mean of the sctinon(not including nans)
section=zero_to_nan(section)
sd_value=stats.nanstd(section)
sd_values.append(sd_value)
sd_values=zero_to_nan(sd_values)
rel_values["sd%s" % i]=sd_values
sd_track=DataFrame({1:rel_values['sd0'], 2:rel_values['sd1'],3:rel_values['sd2'],4:rel_values['sd3'],5:rel_values['sd4']})
sumsd= sd_track.sum(axis=1)
return sumsd
def plot():
plt.figure()
plt.plot(av_values)
plt.show()
plt.figure()
plt.plot(sd_values)
plt.show()
Using
http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb ,
In[4], you can add something like:
x = np.linspace(0, 4.*np.pi, 1000)
y = np.sin(x)
z = np.zeros(1000)
for i in range(1000):
if math.cos(x[i])>0.7:
z[i]=1
fig, axes = plt.subplots()
colorline(x, y, z)
plt.xlim(x.min(), x.max())
plt.ylim(-1.0, 1.0)
plt.show()

Categories

Resources