{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from lets_plot import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"LetsPlot.setup_html()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Draw GeoDataFrame on plot"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from geopandas import GeoDataFrame\n",
"from shapely.geometry import MultiPolygon, Polygon, LinearRing, Point, mapping"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"POINT = Point(-5, 17)\n",
"\n",
"POLYGON = Polygon(\n",
" LinearRing([(1, 1), (1, 9), (9, 9), (9, 1)]),\n",
" [LinearRing([(2, 2), (3, 2), (3, 3), (2, 3)]),\n",
" LinearRing([(4, 4), (6, 4), (6, 6), (4, 6)])]\n",
" )\n",
"\n",
"MULTIPOLYGON = MultiPolygon([\n",
" Polygon(LinearRing([(11, 12), (13, 14), (15, 13)]))\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"gdf = GeoDataFrame(\n",
" data={'id': ['A', 'B', 'C'],\n",
" 'coord': [POINT, POLYGON, MULTIPOLYGON]},\n",
" geometry='coord')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "geom_polygon() takes from 0 to 1 positional arguments but 2 were given",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mC:\\Temp/ipykernel_9592/3737669046.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mggplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mgeom_polygon\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfill\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'id'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgdf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: geom_polygon() takes from 0 to 1 positional arguments but 2 were given"
]
}
],
"source": [
"ggplot() + geom_polygon(aes(fill = 'id'), gdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mapping doesn't work for DataFrame in map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_polygon(aes(fill = 'id'), map = gdf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_polygon(map = gdf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(\n",
" {'name': ['A', 'B', 'C'],\n",
" 'value': [42, 23, 87]})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_polygon(aes(fill = 'value'), df, map = gdf, map_join=('name', 'id'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Geom kinds that support GeoDataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_polygon(data = gdf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_point(aes(color='id'), gdf, size=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_rect(aes(fill='id'), gdf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_path(data = gdf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_map(map = gdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Problems"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_map(data = gdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Error message contains autogenerated column \"\\_\\_key\\_\\_\". It's ok."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_polygon(aes(fill = 'id'), gdf)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}