在Python中使用gmplot绘制散点图时出错

时间:2019-07-04 23:18:39

标签: python gmplot

我想在Python中使用gmplot lib绘制散点图。 但是,我想使每个绘图(每个坐标)的marker_size不同。我写了一个代码,但出现了这个错误:

TypeError: unsupported operand type(s) for /: 'tuple' and 'float'

marker_size的值是整数。

这是代码:

from gmplot import gmplot
import pandas as pd

df = pd.read_csv('tripdataNEW.csv')

def geolocation_scatter(input_data, file_name):

    #extract only coordinates and marker_size and convert them to tuple
    coordinates_data = input_data[["start station latitude", "start station longitude", "marker_size"]]
    coordinates_data = coordinates_data.apply(tuple, axis=1)

    #add tuple coordinates to list
    coordinate_list = []
    for coord in coordinates_data:
        coordinate_list.append(coord)

    # Scatter points
    coordinate_lats, coordinate_lons, marker_size = zip(*coordinate_list)

    # Defult map location
    gmap = gmplot.GoogleMapPlotter(40.7423543, -73.98915076, 13)

    #Scatter dots on the map
    gmap.scatter(coordinate_lats, coordinate_lons, 'red', size = marker_size, marker = False)

    # Draw
    name = str(file_name)+".html"
    gmap.draw(name)

print(geolocation_scatter(df ,"SCATTER"))

1 个答案:

答案 0 :(得分:0)

我认为您的问题出在线路上

gmap.scatter(coordinate_lats, coordinate_lons, 'red', size = marker_size, marker = False)

虽然您可以传递带有“ coordinate_lats”和“ coordinate_lons”的迭代器,但我相信arg“ size”期望的是float参数,而不是元组(因此出现错误)。我不确定它的设计是否允许每个单独的点涂上不同的颜色,可能期望整个系列的大小相同。您可能只需循环调用即可获得所需的效果。像这样:

.
.
.
# Scatter points
coordinate_lats, coordinate_lons, marker_size = zip(*coordinate_list)

# Defult map location
gmap = gmplot.GoogleMapPlotter(40.7423543, -73.98915076, 13)

#Scatter dots on the map
for coord_lat, coord_lon, m_size in zip(coordinate_lats, coordinate_lons, marker_size):
    gmap.scatter(coord_lat, coord_lon, 'red', size = m_size , marker = False)
.
.
.