Plot the element-wise product of two numpy arrays - python

I'm very new to python, I wanted to write a program that multiplies elements present inside two arrays and plots a graph
How should I correct the code?
import matplotlib.pyplot as plt
import numpy as np
x=(np.double[0.1,0.001,0.0001,0.0001,0.00001])
y=(np.double[0.1,0.001,0.0001,0.0001,0.00001])
m=len(x)
n=len(y)
for m in range(0,m):
for n in range(0,n):
plt.plot[x(m),y(n)]
plt.show()

x = np.array([0.1, 0.001, 0.0001, 0.0001, 0.00001] )
plt.plot(x ** 2)
plt.show()
If x and y are different, use:
x = np.array([...]) # replace [...] with your list
y = np.array([...])
plt.plot(x * y)
plt.show()

Related

Scatter plot for points in an array above a given value

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
eps = 0.8
X = np.linspace(-1, 1, 100)
Y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(X, Y)
Z = np.exp(-X**2-Y**2)
data_zero_x = np.array([])
data_zero_y = np.array([])
for i in range(len(X)):
for j in range(len(Y)):
if Z[i][j] > eps:
data_zero_x = np.append(data_zero_x, X[i])
data_zero_y = np.append(data_zero_y, Y[j])
plt.scatter(data_zero_x, data_zero_y)
plt.show()
Hey there!
I would expect this code to produce circular points around the origin since this is where the function Z is above eps=0.8. Instead, I get a rectangular picture out of it. Any ideas what I'm doing wrong here? Also, if there is a better way to code something like this I am all ears.
Try this:
import matplotlib.pyplot as plt
import numpy as np
eps = 0.8
X = np.linspace(-1, 1, 100)
Y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(X, Y)
Z = np.exp(-X**2-Y**2)
mask = Z > eps
plt.scatter(X[mask], Y[mask])
plt.show()
Since you are working with a numpy array, there is no need to loop over the complete array and check your condition (> 0.8) for each element. Numpy arrays have overloaded the comparison operators such that when you compare the whole array, it implicitly loops over each element and returns another array with True and False for each element in the original array. You can then use this array as a mask to select elements from other arrays as I did in the code above.
Also, you don't need the line fig = plt.figure() when you are working with plt.scatter. You only need that you if want to work with the object oriented approach, where you create the figure and the axes explicitly and call the plot methods of the axes objects.

numpy.linspace - bad labels on the X axis

I have this small plotting program.
But when I run it I notice the labels on the X axis are incorrect.
They go from 0 to 5000 while actually I have the interval [-1.5, 1.5]
1... How can I fix that?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1.5, 1.5, 5000)
y1 = np.tan(x) * np.arctan(x)
y2 = x * x
plt.plot(y1)
plt.plot(y2)
plt.show()
2... Also, if I change the linspace to call
x = np.linspace(-mt.pi/2.0 + 1/(10**6), mt.pi/2.0 - 1/(10**6), 5000)
I get an even stranger and really incorrect plot.
Something gets messed up completely.
Why? I want to plot these 2 functions in the range (-pi/2, pi/2)
How do I do this?
Try:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1.5, 1.5, 5000)
y1 = np.tan(x) * np.arctan(x)
y2 = x * x
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()
Now x axis values are is between -1.5 and 1.5.
Regarding strange plot in second case just notice that:
np.tan(-1.5)
-14.101419947171719
and:
np.tan(-mt.pi/2.0)
-1.633123935319537e+16
which is much much bigger.

Peak finding and analysis on python

I have written a code that reads in my data file and plots it and then fits it and finds the peaks however I have 6 peaks and the code is only currently fitting 2 of the peaks and isn't returning any data on them by code is as follows:
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
data = np.genfromtxt("C:\\Users\\lenovo laptop\\practice_data_ll16ame1.dat", skip_header = 15)
x = data[: , 0]
y = data[: , 1]
plt.plot(x,y)
plt.show()
def func(x, *params):
y = np.zeros_like(x)
for i in range(0, len(params), 3):
ctr = params[i]
amp = params[i+1]
wid = params[i+2]
y = y + amp * np.exp( -((x - ctr)/wid)**2)
return y
guess = [0, 60000, 80, 1000, 60000, 80]
for i in range(12):
guess += [60+80*i, 46000, 25]
popt, pcov = curve_fit(func, x, y, p0=guess)
fit = func(x, *popt)
plt.plot(x, y)
plt.plot(x, fit , 'r-')
plt.show()
When I looked at the plot of your custom function, it was clear that the majority of points were in a more-or-less horizontal line, so the function wouldn't fit well to your peaks. Because there is no noise and the peaks are so prominent, you just need to pass the y values and a threshold to the find_peaks function.
By implementing find_peaks instead of your custom function, you get the following code:
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
data = np.genfromtxt("C:\\Users\\lenovo laptop\\practice_data_ll16ame1.dat", skip_header = 15)
x = data[: , 0]
y = data[: , 1]
points = find_peaks(y, height = 100)
plt.plot(x, y)
for i in points[0]:
plt.scatter(x[i], y[i])
plt.show()
Find_peaks returns a tuple consisting of two things:
1. The index of the peaks ( points[0] in the code above)
2. The height of each peak (points[1])
The code yields the following plot, which I believe is what you want:

