有没有一种方法可以使用Python在一个图形上绘制多个多边形

时间:2019-07-03 23:40:54

标签: python plot

我正在学习pythons库gmplot,我想知道是否有一种方法可以用这个库绘制多个多边形。 这是我为绘制一个多边形而编写的代码:

from gmplot import gmplot

gmap5 = gmplot.GoogleMapPlotter(10, 10, 7)

x = [5, 10, 10, 5]
y =[5, 5, 15, 15]
gmap5.scatter(x, y, '# FF0000', size = 40, marker = False) 

# polygon method Draw a polygon with 
gmap5.polygon(x, y, color = 'red') 

gmap5.draw( "map.html" )

但是当我要绘制多个多边形时,出现错误:

TypeError: can't multiply sequence by non-int of type 'float'

这是我编写的代码:

from gmplot import gmplot

gmap5 = gmplot.GoogleMapPlotter(10, 10, 7)

x = [[5, 10, 10, 5], [15, 15, 19, 25]]
y =[[5, 5, 15, 15], [16, 17, 25, 15]]

gmap5.scatter(x, y, '# FF0000', size = 40, marker = False) 

# polygon method Draw a polygon with 
gmap5.polygon(x, y, color = 'red') 

gmap5.draw( "map.html" )

我也尝试使用此方法:x = [(5, 10, 10, 5), (15, 15, 19, 25)] y =[(5, 5, 15, 15), (16, 17, 25, 15)],但它给了我同样的错误

1 个答案:

答案 0 :(得分:2)

所以,我认为您的问题是gmap5.scatter期望有一个经度和纬度点列表,并且您正在传递一个列表列表。通过遍历您的x&y列表中的列表,一次绘制一个列表,我设法使其起作用。

尝试一下

from gmplot import gmplot

gmap5 = gmplot.GoogleMapPlotter(10, 10, 7)

x = [[5, 10, 10, 5], [15, 15, 19, 25]]
y =[[5, 5, 15, 15], [16, 17, 25, 15]]

for lat, long in zip(x,y):
    gmap5.scatter(lat, long, '# FF0000', size = 40, marker = False)

    # polygon method Draw a polygon with
    gmap5.polygon(lat, long, color = 'red')

gmap5.draw( "map.html" )

通过这样做,您应该能够绘制多个多边形。

例如,这也对我有用:

from gmplot import gmplot

gmap5 = gmplot.GoogleMapPlotter(10, 10, 7)

x = [[5, 10, 10, 5], [15, 15, 19, 25], [10, 20, 25, 50, 10]]
y = [[5, 5, 15, 15], [16, 17, 25, 15], [25, 30, 35, 40, 25]]

for lat, long in zip(x,y):
    gmap5.scatter(lat, long, '# FF0000', size = 40, marker = False)

    # polygon method Draw a polygon with
    gmap5.polygon(lat, long, color = 'red')

gmap5.draw( "map.html" )

只要x和y中的相应列表具有相同的长度,就不会有任何问题。