{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "states = pd.read_csv(\"../data/USA_States.csv\")\n", "states.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "LetsPlot.set(maptiles_zxy(url='https://a.tile.openstreetmap.org/{z}/{x}/{y}.png'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ggplot(data=states) + geom_livemap(aes(\"Longitude\", \"Latitude\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dept = pd.read_csv(\"../data/usgs_dept_by_state_2019.csv\")\n", "\n", "dept.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dept_gg = dept \\\n", " .drop([\" \", \"Unnamed: 2\", \"Unnamed: 4\", \"State and Local Debt\", \n", " \"Unnamed: 6\", \"Unnamed: 8\", \"Real State Growth %\", \"Unnamed: 10\", \"Population (million)\"], axis=1) \\\n", " .set_index(\"State\") \\\n", " .stack() \\\n", " .reset_index() \\\n", " .rename(columns={\"level_1\": \"Item\", 0: \"$B\"})\n", "\n", "\n", "dept_gg\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ggplot(dept_gg) \\\n", "+ geom_bar(aes(\"State\", \"$B\", fill=\"Item\"), stat=\"identity\", position=\"dodge\", sampling=\"none\") \\\n", "+ scale_fill_manual(values=[\"#FE0968\", \"#FF75A6\", \"#007BCD\"]) \\\n", "+ ggsize(1000, 500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "states_gdf = gpd.GeoDataFrame(states, geometry=gpd.points_from_xy(states.Longitude, states.Latitude))\n", "states_gdf.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ggplot(dept_gg) + geom_livemap(aes(sym_x=\"Item\", sym_y=\"$B\", fill=\"Item\"), symbol='bar', size=30, map=states_gdf, map_join=[\"State\", \"State\"])\n", "\n" ] } ], "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": 2 }