Skip to content

Instantly share code, notes, and snippets.

@davidrajm
Last active April 23, 2021 17:51
Show Gist options
  • Save davidrajm/3085724424e8741a3a852090065d14bc to your computer and use it in GitHub Desktop.
Save davidrajm/3085724424e8741a3a852090065d14bc to your computer and use it in GitHub Desktop.
This helper script will give a function to plot the given vectors. One can input the 2-tuple vectors in an array and get the plot of them.
def plot_vectors(vectors, title=None):
import numpy as np
from matplotlib import pyplot as plt
# Initiate the plot
fig, ax = plt.subplots(figsize = (5,5))
# Draw each vector as quiver plot
for x_cor, y_cor in vectors:
ax.quiver(0,0,x_cor,y_cor,
scale=1,angles='xy',
scale_units='xy',color='steelblue')
# Calculate the limit
limit = np.max(np.abs(vectors))*1.25
ax.set_xlim([-limit, limit])
ax.set_ylim([-limit, limit])
ax.set_aspect('equal')
# Make the grid visible
ax.grid(True, linewidth = 0.5, alpha= 0.85)
# show x-y axis in the center, and hide the frames
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
if title != None:
plt.title(title)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment