In [72]:
# http://kestrel.nmt.edu/~raymond/software/python_notes/paper004.html
# 4.3.4 Vector plots
# linapce funtion in this documentaion,https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linspace.html
# meshgrid from https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html
# plt.quiver from http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver
% matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,11) # the number of row, after meshgrid
y = np.linspace(0,15,16) # the number of column, after meshgrid
print ('===== x axis =====')
print (x)
print ('===== y axis =====')
print (y)
(X,Y) = np.meshgrid(x,y)
print ('===== the location of X coordinate =====')
print (X)
print ('===== the location of Y coordinate =====')
print (Y)
u = 5*X
print ('===== u matrix =====')
print (u)
v = 5*Y
print ('===== v matrix =====')
print (v)
q = plt.quiver(X,Y,u,v,angles ="xy", scale=1000, color='b')
print ("q : {0}".format(q))
#p = plt.quiver(q,1,16.5,5,50, "50 m/s", coordinates='data', color='r')
xl = plt.xlabel("x (km)")
yl = plt.ylabel('y (km)')
plt.show()
In [41]:
# form stackoverflow http://stackoverflow.com/questions/12265234/how-to-plot-2d-math-vectors-with-matplotlib
% matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(3) # the number of row, after meshgrid
y = np.arange(3) # the number of column, after meshgrid
(X,Y) = np.meshgrid(x,y) # python format from https://pyformat.info/
print ("x : {0}".format(x))
print ("y : {0}".format(y))
print ("====X matrix====")
print (X)
print ("====Y matrix====")
print (Y)
u = 5*X
print ("====u matrix====")
print (u)
v = 5*Y
print ("====x matrix====")
print (v)
q = plt.quiver(X,Y,u,v,color="b")
# X : 1D or 2D array, sequence, optional
# The x coordinates of the arrow locations
# Y : 1D or 2D array, sequence, optional
# The y coordinates of the arrow locations
# U : 1D or 2D array or masked array, sequence
# The x components of the arrow vectors
# V : 1D or 2D array or masked array, sequence
# The y components of the arrow vectors
xl=plt.xlabel("x (km)")
yl=plt.ylabel("y (km)")
plt.show()
print ("===== xl =====")
print ("xl : {0}".format(xl))
print ("===== yl =====")
print ("yl : {0}".format(yl))
In [66]:
# stackoverflow of http://stackoverflow.com/questions/12265234/how-to-plot-2d-math-vectors-with-matplotlib
# thai is about how to draw vector in python with matplotlib
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
vec = np.array([[0,0,3,2],[0,0,1,1],[0,0,9,9]])
X, Y, U, V = zip(*vec)
print ("X : {0}".format(X))
print ("Y : {0}".format(Y))
print ("U : {0}".format(U))
print ("V : {0}".format(V))
plt.figure()
ax = plt.gca()
plt.grid()
ax.quiver(X,Y,U,V, angles="xy", scale_units="xy", scale=1)
ax.quiver(0,0,1,1,angles="xy", scale_units="xy",scale=1, color="r")
ax.quiver(0,1,angles="xy", scale_units="xy", scale=1, color="r")
ax.set_xlim([-2,10])
ax.set_ylim([-2,10])
plt.show()
In [70]:
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from numpy import sin, cos
from matplotlib.patches import Rectangle, Circle, PathPatch
import mpl_toolkits.mplot3d.art3d as art3d
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("auto")
ax.set_autoscale_on(True)
#dibujar cubo
r = [-1, 1]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s,e), color="b")
#dibujar punto
ax.scatter([0],[0],[0],color="g",s=100)
#dibujar vector
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
FancyArrowPatch.draw(self, renderer)
print ("ingrese coordenada inicial")
#m=float(raw_input())
a = Arrow3D([0,0],[0,1],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="k")
b = Arrow3D([0,-1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r")
c = Arrow3D([0,0],[0,0],[0,1], mutation_scale=20, lw=1, arrowstyle="-|>", color="b")
d = Arrow3D([0,0],[0,0],[0,-1], mutation_scale=20, lw=1, arrowstyle="-|>", color="g")
e = Arrow3D([0,1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="c")
f = Arrow3D([0,0],[0,-0.5],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="m")
ax.add_artist(a)
ax.add_artist(b)
ax.add_artist(c)
ax.add_artist(d)
ax.add_artist(e)
ax.add_artist(f)
plt.show()
In [71]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
point = np.array([1, 2, 3])
normal = np.array([1, 1, 2])
# a plane is a*x+b*y+c*z+d=0
# [a,b,c] is the normal. Thus, we have to calculate
# d and we're set
d = -point.dot(normal)
# create x,y
xx, yy = np.meshgrid(range(10), range(10))
# calculate corresponding z
z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]
# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z)
plt.show()