plotting a DataFrame in 3D surfaceplot

I have a dataframe (size: 1008,100). the values of cells are within 0.1 and 1. I would like to visualize it in a surface plot but i cannot really figure out what the x,y and z values are gonna be. I'd like to position the surface plot like the rows(1008) is the aligned with the x axis and the columns(100) is aligned with the y axis.
Any help is much appreciated.
thanks
The x and y you are looking for can be created with meshgrid. A good way to start is to find an example on the matplotlib gallery and make changes from there. As an example:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# create a data frame with 1008x100 values between 0.1 and 1.0
xs = np.arange(0, 1008)
ys = np.arange(0,100)
zs = np.square(xs[:, np.newaxis]) * np.square(ys[np.newaxis, :])
max_value = np.max(zs)
min_value = np.min(zs)
zs = (zs - min_value) / (max_value - min_value) * 0.9 + 0.1
data = pd.DataFrame(zs)
# create X and Y with np.meshgrid and the 2D data frame
# (reusing the scratch variable xs and ys)
xs = np.arange(data.shape[0]) # [0,1,....,1007]
ys = np.arange(data.shape[1]) # [0,1,...,99]
X, Y = np.meshgrid(xs, ys)
# create a surface plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, data.T)
( note: i needed to transpose the data with .T, don't know why, sometime it is needed...)

Plotting a polynomial using Matplotlib and coeffiecients

My code is:
import numpy as np
import matplotlib as plt
polyCoeffiecients = [1,2,3,4,5]
plt.plot(PolyCoeffiecients)
plt.show()
The result for this is straight lines that describe the points in 1,2,3,4,5 and the straight lines between them, instead of the polynomial of degree 5 that has 1,2,3,4,5 as its coeffiecients ( P(x) = 1 + 2x + 3x + 4x + 5x)
How am i suppose to plot a polynomial with just its coefficients?
Eyzuky, see if this is what you want:
import numpy as np
from matplotlib import pyplot as plt
def PolyCoefficients(x, coeffs):
""" Returns a polynomial for ``x`` values for the ``coeffs`` provided.
The coefficients must be in ascending order (``x**0`` to ``x**o``).
"""
o = len(coeffs)
print(f'# This is a polynomial of order {o}.')
y = 0
for i in range(o):
y += coeffs[i]*x**i
return y
x = np.linspace(0, 9, 10)
coeffs = [1, 2, 3, 4, 5]
plt.plot(x, PolyCoefficients(x, coeffs))
plt.show()
You could approximately draw the polynomial by getting lots of x-values and using np.polyval() to get the y-values of your polynomial at the x-values. Then you could just plot the x-vals and y-vals.
import numpy as np
import matplotlib.pyplot as plt
curve = np.array([1,2,3,4,5])
x = np.linspace(0,10,100)
y = [np.polyval(curve, i) for i in x]
plt.plot(x,y)
A very pythonic solution is to use list comprehension to calculate the values for the function.
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(0, 10, 11)
coeffs = [1, 2, 3, 4, 5]
y = np.array([np.sum(np.array([coeffs[i]*(j**i) for i in range(len(coeffs))])) for j in x])
plt.plot(x, y)
plt.show()
Generic, vectorized implementation:
from typing import Sequence, Union
import numpy as np
import matplotlib.pyplot as plt
Number = Union[int, float, complex]
def polyval(coefficients: Sequence[Number], x: Sequence[Number]) -> np.ndarray:
# expand dimensions to allow broadcasting (constant time + inexpensive)
# axis=-1 allows for arbitrarily shaped x
x = np.expand_dims(x, axis=-1)
powers = x ** np.arange(len(coefficients))
return powers # coefficients
def polyplot(coefficients: Sequence[Number], x: Sequence[Number]) -> None:
y = polyval(coefficients, x)
plt.plot(x, y)
polyplot(np.array([0, 0, -1]), np.linspace(-10, 10, 210))
plt.show()

Categories