Giter VIP home page Giter VIP logo

mapsplotlib's Introduction

Mapsplotlib

Build

Custom Python plots on a Google Maps background. A flexible matplotlib like interface to generate many types of plots on top of Google Maps.

This package was renamed from the legacy tcassou/gmaps due to an unfortunate conflict in names with a package from Pypi.

Setup

Simply install from pip:

pip install mapsplotlib

You need to have a Google Static Maps API key, go to https://console.cloud.google.com/google/maps-apis, create a project, enable Google Static Maps API and get your API key. Billing details have to be enabled for your account for the API calls to succeed. Before plotting maps, you'll have to register your key (only once for each session you start):

from mapsplotlib import mapsplot as mplt

mplt.register_api_key('your_google_api_key_here')

# all plots can now be performed here

Examples

Marker Plots

Simply plotting markers on a map. Consider a pandas DataFrame df defined as follows:

|   | latitude | longitude |  color |  size | label |
|---|----------|-----------|--------|-------|-------|
| 0 |  48.8770 |  2.30698  |  blue  |  tiny |       |
| 1 |  48.8708 |  2.30523  |   red  | small |       |
| 2 |  48.8733 |  2.32403  | orange |  mid  |   A   |
| 3 |  48.8728 |  2.30491  |  black |  mid  |   Z   |
| 4 |  48.8644 |  2.33160  | purple |  mid  |   0   |

Simply use (assuming mapsplot was imported already, and your key registered)

mplt.plot_markers(df)

will produce

Marker Plot

Density Plots

The only thing you need is a pandas DataFrame df containing a 'latitude' and a 'longitude' columns, describing locations.

mplt.density_plot(df['latitude'], df['longitude'])

Density Plot

Heat Maps

This time your pandas DataFrame df will need an extra 'value' column, describing the metric you want to plot (you may have to normalize it properly for a good rendering).

mplt.heatmap(df['latitude'], df['longitude'], df['value'])

Heat Map

Scatter Plots

Let's assume your pandas DataFrame df has a numerical 'cluster' column, describing clusters of geographical points. You can produce plots like the following:

mplt.scatter(df['latitude'], df['longitude'], colors=df['cluster'])

Scatter Plot

Polygon Plots

Still with the same DataFrame df and its 'cluster' column, plotting clusters and their convex hull.

mplt.polygons(df['latitude'], df['longitude'], df['cluster'])

Polygons Plot

Polygon Plots

Given a DataFrame df with 'latitude' & 'longitude' columns, plotting a line joining all (lat, lon) pairs (with the option to close the line).

mplt.polyline(df['latitude'], df['longitude'], closed=True)

Polyline Plot

More to come!

Requirements

  • pandas >= 0.13.1
  • numpy >= 1.8.2
  • scipy >= 0.13.3
  • matplotlib >= 1.3.1
  • requests >= 2.7.0
  • requests>=2.18.4
  • pillow>=4.3.0

mapsplotlib's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mapsplotlib's Issues

Heatmap colours are very dim

Hello Thomas,

Thanks for the great package. It helps me a lot. I just want to know whether I can increase the colour intensities in the heatmap function. I could not find a parameter to do that. Appreciate your help. Cheers!

unable to import mapsplotlib

While I was importing the library

from mapsplotlib import mapsplot as mplt

It prompts me the error

File "C:\ProgramData\Anaconda3\lib\site-packages\mapsplotlib\mapsplot.py", line 213
print '[WARN] Cannot draw polygon for cluster {} - only {} samples.'.format(c, in_polygon.sum())

SyntaxError: invalid syntax

Please advise what to do

HTTP firbidden error

#%matplotlib inline
import pandas as pd

from mapsplotlib import mapsplot as mplt

mplt.register_api_key('AIza*****************yJDcvyus')
df = pd.read_csv(r'C:\Users\leo_a\Daily_Learning\Thesis_Work\AoT_Chicago.complete.2018-08-08.from-2018-08-02-to-2018-08-08\nodes.csv')
df['latitude']=df['lat']
df['longitude']=df['lon']
new_df=df[['latitude', 'longitude']].copy()
mplt.plot_markers(new_df)

I am trying to mark in the df on gmap but the result of this is

