绘图矢量添加?

时间:2014-10-15 11:16:12

标签: python

直截了当:

vector images

假设我有一堆向量A,B和C,它们的水平和垂直分量分别分成两个数组:

xdat = np.array([x1,x2,x3])
ydat = np.array([y1,y2,y3])

我想:

  1. 根据数组中给出的数据绘制每个单独的向量

  2. 绘制顺序向量添加

  3. 绘制和矢量A + B + C

  4. 我对python很新,所以这个东西对我来说很难。我尝试通过简单地做plt.plot(xdat,ydat)来绘制1,但我看到的数字看起来不对。理想情况下,我想学习适用于N个向量的这种东西的一般方法。

1 个答案:

答案 0 :(得分:0)

以下代码打印一个向量和,知道其中一个组件和总数..您可以轻松地将其调整为您的特定用户案例..

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

def plotVectorChart_inner(origin,end1,endt,xlabel,ylabel,filename):
 a=plt.figure()
 ax = plt.gca()
 fig = plt.gcf()
 flag_2d = True
 if(origin[0] == end1[0] == endt[0]):
   flag_2d = False;
   fig.set_size_inches(6,10)
 else:
   fig.set_size_inches(10,10)
 end2 = (endt[0]-end1[0]+origin[0],endt[1]-end1[1]+origin[1])
 minx = min(origin[0],end1[0],end2[0],endt[0])
 maxx = max(origin[0],end1[0],end2[0],endt[0])
 miny = min(origin[1],end1[1],end2[1],endt[1])
 maxy = max(origin[1],end1[1],end2[1],endt[1])
 centre = (((maxx-minx)/2)+minx,((maxy-miny)/2)+miny)
 X  = (origin[0], origin[0], origin[0])
 Y  = (origin[1], origin[1], origin[1])
 X2 = (end1[0]-origin[0], endt[0]-origin[0], end2[0]-origin[0]) 
 Y2 = (end1[1]-origin[1], endt[1]-origin[1], end2[1]-origin[1])
 C  = (255,010,150) # ? colour codes, but didn't got it

 ax.quiver(X,Y,X2,Y2,C,angles='xy',scale_units='xy',scale=1, width=0.008)
 x  = (end1[0],end2[0])
 y  = (end1[1],end2[1])
 x2 = (endt[0]-end1[0], endt[0]-end2[0])
 y2 = (endt[1]-end1[1], endt[1]-end2[1])

 if(flag_2d):
   ax.quiver(x,y,x2,y2,angles='xy',scale_units='xy',scale=1, width=0.005, color='gray')
   ax.set_xlim([minx- (centre[0]-minx)*0.4, maxx + (maxx-centre[0])*0.4])

 ax.set_ylim([miny- (centre[1]-miny)*0.4, maxy + (maxy-centre[1])*0.4])

 plt.xlabel(unicode(xlabel, 'utf_8'))
 plt.ylabel(unicode(ylabel, 'utf_8'))
 # Uncomment the following lines if you want to display instead of save the figure..
 #plt.draw()
 #plt.show()
 plt.savefig(filename, dpi=300)

plotVectorChart_inner((9.791,1609),(9.441,5489),(9.760,4645),"Area [M sq.km]", "Volume [M c.m.]","vol_area_broadL.png")
plotVectorChart_inner((4.317,926),(4.667,1943),(4.349,1510),"Area [M sq.km]", "Volume [M c.m.]","vol_area_con.png")
plotVectorChart_inner((14.108,2535),(14.108,7432),(14.108,6155),"Area [M sq.km]", "Volume [M c.m.]","vol_area_tot.png")

这会生成如下图: output