如何使用python在点之间绘制线

时间:2016-04-22 04:12:30

标签: python python-2.7 matplotlib

如何在2点或3点之间画线。 我有2个文本文件,第一个文本文件是每个点的posisition列表。

point    long    lat
A        115     12
B        89      13
C        100     13
etc.

,第二个文件是这样的:

3, 4
A, B, C
R, X, Y
V, P, O
J, M, N
2, 3
Q, S
H, K
T, W
4, 1
E, D, F, G

我想绘制像这张照片的行:

What I wannt to draw

实际上,我对我的代码不确定。这是我的代码::

import psycopg2
import psycopg2.extensions
import matplotlib.pyplot as plt
import itertools
import collections
import numpy as np

def readRules(_dir):
  #_dir = r'D:\s2\semester 3\tesis\phyton\hasil\Hasil_20160116_09.12.11'
   mydict = {}

   with open(os.path.join(_dir, 'rule3.csv'), 'rb') as testfile:
     for line in testfile:
        # first line is the next record "matrix size"
        columns, rows = (int(x) for x in strip_comment(line).split(','))
        # next line is the header for this record
        key = tuple(strip_comment(next(testfile)).split(','))
        # the next lines are the rows for this record
        vals = [tuple(int(x) for x in     strip_comment(next(testfile)).split(','))
            for _ in range(rows)]
        mydict[key] = vals
    #print(mydict)
return mydict




data=getDataReference(cur)   # to get location for each point
myPoints={}
myLines=[]
mydict=readRules(_dir)

# print "value :", mydict.values()
# print "key:", mydict.keys()

for value in mydict.values():
    for x in value:
        for s in range(len(x)):
            myPoints[x[s]]= data[x[s]][0]
            #print x[s]
        if len(x)>1:
            myLines.append(x)

myPoints_reversed = collections.defaultdict(list)
for number, string in myPoints.items():
    myPoints_reversed[string].append(number)

colors = plt.cm.Spectral(np.linspace(0, 1, len(myPoints_reversed)))

myplt={}
for k, col in zip(myPoints_reversed.keys(),colors):
    Long=[]
    Lat=[]
    for x in myPoints_reversed[k]:
        Long.append(data[x][2])
        Lat.append(data[x][1])
    myplt[k] =plt.plot( Lat,Long , 'o', markerfacecolor=col, markeredgecolor='k', markersize=10, label=k)

#plt.legend(myplt,myPoints_reversed.keys(),loc=3,ncol=2, mode="expand", borderaxespad=0.)

plt.legend(loc =3,ncol=2, borderaxespad=0.)

#print myLines

#plt.plot(getListLat(myPoints.keys(),data), getListLong(myPoints.keys(),data),'o')

for point in myPoints:
    #plt.annotate(getName(point,data), xy=getLatLong(point,data)) #Print name of point
    plt.annotate(point, xy=getLatLong(point,data))
for line in myLines:
    plotLine(line[0],line[1],data)
plt.show()

0 个答案:

没有答案