【问题标题】:How to plot the resultant vector如何绘制结果向量
【发布时间】:2013-09-19 16:02:12
【问题描述】:

我正在处理一个带有矢量的项目,但现在我想计算并绘制具有更大价值的矢量的合成矢量,例如,在红色和蓝色之间。这是代码:

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()

对此有什么帮助吗? 谢谢!

【问题讨论】:

  • 我不明白你的问题。 究竟你遇到了什么问题?
  • 我必须在箭头“b”和“c”之间绘制结果。
  • 好的,那么你的代码有什么问题?
  • 我已经绘制了向量,但我不知道如何绘制它们之间的结果!现在清楚了吗?

标签: python matplotlib


【解决方案1】:

您的问题包含答案,因为您有一个类可以在 3 空间中的任意点之间绘制箭头

 from itertools import cycle
 a = [0, 0, 1]
 b = [1, 0, 1]
 orig = [0, 0, 0]
 for (_b, _e), _c in zip([[orig, a], [orig, b], [a, b]], cycle(['m', 'r', 'g', 'b'])):
     xs, ys, zs = zip(_b, _e)
     res = Arrow3D(xs, ys, zs, mutation_scale=20, lw=1, arrowstyle="-|>", color=_c)
     ax.add_artist(res)

【讨论】:

  • 太好了!但是为什么它会改变箭头的颜色,所以我试图将结果绘制为黄色但我做不到! :S
  • @DiegoMartínezGiardini 这对我来说毫无意义。
  • 如果我改变向量“e”的值,那么结果不会被它的ef改变,它保持相同的值,那么我怎样才能自动做到呢?
  • 我还是不明白。
猜你喜欢
  • 1970-01-01
  • 2021-12-16
  • 2020-01-01
  • 1970-01-01
  • 2022-12-16
  • 2022-01-27
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多