In [1]:
import pandas as pd

from lets_plot.geo_data import *
from lets_plot import *

LetsPlot.setup_html()
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
In [2]:
mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg_df
Out[2]:
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
... ... ... ... ... ... ... ... ... ... ... ... ...
229 230 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize
230 231 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize
231 232 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize
232 233 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize
233 234 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 12 columns

In [3]:
p = (ggplot(mpg_df, aes(x='displ', y='cty', fill='drv', size='hwy'))
     + scale_size(range=[5, 15], breaks=[15, 40])
     + ggsize(600, 350)
    )
In [4]:
# Tooltips: split a line using '\n'

p + geom_point(shape=21, color='white',
                   tooltips=layer_tooltips()
                          .line('@manufacturer \n@model')
                          .line('@|@class')
                          .line('@|@year'))
Out[4]:
In [5]:
# Use '\n' in 'format'

p + geom_point(shape=21, 
               color='white', 
               tooltips=layer_tooltips().format('^size', '{.0f} \n(mpg)'))
Out[5]:
In [6]:
# Tooltip title

# Add title to default tooltip lines

p + geom_point(shape=21, color='white',
                   tooltips=layer_tooltips().title('@manufacturer @model'))
Out[6]:
In [7]:
# Add title to user specified multiline tooltip 

p + geom_point(shape=21, 
               color='white',
               tooltips=layer_tooltips(['class', 'year']).title('@manufacturer @model'))
Out[7]:
In [8]:
# Multi-line title 

p + geom_point(shape=21, 
               color='white',
               tooltips=layer_tooltips()
                   .line('@|@class')
                   .line('drive train|@drv')
                   .line('@|@year')
                   .title('Car info: \n@manufacturer @model'))
Out[8]:
In [9]:
cities = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/cities.csv")
cities.head()
Out[9]:
Unnamed: 0 name link coutry_id latitude longitude text
0 0 Algiers /wiki/Algiers 1 36.776389 3.058611 — with nearly 3 million inhabitants, Algiers ...
1 1 Annaba /wiki/Annaba 1 36.900000 7.766667 — a town with 200,000 inhabitants in the east...
2 2 Batna /wiki/Batna 1 35.550000 6.166667 NaN
3 3 Bechar /wiki/Bechar 1 31.633333 -2.200000 — small city in the Sahara, not far from the ...
4 4 Constantine /wiki/Constantine 1 36.365000 6.614722 - Algeria's 3rd largest city with a canyon go...
In [10]:
czech_cities = cities.loc[cities['coutry_id'] == 144]
czech_cities
Out[10]:
Unnamed: 0 name link coutry_id latitude longitude text
1044 0 Prague /wiki/Prague 144 50.083333 14.416667 () — the capital and largest city of the Czec...
1045 1 Brno /wiki/Brno 144 49.200000 16.616667 — the largest city in Moravia and its former ...
1046 2 České Budějovice /wiki/%C4%8Cesk%C3%A9_Bud%C4%9Bjovice 144 48.974722 14.474722 – attractive large city in South Bohemia
1047 3 Český Krumlov /wiki/%C4%8Cesk%C3%BD_Krumlov 144 48.816667 14.316667 — beautiful old town in South Bohemia with th...
1048 4 Karlovy Vary /wiki/Karlovy_Vary 144 50.233333 12.866667 () — historic (and biggest Czech) spa resort,...
1049 5 Kutná Hora /wiki/Kutn%C3%A1_Hora 144 49.950000 15.266667 — historical town with famous Saint Barbara c...
1050 6 Olomouc /wiki/Olomouc 144 49.600000 17.250000 ( or ) — riverside university town with a tho...
1051 7 Ostrava /wiki/Ostrava 144 49.835556 18.292500 — a vibrant local subculture and long history...
1052 8 Pilsen /wiki/Pilsen 144 49.747500 13.377500 () — home of the original Pilsner Urquell bee...
In [11]:
country_gcoder = geocode_countries(['Czech'])
country_gcoder.get_geocodes()
Out[11]:
id country found name centroid position limit
0 51684 Czech Česko [15.5282667270321, 49.8037022352219] [12.0905895531178, 48.5518079996109, 18.859253... [12.0905895531178, 48.5518079996109, 18.859253...
In [12]:
# Automatic word wrap:
# the value string is limited by the number of characters in it,
# and description will be split into multiple lines.

(ggplot()
 + geom_livemap(location=[15.78,49.35], zoom=6)
 + geom_polygon(aes(fill='country'), 
                data=country_gcoder.get_boundaries(), 
                alpha=.2)
 + geom_point(aes('longitude', 'latitude', color='name'),
                data=czech_cities,
                size=4, 
                tooltips=layer_tooltips()
                    .title('@name')
                    .line('Longitude|@longitude')
                    .line('Latitude|@latitude')
                    .line('Description|@text'))
 + theme(legend_position='none')
)
Out[12]: