Hii experts i need to join the line between the points obtained using for loop .But only scatter plot(plt.scatter) is working not the plt.plot option in matplotlib
my prograamme is
import numpy as np
import matplotlib.pyplot as plt
f=np.arange(1,5,1)
for i in f:
sum = 0
for m in np.arange(1,6,1):
x=((4.6)*(m*2)*(5)**2)*(i)/62
print(x)
sum += x
print(i,sum)
plt.scatter(i,sum)
plt.show()
Try this :
import numpy as np
import matplotlib.pyplot as plt
f=np.arange(1,5,1)
x_values = []
y_values = []
for i in f:
sum = 0
for m in np.arange(1,6,1):
x=((4.6)*(m*2)*(5)**2)*(i)/62
sum += x
#plt.scatter(i,sum)
x_values.append(i)
y_values.append(sum)
plt.plot(x_values, y_values, marker='o')
plt.show()
Related
I am using Basemap with a 3D graph to display ray paths. I would like to implement one of basemap's topo, shaded relief or bluemarble layers but I am running into the same issue over and over again:
NotImplementedError: It is not currently possible to manually set the aspect on 3D axes
I have already implemented fixed_aspect=False into calling the basemap
and have also tried ax.set_aspect('equal') which gives me the same error
I am using matplotlib ==2.2.3
Here is my code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
import numpy as np
from io import StringIO
import re
f = open("best-working4D.ray_paths", 'r') #125 waves - 125 "lines"
data = f.read()
lines = data.split('\n ')
fig = plt.figure()
ax = plt.axes(projection='3d')
extent = [300, 360, 50, 75]
bm = Basemap(llcrnrlon=extent[0], llcrnrlat=extent[2],
urcrnrlon=extent[1], urcrnrlat=extent[3], resolution='l', fix_aspect= False)
bm.bluemarble()
for i in range(1, 119):
wave = lines[i]
j = wave.split('\n')
k = []
for i in j:
k.append(i.split())
x=[]
y=[]
z=[]
n= 0
for m in k[1:]:
x.append(m[0])
y.append(m[1])
z.append(m[2])
x= np.array(x).astype('float32')
y= np.array(y).astype('float32')
z= np.array(z).astype('float32')
ax.plot3D(x,y,z, color='red')
##Plotting Tropopause
T_hi = 20
xx, yy = np.meshgrid(range(300,360), range(50,75))
zz = yy*0 + T_hi
ax.plot_surface(xx, yy, zz, alpha=0.15)
ax.set_xlabel("Latitude [deg]")
ax.set_ylabel("Longitude [deg]")
ax.set_zlabel("Altitude [km]")
ax.add_collection3d(bm.drawcoastlines(linewidth=0.25))
plt.show()
The basemap IS working for the bm.drawcoastlines just nothing else.
IMAGELINK
I would greatly appreciate any ideas!
I have tried a sample code where I used my figure to plot:
import matplotlib.pyplot as plt
import numpy as np
l = [1.10867,1.10894,1.10914,1.10926,1.10930,0.00000,0.00000,0.00000,0.00000,0.00000,1.10867,1.10894,1.10914,1.10926,1.10930]
x = np.arange(len(l))
y = np.array(l)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, 'r-') # Returns a tuple of line objects, thus the comma
fig.canvas.draw()
fig.canvas.flush_events()
The figure comes out to be the following:
But I am expecting to have a figure this:
The expected figure is obtained when I removed the zeroes from the list. But I want to if there is a way to plot without removing zeroes from the list and still the figure looks like the expected one.
Please share any thoughts.
You can interpolate the points where y's are equal to 0 and plot:
import matplotlib.pyplot as plt
import numpy as np
l = [1.10867,1.10894,1.10914,1.10926,1.10930,0.00000,0.00000,0.00000,0.00000,0.00000,1.10867,1.10894,1.10914,1.10926,1.10930]
x = np.arange(len(l))
y = np.array(l)
y_interp = np.interp(x, x[np.where(y != 0)], y[np.where(y != 0)])
plt.plot(x, y_interp)
My data on x goes from 3MHz to 12MHz, I don't want show all those points on the x-axis instead I want to show an interval of from 3MHz to 12MHz spaced out one 1MHz a part.
Here is an example code.
import numpy as np
import matplotlib.pyplot as plt
x_array = np.arange(3000000, 12000000)
y_array = np.arange(3000000, 12000000)
plt.plot(x_array, y_array)
plt.show()
I want the x-axis first marker to to be 3MHz second marker to be 4MHz and so on up to 12MHz.
You want to have MHz on the x-axis? then use mega Hertz in the definition of the x array and multiply by 10⁶ when you use the array as a frequency in a subsequent calculation
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(3, 12, 901)
f = x*1E6
def H(f) : return f
plt.plot(x, H(f))
plt.xticks(range(3, 13))
plt.xlabel('Frequency/MHz')
plt.ylabel('Transfer function')
plt.grid()
plt.show()
You can change the matplotlib xticks using the following:
plt.xticks(np.arange(3000000, 12000000, step=1000000))
Defining the step will ensure you have 1MHz space.
You can find more here:
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xticks.html
Edit:
If you want to have the MHz on the x ticks, you can do the following:
import numpy as np
import matplotlib.pyplot as plt
x_array = np.arange(3000000, 12000000)
y_array = np.arange(3000000, 12000000)
plt.plot(x_array, y_array)
plt.xticks(ticks=np.arange(3, 13)*1e6, labels=[str(t) + "MHz" for t in np.arange(3, 13)])
plt.show()
Output: https://i.stack.imgur.com/JdIM3.png
Otherwise, you can do as mentioned in the other answer:
import numpy as np
import matplotlib.pyplot as plt
x_array = np.arange(3000000, 12000000)
y_array = np.arange(3000000, 12000000)
plt.plot(x_array, y_array)
plt.xticks(ticks=np.arange(3, 13)*1e6, labels=np.arange(3, 13))
plt.xlabel("Frequency (MHz)")
plt.show()
Output: https://i.stack.imgur.com/Vc1gb.png2
i have written this code
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
r=np.arange(1,2)
theta=np.linspace(0,np.pi/2)
x=r*np.cos(theta)
y=r*np.sin(theta)
plt.plot(x,y)
plt.show()
and get this graph.
but i want to get the graph below.
i'm confused about how to set a proper range of r.
i want to set r's range 1<=r<=2, but don't know how to do that.
how can i modify my code?
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
theta = np.linspace(0,np.pi/2)
for i in range(1,3):
x = []
y = []
for t in theta:
if t == 0:
y.append(0)
x.append(3-1)
elif t == np.pi/2:
y.append(3-1)
x.append(0)
else:
x.append(i*np.cos(t))
y.append(i*np.sin(t))
plt.plot(x,y)
plt.show()
I have a dataset looking like this:
1 38.7114 -7.92482 16.4375 0.2
...
I'd like to make a 3D scatter plot. I've done it using cartesian coordinates. How I can do it using geographic coordinates? Any hint?
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sys
from mpl_toolkits.basemap import Basemap
ID=[]
Latitude=[]
Longitude=[]
Depth=[]
cluster1='data1'
with open(cluster1) as f:
lines = f.readlines()
for line in lines:
items = line.strip().split()
lat = float(items[1])
lon = float(items[2])
dep = float(items[3])
mag = float(items[4])
Latitude.append(lat)
Longitude.append(lon)
Depth.append(dep)
ID.append(mag)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(Longitude, Latitude, Depth, c=ID, marker='o')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Depth (km)')
ax.invert_zaxis()
cb = fig.colorbar(p,label='Magnitude')
plt.savefig('plot1.png')