Related
I have a problem related to cv2. I am trying to change a color for each 10th pixel on even row and each 11th pixel on odd row to the red. I am trying to select a specific row but I cannot. Please help
import cv2
import matplotlib.pyplot as plt
# read image
image = cv2.imread('2161382.jpg')
im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# resize image
resized_img = cv2.resize(im_rgb,(500,500))
img_width = resized_img.shape[1]
img_height = resized_img.shape[0]
# Change individual pixel value (y,x)
resized_img[200, 10] = (255,0,0)
for row in resized_img:
if row.all() % 2 == 0:
resized_img[:,row + 11] = (255,0,0)
# On your own create a cycle where you can change the color of every N-th pixel on the odd row
# and every M-th pixel on the even row to a different colour
%matplotlib notebook
plt.figure(figsize=(10,10))
plt.imshow(resized_img)
You need to check the row number. row.all doesn't do that. This works:
# Change individual pixel value (y,x)
resized_img[200, 10] = (255,0,0)
for y,row in enumerate(resized_img):
if y % 2:
row[11] = (255,0,0)
else:
row[10] = (255,0,0)
You don't want loops at all, you want indexing which is miles faster and simpler. The indices are set using:
array[START:END:STEP]
so you want:
# Make image of 4 rows 22 columns of zeroes
im = np.zeros((4,22), np.uint8)
That looks like this:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
dtype=uint8)
Now change some values using indexing:
# Start at row 0 and on every 2nd row, set every 10th pixel to 7
im[0::2,::10] = 7
# Start at row 1 and on every 2nd row, set every 11th pixel to 9
im[1::2,::11] = 9
Now it looks like this:
array([[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0],
[9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0],
[9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
dtype=uint8)
If you have a 3-channel RGB image and you want to set R, G and B, you could add a third, explicit index:
im[::2, ::10, :] = [255,0,0]
Or you could omit it and remain implicit (thanks #Reti43):
im[::2, ::10] = [255,0,0]
I have a square matrix filled by -1, 0 or 1. I would like to visualize this matrix with spheres or circles of the same radius. Radius, indeed is not important at all. Those circles though, must have a different colour according to the number of the matrix cell.
For example:
10 x 10 matrix -> 100 circles on a plane, 10 rows x 10 columns
Color of circle in position (2,9) depending on number of matrix in position (2,9).
Thank you!
People I know told me to use matlibplot, but I am new to Python and
I have many issues!
This is what I did up to now:
{`
import numpy as np
#from implementations import *
#from config import *
import matplotlib.pyplot as plt
A = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
rows=len(A) # finding number rows of lattice, so no need to worry about it!
columns=len(A[0]) # finding number columns of lattice, so no need to worry about it!
fig, ax = plt.subplots()
for i in range(rows):
for j in range(columns):
if A[i][j]==-1:
circle1 = plt.Circle((i*4, j*4), 2, color='blue')
fig = plt.gcf()
ax = fig.gca()
ax.add_artist(circle1)
if A[i][j]== 1:
circle2 = plt.Circle((i*4, j*4), 2, color='yellow')
fig = plt.gcf()
ax = fig.gca()
ax.add_artist(circle2)
`}
Here is the matplotlib code that uses scatter matrix:
# Imports
import matplotlib.pyplot as plt
from itertools import chain
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Here is our matrix. Note, that it is expected to be rectangular!
matrix = [
[0, 1, 0,1],
[-1,1, 0,0],
[1,-1,-1,1],
]
# Get X-length
X = len(matrix[0])
# Get Y-length
Y = len(matrix)
# Construct grids for scatter
x_grid = list(range(X)) * Y # 1,2,3,4,1,2,3,4...
y_grid = [y for y in range(Y) for _ in range(X)] # 1,1,1,1,2,2,2,2...
# Flatten the matrix because ax.scatter uses flat arrays
matrix_grid = list(chain(*matrix))
plt.scatter(
x_grid, # X-grid array of coordinates
y_grid, # Y-grid array of coordinates
c=matrix_grid, # Our flatten matrix of -1/0/1s
cmap='gist_rainbow' # Color map - defines colors
)
You can directly use a scatter as follows:
import numpy as np
import matplotlib.pyplot as plt
A = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
X,Y = np.meshgrid(np.arange(A.shape[1]), np.arange(A.shape[0]))
plt.scatter(X.flatten(), Y.flatten(), c=A.flatten())
plt.show()
I have the following annual cash flows:
w=np.array([ -56501, -14918073, -1745198, -20887403, -9960686, -31076934,
0, 0, 11367846, 26736802, -2341940, 20853917,
22166416, 19214094, 23056582, -11227178, 18867100, 24947517,
28733869, 24707603, -17030396, 7753089, 27526723, 31534327,
26726270, -24607953, 11532035, 29444013, 24350595, 30140678,
-33262793, 5640172, 32846900, 38165710, 31655489, -74343373,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, -8727068])
I calculate IRR using np.irr
np.irr(w)
Out[141]: -0.05393588064654964
When I use IRR function in Excel for the same cash flows, I get 12%.
These two functions usually produce the same result. Does anyone know why in this case the results are so different? Thanks!
For the given cash flows, the IRR is not unique; see Multiple IRRs. Both the numpy and Excel values for r satisfy NPV(r) = 0, where NPV is the net present value.
Here's a plot of NPV(r) for the data in w. The red stars mark the IRR values (where NPV(r) is zero).
Here's the script that generates the plot:
import numpy as np
import matplotlib.pyplot as plt
w = np.array([ -56501, -14918073, -1745198, -20887403, -9960686, -31076934,
0, 0, 11367846, 26736802, -2341940, 20853917,
22166416, 19214094, 23056582, -11227178, 18867100, 24947517,
28733869, 24707603, -17030396, 7753089, 27526723, 31534327,
26726270, -24607953, 11532035, 29444013, 24350595, 30140678,
-33262793, 5640172, 32846900, 38165710, 31655489, -74343373,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, -8727068])
r_excel = 0.1200963665
r_numpy = np.irr(w)
rr = np.linspace(-0.055, 0.16, 500)
npvals = np.array([np.npv(r, w) for r in rr])
plt.plot(rr, npvals/1e6, alpha=0.8)
plt.plot(r_numpy, 0, 'r*')
plt.plot(r_excel, 0, 'r*')
plt.grid(True)
plt.xlabel('r')
plt.ylabel('NPV(r) [millions]')
plt.show()
I want to make convolution of gaussian and rectangular functions like that:
from numpy import linspace, sqrt, sin, exp, convolve, abs
from matplotlib import pyplot as plt
def gauss(x, x0=0, sigma=1):
return exp(-1*(x-x0)**2/(2*sigma**2))
def rect(x):
return 1 if abs(x)<=0.5 else 0
x = linspace(-10, 10, 100)
f1 = gauss(x, sigma=1)
f2 = rect(x)
fsum = convolve(f1, f2)
y = linspace(-10, 10, 199)
plt.plot(x, f1)
plt.plot(x, f2)
plt.plot(y, fsum)
plt.show()
But I can't correctly describe rect function:
return 1 if abs(x)<=0.5 else 0
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Assuming you want an array of 0 and 1 values, the same shape as x, you can use numpy.where:
In [8]: x = np.linspace(-10, 10, 100)
In [9]: np.where(abs(x)<=0.5, 1, 0)
Out[9]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0])
So your function could be:
from numpy import linspace, sqrt, sin, exp, convolve, abs, where
...
def rect(x):
return where(abs(x)<=0.5, 1, 0)
def rect(x):
y=x
for i in range(len(x)):
if abs(x[i]) <= 0.5:
y[i] = 1
else:
y[i] = 0
return y
I am plotting a 5X10 matrix of vectors using pyplot.quiver :
from pylab import *
COLUMN_RESOLUTION = 10
ROW_RESOLUTION = 5
plotBorders = 2
X,Y = meshgrid(arange(COLUMN_RESOLUTION),arange(ROW_RESOLUTION)) # X,Y positions of vectors
U = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -1.0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
V = [0, 0, 0, 0, -1.0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
lim = 10
xlim(-1*lim,lim)
ylim(-1*lim,lim)
quiver(X,Y, U, V)
show()
The resulting figure has vectors with infinite length - no matter how much I extend the axes (the parameter lim) The arrows' head is not seen :
lim = 10
lim = 100
What am I doing wrong?
Thanks!
Use the "scale" parameter in the quiver command:
quiver(X,Y, U, V, scale=20.0)