HTTPError: 403 - Forbidden. b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x05\x00\x00\x00\x05\x00\x08\x06\x00\x00\x00\x18\xe4\xff\xf7\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00 \x00IDATx\xda\xec\xdd\xd9sV\xf7\x99\xe0\xf1GB+\x8b\x84\x80\xb0\x98\xd5\x04\xca,2\x8b'C&\x86\x01\xdcJ\xc7\xdb\xc4\x0e\xa9\xdcLWMn\xa6\xa7\xa7\xab\5]5=\x9e\x9at\xdf\xe4\xba's\xd7\xfe\x17:S\x9d\xc5cw\xec$@\x8cB\x19\xbcT\xc5\x08\x0c\x02\x1c\x9cA\xb2\x8d1\x9b$\x10Bz\xb5\xcdEw<ql\xbf\xafdK:\xf2\xc3\xe7sC\x99\xdf\xef}\xf5\x9c\x83Q\xa9\xbe\x9c\xf7\x9c\xaa\x8b\x17/\x8e\x07\x00\x00\x00\x00\x90Ru\xd1\x03\x00\x00\x00\x00\x00\xd3G\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\x04@\x00\x00\x00\x00HL\x00\x04\x00\x00\x00\x80\xc4\[email protected]

I am not sure what I am doing something wrong , please guide

Rotate map

This is a feature request.

Can we specify which direction is "up" on the map? I don't always want North to point upward.

Set upper limit in `get_zoom` function.

If the list of latitudes and longitudes passed to get_zoom has only one point, or they are all at the same point, the return value of this function goes to infinity, causing OverflowError: cannot convert float infinity to integer. This should be caught or prevented, and a maximum zoom value returned. OR: A more descriptive error should be raised.

Zoom in and out

This is a feature request. Zooming in and out is essential to visualizing data on different scales. Is there a way to incorporate the same in mapsplotlib as well?

Integrate with GeoPandas?

I think it would be awesome if you integrated this with GeoPandas. It would make both packages more powerful. Haven't really looked under the hood, but if you're already working with dataframes I imagine it wouldn't be much of a stretch.

Integrate into subplot

Hi tcassou,

Thank you very much for the work!

I am new to Python. I invoke mplt.scatter function in a for loop, each time inputting with the corresponding row of my dataframe. 2 problems I encounter so far:

  1. When I pass the dataframe's ith row in the for loop mplt.scatter(df_geo['latitude'][i],df_geo['longitude'][i]) it throws the following error "numpy.float64' object has no attribute 'index". I wanted to put ith dot on the map within the loop.

  2. In the for loop, each time it creates a figure, how can I put it one of my subplots and update the map in the for loop.

Cheers,

AttributeError: module 'gmaps' has no attribute 'plot_markers'

Trying to replicate the example fails:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gmaps
>>> gmaps.__version__
'0.6.1'
>>> import pandas as pd
>>> pd.__version__
'0.20.3'

>>> latitude = [48.8733, 48.8728]
>>> longitude = [2.32403, 2.30491]
>>> color = ['orange', 'black']
>>> size = ['mid', 'mid']
>>> label = ['A', 'Z']
>>> df = pd.DataFrame({'latitude': latitude, 'longitude': longitude, 'color': color, 'size': side, 'label': label})
>>> df
    color label  latitude  longitude size
0  orange     A   48.8733    2.32403  mid
1   black     Z   48.8728    2.30491  mid
>>> import gmaps
>>> 
>>> gmaps.plot_markers(df)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'gmaps' has no attribute 'plot_markers'

Polyline?

Heya,
You have polygons, but I was surprised to see you don't support Polylines.
Would this be a feature worth adding?
Thanks.

Unusable Map Results

Using the variety of different plotting tools offered by your (very awesome, mind you) tool, I'm unable to see any details on the map whatsoever aside from the plots and very basic water features. Any idea what the cause may be? There's no output indicating that my inputs aren't correct.

image

Image positioning and zooming

Hi,

Is there any way to manually specify:

  • coordinates for that will be located at the center of the image,
  • zooming ratio,
  • or maybe ROI?

For example, for the attached image it seems like a good idea to move the center higher to the North and zoom in.

download

And, by the way, thanks for the very cool tool!

Heatmap returns error: UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)

For some reason, using the heatmap function returns this error
'UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)'
, when the function worked fine a few days ago.

After doing some investigating, I was able to figure out with what I believe may have been the issue:

The GoogleStaticMapsAPI.map function uses one's api key to call an image of the map, which returns the 403 error. I was able to side step this issue by customizing my own version of the GoogleStaticMapsAPI.map function without appending api key to the end of the url.

Is this issue happening to other people too? How can one sidestep this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.