Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
614 views
ubuntu2004
Kernel: Python 3 (system-wide)
import matplotlib.pyplot as plt x = [-1, 150] y = [-3, 72] plt.figure(figsize=(10,10)) plt.scatter(x,y) plt.xlim(-5, 200) plt.ylim(-10, 200)
(-10.0, 200.0)
Image in a Jupyter notebook
import matplotlib.image as mpi img = mpi.imread('atlantic-basin.png') plt.imshow(img)
<matplotlib.image.AxesImage at 0x7fa698cd23d0>
Image in a Jupyter notebook
# latitude/longitude co-ordinates of edges of map left = -90 right = -17.06 bottom = 0 top = 45 # dimensions of image in pixels width = 964 height = 600 # lat/long of New York lat_NY = 40.7 long_NY = -74.0 # Convert from lat/long to pixel coordinates x_NY = (long_NY - left)*width/(right-left) y_NY = (height*(1 - (lat_NY - bottom)/(top-bottom))) # Print the pixel co-ordinates of New York print("NY x pos:", x_NY) print("NY y pos:", y_NY)
NY x pos: 211.46147518508363 NY y pos: 57.3333333333333
plt.imshow(img) plt.scatter(x_NY, y_NY) plt.text(x_NY, y_NY, "New York", color="white")
Text(211.46147518508363, 57.3333333333333, 'New York')
Image in a Jupyter notebook
import csv # load the code library for reading CSV files x = [] y = [] # Open the data file with open("irma.csv") as f: reader = csv.reader(f) # skip the first line of the file next(reader) for row in reader: latitude = float(row[2]) longitude = float(row[3]) # translate to pixel coordinates x.append((longitude - left)*width/(right-left)) y.append(height*(1 - (latitude - bottom)/(top-bottom))) print("x-coords:", x) print("y-coords:", y)
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_1299/3065243181.py in <module> 10 next(reader) 11 for row in reader: ---> 12 latitude = float(row[20]) 13 longitude = float(row[300]) 14 IndexError: list index out of range
plt.imshow(img) plt.scatter(x, y, color='pink')
<matplotlib.collections.PathCollection at 0x7fa698b1d310>
Image in a Jupyter notebook
import matplotlib.animation as animation fig = plt.figure() ax = plt.axes() patch = plt.Circle((0, 0), 20, fc='y') def init(): patch.center = (20, 20) ax.add_patch(patch) return patch, def animate(i): patch.center = (x[i], y[i]) return patch, anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x), interval=20, blit=True) plt.imshow(img,zorder=0) anim.save('hurricane_irma.mp4', writer = 'ffmpeg', fps=30)
Image in a Jupyter notebook