I have 3 known positions onto a map using X, Y matplotlib using subplots, I need to plot a bearing from one of the towers and place a line onto the map.
I am currently using X, Y for start position and arrow/line for the end position. But would like to know how to use a bearing in place of the end of the line instead of x, y. I'm currently looking into Vectors but having no luck.
My code is below any help would be great
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
df = pd.read_csv("Book1.csv")
df.head()
tower1x = [51.69557]
tower1y = [-3.34197]
tower2x = [51.69673]
tower2y = [-3.34235]
tower3x = [51.69630]
tower3y = [-3.34090]
BBox = (df.longitude.min(), df.longitude.max(),
df.latitude.min(), df.latitude.max())
print (BBox)
ruh_m = plt.imread('map.png')
fig, ax = plt.subplots(figsize = (12,12))
ax.scatter(tower1x, tower1y, zorder=1, alpha= 0.5, c='red', s=50, label="Tower 1")
ax.scatter(tower2x, tower2y, zorder=1, alpha= 0.5, c='blue', s=50, label="Tower 2")
ax.scatter(tower3x, tower3y, zorder=1, alpha= 0.5, c='green', s=50, label="Tower 3")
ax.annotate("",xy=(51.69557,-3.34197), xytext=**(51.69799, -3.34155)**, textcoords='data',
arrowprops=dict(arrowstyle="<-", connectionstyle="arc3"),) # point from tower 1 using X,Y
ax.set_title('Plotting Towers in Aberfan')
ax.set_xlim(BBox[0],BBox[1])
ax.set_ylim(BBox[2],BBox[3])
ax.imshow(ruh_m, zorder=0, extent = BBox, aspect= 'equal')
plt.legend()
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
df = pd.read_csv("Grids.csv")
df.head()
##
###X and Y Coordinates using www.openstreetmap.org
BBox = (df.longitude.min(), df.longitude.max(),
df.latitude.min(), df.latitude.max())
#Signal power
s1 = int(input("Enter power from tower 1: "))
s2 = int(input("Enter power from tower 2: "))
s3 = int(input("Enter power from tower 3: "))
#weight of signal
w1 = [s1 / ( s1 + s2 + s3 )]
w2 = [s2 / ( s1 + s2 + s3 )]
w3 = [s3 / ( s1 + s2 + s3 )]
#Tower locations
tower1_lat = np.array ([51.6985630])
tower1_lon = np.array ([-3.3410239])
##
tower2_lat = np.array ([51.6984699])
tower2_lon = np.array ([-3.3400422])
##
tower3_lat = np.array([51.6980743])
tower3_lon = np.array([-3.3406511])
#Your location using the above values
Locaion_Longitude = [tower1_lat * w1 + tower2_lat * w2 + tower3_lat * w3 ]
Locaion_Latitude = [tower1_lon * w1 + tower2_lon * w2 + tower3_lon* w3 ]
print (Locaion_Longitude)
print (Locaion_Latitude)
map_fig = plt.imread('map.png')
fig, ax = plt.subplots(dpi=150,figsize = (12,7))
ax.scatter(tower1_lon, tower1_lat, zorder=1, alpha= 0.5, c='red', s=50, label="Tower 1")
ax.scatter(tower2_lon, tower2_lat, zorder=1, alpha= 0.5, c='blue', s=50, label="Tower 2")
ax.scatter(tower3_lon, tower3_lat, zorder=1, alpha= 0.5, c='green', s=50, label="Tower 3")
ax.scatter(Locaion_Longitude, Locaion_Latitude, zorder=1, c='yellow', s=50, label="Your location")
ax.tick_params (axis='y', which='major', labelsize=5)
ax.tick_params (axis='x', which='major', labelsize=5)#axis settings
ax.set_title('Plotting Towers in Aberfan')
ax.set_xlim(BBox[0],BBox[1])
ax.set_ylim(BBox[2],BBox[3])
ax.imshow(map_fig, zorder=0, extent = BBox, aspect= "auto")
mng = plt.get_current_fig_manager() #full screen
mng.window.state("zoomed")
plt.legend()
plt.show()
enter code here
Related
I have the following code that should draw a cycloid with animation and save it to a gif
but after running the program, a white square appears that covers everything, I can't find the reason cycloid_animation
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation, PillowWriter
plt.rcParams['animation.html'] = 'html5'
R = 1
def circle(a, b, r):
# (a,b): the center of the circle
# r: the radius of the circle
# T: The number of the segments
T = 100
x, y = [0]*T, [0]*T
for i,theta in enumerate(np.linspace(0,2*np.pi,T)):
x[i] = a + r*np.cos(theta)
y[i] = b + r*np.sin(theta)
return x, y
# Calculate the cycloid line
thetas = np.linspace(0,4*np.pi,100)
cycloid_x = R*(thetas-np.sin(thetas))
cycloid_y = R*(1-np.cos(thetas))
cycloid_c = R*thetas
fig = plt.figure()
lns = []
trans = plt.axes().transAxes
for i in range(len(thetas)):
x,y = circle(cycloid_c[i], R, R)
ln1, = plt.plot(x, y, 'g-', lw=2)
ln2, = plt.plot(cycloid_x[:i+1] ,cycloid_y[:i+1], 'r-', lw=2)
ln3, = plt.plot(cycloid_x[i], cycloid_y[i], 'bo', markersize=4)
ln4, = plt.plot([cycloid_c[i], cycloid_x[i]], [R,cycloid_y[i]], 'y-', lw=2)
tx1 = plt.text(0.05, 0.8, r'$\theta$ = %.2f $\pi$' % (thetas[i]/np.pi), transform=trans)
lns.append([ln1,ln2,ln3,ln4,tx1])
plt.xlim(0,15)
plt.ylim(0,3)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axes().set_aspect('equal')
ani = animation.ArtistAnimation(fig, lns, interval=50)
#ani.save('cycloid_ArtistAnimation.mp4',writer='ffmpeg')
ani.save('cycloid_ArtistAnimation.gif',writer='pillow')
ani
Each time you call plt.axis() you are creating a new axis on top of the figure. Since what you want is to get the current axis and then apply the transformations, after creating the figure you should call plt.gca() to get the current axis and use that instead.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation, PillowWriter
plt.rcParams['animation.html'] = 'html5'
R = 1
def circle(a, b, r):
# (a,b): the center of the circle
# r: the radius of the circle
# T: The number of the segments
T = 100
x, y = [0]*T, [0]*T
for i,theta in enumerate(np.linspace(0,2*np.pi,T)):
x[i] = a + r*np.cos(theta)
y[i] = b + r*np.sin(theta)
return x, y
# Calculate the cycloid line
thetas = np.linspace(0,4*np.pi,100)
cycloid_x = R*(thetas-np.sin(thetas))
cycloid_y = R*(1-np.cos(thetas))
cycloid_c = R*thetas
fig = plt.figure()
lns = []
trans = plt.gca().transAxes #<=== HERE
for i in range(len(thetas)):
x,y = circle(cycloid_c[i], R, R)
ln1, = plt.plot(x, y, 'g-', lw=2)
ln2, = plt.plot(cycloid_x[:i+1] ,cycloid_y[:i+1], 'r-', lw=2)
ln3, = plt.plot(cycloid_x[i], cycloid_y[i], 'bo', markersize=4)
ln4, = plt.plot([cycloid_c[i], cycloid_x[i]], [R,cycloid_y[i]], 'y-', lw=2)
tx1 = plt.text(0.05, 0.8, r'$\theta$ = %.2f $\pi$' % (thetas[i]/np.pi), transform=trans)
lns.append([ln1,ln2,ln3,ln4,tx1])
plt.xlim(0,15)
plt.ylim(0,3)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.gca().set_aspect('equal') #<=== And HERE
ani = animation.ArtistAnimation(fig, lns, interval=50)
#ani.save('cycloid_ArtistAnimation.mp4',writer='ffmpeg')
ani.save('cycloid_ArtistAnimation.gif',writer='pillow')
Having the following line in my plot code
ax.plot(x, pdf_individual, '--k', label = "single Gaussians")
, with pdf_individual being a list of lists, results in this picture:
Is there a way to just have "single Gaussians" once in the labels, instead of 6 times, which is the amount of single Gaussians for the Gaussian Mixture Model?
This is the whole post with the suggested solution
import matplotlib as mpl
import matplotlib.ticker as mtick
from matplotlib.lines import Line2D
mpl.rcParams['figure.dpi'] = 600
test_input = input_list # THIS IS A 1D LIST with a few hundred items
X = np.asarray(test_input).reshape(-1,1)
N = np.arange(1, 11)
models = [None for i in range(len(N))]
for i in range(len(N)):
models[i] = GaussianMixture(N[i]).fit(X)
# compute the AIC and the BIC
AIC = [m.aic(X) for m in models]
BIC = [m.bic(X) for m in models]
fig = plt.figure(figsize=(12, 4))
fig.subplots_adjust(left=0.1, right=0.9,
bottom=0.21, top=0.9, wspace=0.3)
ax = fig.add_subplot(131)
M_best = models[np.argmin(AIC)]
comp_count = str(M_best)
x = np.linspace(0, 0.1, 100)
logprob = M_best.score_samples(x.reshape(-1, 1))
responsibilities = M_best.predict_proba(x.reshape(-1, 1))
pdf = np.exp(logprob)
pdf_individual = responsibilities * pdf[:, np.newaxis]
left, width = .245, .5
bottom, height = .4, .5
right = left + width
top = bottom + height
plt.setp( ax.xaxis.get_majorticklabels(), rotation=-45, ha="left" )
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.hist(X, 30, density=True, histtype='stepfilled', alpha=0.4, label="Data")
ax.plot(x, pdf, '-k', color = "red", label='GMM')
for i, pdf_individual in enumerate(pdf_individual):
ax.plot(x, pdf_individual, '--k', label = "single Gaussians" if i == 0 else "")
#for pdf in pdf_individual[1:]: ax.plot(x, pdf, '--k')
ax.text(right, top, "Anzahl Komponenten: " + comp_count[-2],
horizontalalignment='center',
verticalalignment='bottom',
transform=ax.transAxes)
ax.set_xlabel('$x$')
ax.set_ylabel('$p(x)$')
plt.legend()
plt.show()
It results in this error:
ValueError: x and y must have same first dimension, but have shapes (100,) and (6,)
EDIT:
Putting
pdf_individual = np.transpose(pdf_individual)
makes the code above work
I am trying to calculate one of the basic decay simulation and plot the results as animation. Without animation results fine but when i try to create animation both line appears fully developed at the first time step. The change over time is not as it should be. What am I doing wrong? I'm open to suggestions.
Original plot:
plot
Here the code:
# I135 Xe135 decay.
"""
EQUATIONS:
dIdt = (-Lambda_I * N_I)
dXedt = ((-Lambda_Xe * N_Xe) + (Lambda_I * N_I))
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.integrate import odeint
def model(z,t):
"""
INITIAL CONDITIONS:
I(0) = 1e8 atoms
Xe(0) = 0 atom
PARAMETERS:
Lambda_I
Lambda_Xe
"""
Lambda_I = (np.log(2) / 6.57)
Lambda_Xe = (np.log(2) / 9.2)
N_I = z[0]
N_Xe = z[1]
dIdt = (-Lambda_I * N_I)
dXedt = ((-Lambda_Xe * N_Xe) + (Lambda_I * N_I))
return [dIdt,dXedt]
z0 = [1e8, 0.0] # initial conditions for N_Xe and N_I
n = 10000
max_time = 100
t = np.linspace(0,max_time,n)
N_I = np.zeros(n)
N_Xe = np.zeros(n)
# Solution
for i in range(n):
z = odeint(model,z0,t)
z0 = z[1]
N_I[i] = z0[0]
N_Xe[i] = z0[1]
# Graph and animation
fig, ax = plt.subplots(figsize=(8,8))
ax.set_ylim(0, 1e8)
ax.set_xlim(0, max_time)
line1, = ax.plot(N_I, 'b-', linewidth=2)
line2, = ax.plot(N_Xe,'g-.', linewidth=2)
plt.rcParams['font.size'] = '14'
plt.minorticks_on()
plt.tick_params(axis="both", which="major", length=12, labelsize=12, width=1, color="black")
plt.tick_params(axis="both", which="minor", length=6, labelsize=10, width=0.8, color="black")
plt.title('I-135 Xe-135 Decay Sim.', fontsize=18)
plt.xlabel('Time (h)', fontsize=16)
plt.ylabel('N', fontsize=16)
plt.legend(['I-135','Xe-135'],prop={'size': 12})
plt.grid(color = 'black', linestyle = '--', linewidth = 0.6)
def animate(j):
line1.set_xdata(np.linspace(0,j,n))
line2.set_xdata(np.linspace(0,j,n))
return [line1,line2,]
ani = animation.FuncAnimation(fig, animate, frames=max_time, blit=True, interval=10, save_count=10)
plt.show()
f = r"C://Users/USER_NAME/Desktop/animation.gif"
writergif = animation.PillowWriter(fps=30)
ani.save(f, writer=writergif)
I need to plot a hist with bot logarithmic y and x-axis, but I'd like also to have hist's bins displayed of same size.
How can I achieve this result with the following code (the x used is very long so I have intentionally avoided to insert it):
import matplotlib as plt
import numpy as np
fig, ax1 = plt.subplots()
hist, bins, _ = ax1.hist(x, log=True, color="red", rwidth=0.5)
plt.xscale("log")
np_x = np.array(x)
print("np_x.mean() = " + str(np_x.mean()))
plt.axvline(np_x.mean() * 1.1, color='lime', linestyle='dashed', linewidth=3,
label='Mean: {:.2f}'.format(np_x.mean()))
handles, labels = ax1.get_legend_handles_labels()
binwidth = math.floor(bins[1] - bins[0])
mylabel = "Binwidth: {}".format(binwidth) + ", Bins: {}".format(len(hist))
red_patch = mpatches.Patch(color='red', label=mylabel)
handles = [red_patch] + handles
labels = [mylabel] + labels
ax1.legend(handles, labels)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.show()
I'm trying to calculate the area between a set of coordinate points joined by straight line segments. The image below is the plot:
The plot is produced using this code:
def data():
Cp_AoA_6=[0.833,-0.833,-0.467,-0.167,0.000,0.000,0.000,0.000,0.000,-0.167,0.833,2.167,1.833,1.500,1.000,0.667,0.500,0.333,0.167,-0.167]
Cp_AoA_16=[-4.667,-1.000,-0.833,-0.500,-0.167,0.000,0.000,0.167,0.333,0.667,-4.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667]
x_c=[0.000,0.028,0.044,0.109,0.207,0.354,0.520,0.696,0.847,1.000,0.000,0.028,0.044,0.109,0.207,0.354,0.520,0.698,0.849,1.000]
return(Cp_AoA_6,Cp_AoA_16,x_c)
def plot():
x_lower = data()[2][0:10]
x_upper = data()[2][10:20]
y_lower = data()[0][0:10]
y_upper = data()[0][10:20]
scale_y = 1
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x_lower,y_lower, '-ob')
ax.plot(x_upper,y_upper, '-ob')
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1],
[r'$0$', r'$0.2$', r'$0.4$', r'$0.6$', r'$0.8$', r'$1$'])
ticks_y = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_y))
ax.yaxis.set_major_formatter(ticks_y)
plt.title("Pressure Versus Postion for an AoA of 6°")
ax.set_xlabel("x/c ($m$)")
ax.set_ylabel("Coefficient of Pressure")
plt.legend()
plt.grid(True)
return(None)
plot()
How do I find the area enclosed by the straight line segments?
Assuming you can accept using numpy, you can get this simply by subtracting the trapezoidal integral of the bottom line from that of the top line: np.trapz(y_upper, x=x_upper) - np.trapz(y_lower, x=x_lower).
Pasted below with your example data which returns area: 0.6880585
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
def data():
Cp_AoA_6=[0.833,-0.833,-0.467,-0.167,0.000,0.000,0.000,0.000,0.000,-0.167,0.833,2.167,1.833,1.500,1.000,0.667,0.500,0.333,0.167,-0.167]
Cp_AoA_16=[-4.667,-1.000,-0.833,-0.500,-0.167,0.000,0.000,0.167,0.333,0.667,-4.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667,0.667]
x_c=[0.000,0.028,0.044,0.109,0.207,0.354,0.520,0.696,0.847,1.000,0.000,0.028,0.044,0.109,0.207,0.354,0.520,0.698,0.849,1.000]
return(Cp_AoA_6,Cp_AoA_16,x_c)
def enclosed_area(x_lower, y_lower, x_upper, y_upper):
return(np.trapz(y_upper, x=x_upper) - np.trapz(y_lower, x=x_lower))
def plot():
x_lower = data()[2][0:10]
x_upper = data()[2][10:20]
y_lower = data()[0][0:10]
y_upper = data()[0][10:20]
scale_y = 1
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x_lower,y_lower, '-ob')
ax.plot(x_upper,y_upper, '-ob')
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1],
[r'$0$', r'$0.2$', r'$0.4$', r'$0.6$', r'$0.8$', r'$1$'])
ticks_y = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_y))
ax.yaxis.set_major_formatter(ticks_y)
plt.title("Pressure Versus Postion for an AoA of 6°")
ax.set_xlabel("x/c ($m$)")
ax.set_ylabel("Coefficient of Pressure")
plt.grid(True)
plt.show()
print("area: " + str(enclosed_area(x_lower, y_lower, x_upper, y_upper)))
return(None)
plot()