在Python中使用gmplot绘制散点图时出错 [英] Error when plotting scatter plot with gmplot in Python

查看:310
本文介绍了在Python中使用gmplot绘制散点图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to plot scatter plot using gmplot lib in Python. However, I want to make marker_size different for each plot (for each coordinate). I wrote a code and I got this error:

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

推荐答案

我认为您的问题出在以下方面:

I think your problem is with the line:

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

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

While you can pass an iterable with "coordinate_lats" and "coordinate_lons" I believe the arg "size" is expecting a float parameter, not a tuple (hence the error). I not sure it was designed to allow for every individual point to be colored different, it's probably expecting the whole series to be the same size. You could probably just loop over this call to get your desired effect. Like so:

.
.
.
# 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)
.
.
.

这篇关于在Python中使用gmplot绘制散点图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