{ "cells": [ { "cell_type": "markdown", "id": "4cfeecab-7629-46e4-802a-56175c46b925", "metadata": {}, "source": [ "## Altair demos\n", "\n", "https://altair-viz.github.io/gallery/simple_bar_chart.html" ] }, { "cell_type": "code", "execution_count": null, "id": "e5e3fbb5-13ed-4c92-9ffa-6dd20325db70", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "import pandas as pd\n", "\n", "source = pd.DataFrame({\n", " 'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],\n", " 'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]\n", "})\n", "\n", "alt.Chart(source).mark_bar().encode(\n", " x='a',\n", " y='b'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "863cc7dc-e2d8-4c6f-9e37-b4e39d850a50", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "# Compute x^2 + y^2 across a 2D grid\n", "x, y = np.meshgrid(range(-5, 5), range(-5, 5))\n", "z = x ** 2 + y ** 2\n", "\n", "# Convert this grid to columnar data expected by Altair\n", "source = pd.DataFrame({'x': x.ravel(),\n", " 'y': y.ravel(),\n", " 'z': z.ravel()})\n", "\n", "alt.Chart(source).mark_rect().encode(\n", " x='x:O',\n", " y='y:O',\n", " color='z:Q'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "6569c08b-e6be-4667-aca1-1dbe8d795f20", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = data.cars()\n", "\n", "alt.Chart(source).mark_circle(size=60).encode(\n", " x='Horsepower',\n", " y='Miles_per_Gallon',\n", " color='Origin',\n", " tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']\n", ").interactive()" ] }, { "cell_type": "code", "execution_count": null, "id": "d12b5229-67b9-42c3-be9b-6dc49d204fac", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = data.wheat()\n", "\n", "base = alt.Chart(source).encode(x='year:O')\n", "\n", "bar = base.mark_bar().encode(y='wheat:Q')\n", "\n", "line = base.mark_line(color='red').encode(\n", " y='wages:Q'\n", ")\n", "\n", "(bar + line).properties(width=600)" ] }, { "cell_type": "code", "execution_count": null, "id": "f5ea297e-b2ba-4ac0-b9ae-7602815f523c", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = data.us_employment()\n", "\n", "alt.Chart(source).mark_bar().encode(\n", " x=\"month:T\",\n", " y=\"nonfarm_change:Q\",\n", " color=alt.condition(\n", " alt.datum.nonfarm_change > 0,\n", " alt.value(\"steelblue\"), # The positive color\n", " alt.value(\"orange\") # The negative color\n", " )\n", ").properties(width=600).interactive()" ] }, { "cell_type": "code", "execution_count": null, "id": "54227feb-e772-41d2-a4b2-eb00f80ef5ae", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = data.cars()\n", "\n", "alt.Chart(source).mark_circle().encode(\n", " alt.X(alt.repeat(\"column\"), type='quantitative'),\n", " alt.Y(alt.repeat(\"row\"), type='quantitative'),\n", " color='Origin:N'\n", ").properties(\n", " width=150,\n", " height=150\n", ").repeat(\n", " row=['Horsepower', 'Acceleration', 'Miles_per_Gallon'],\n", " column=['Miles_per_Gallon', 'Acceleration', 'Horsepower']\n", ").interactive()" ] }, { "cell_type": "code", "execution_count": null, "id": "1d006443-d2c6-4ef3-8252-62fed52136c8", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "# Data generators for the background\n", "sphere = alt.sphere()\n", "graticule = alt.graticule()\n", "\n", "# Source of land data\n", "source = alt.topo_feature(data.world_110m.url, 'countries')\n", "\n", "# Layering and configuring the components\n", "alt.layer(\n", " alt.Chart(sphere).mark_geoshape(fill='lightblue'),\n", " alt.Chart(graticule).mark_geoshape(stroke='white', strokeWidth=0.5),\n", " alt.Chart(source).mark_geoshape(fill='ForestGreen', stroke='black')\n", ").project(\n", " 'naturalEarth1'\n", ").properties(width=600, height=400).configure_view(stroke=None)" ] }, { "cell_type": "code", "execution_count": null, "id": "1ffbf13f-200f-43af-bb6d-1708cda1789e", "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = alt.topo_feature(data.world_110m.url, 'countries')\n", "\n", "base = alt.Chart(source).mark_geoshape(\n", " fill='#666666',\n", " stroke='white'\n", ").properties(\n", " width=300,\n", " height=180\n", ")\n", "\n", "projections = ['equirectangular', 'mercator', 'orthographic', 'gnomonic']\n", "charts = [base.project(proj).properties(title=proj)\n", " for proj in projections]\n", "\n", "alt.concat(*charts, columns=2)" ] }, { "cell_type": "code", "execution_count": null, "id": "f516c96a-f01e-4747-81a4-c902d0fd58c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c1fb2c97-98c5-4f6b-ac7b-193cdc541f00", "metadata": {}, "source": [ "## PyDeck\n", "\n", "https://pydeck.gl/gallery/globe_view.html" ] }, { "cell_type": "code", "execution_count": null, "id": "1c07df53-d8ab-49e1-a463-a663aefb8266", "metadata": {}, "outputs": [], "source": [ "import pydeck\n", "\n", "# 2014 location of car accidents in the UK\n", "UK_ACCIDENTS_DATA = ('https://raw.githubusercontent.com/uber-common/'\n", " 'deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv')\n", "\n", "# Define a layer to display on a map\n", "layer = pydeck.Layer(\n", " 'HexagonLayer',\n", " UK_ACCIDENTS_DATA,\n", " get_position=['lng', 'lat'],\n", " auto_highlight=True,\n", " elevation_scale=50,\n", " pickable=True,\n", " elevation_range=[0, 3000],\n", " extruded=True,\n", " coverage=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "9eb177a2-35f5-4d27-bfcb-b777e9864e0a", "metadata": {}, "outputs": [], "source": [ "import pydeck as pdk\n", "\n", "UK_ACCIDENTS_DATA = 'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv'\n", "\n", "layer = pdk.Layer(\n", " 'HexagonLayer', # `type` positional argument is here\n", " UK_ACCIDENTS_DATA,\n", " get_position=['lng', 'lat'],\n", " auto_highlight=True,\n", " elevation_scale=50,\n", " pickable=True,\n", " elevation_range=[0, 3000],\n", " extruded=True,\n", " coverage=1)\n", "\n", "# Set the viewport location\n", "view_state = pdk.ViewState(\n", " longitude=-1.415,\n", " latitude=52.2323,\n", " zoom=6,\n", " min_zoom=5,\n", " max_zoom=15,\n", " pitch=40.5,\n", " bearing=-27.36)\n", "\n", "# Combined all of it and render a viewport\n", "r = pdk.Deck(layers=[layer], initial_view_state=view_state)" ] }, { "cell_type": "code", "execution_count": null, "id": "8e18a7a7-5e6f-4f94-b4c7-a82184ad9c9e", "metadata": {}, "outputs": [], "source": [ "r" ] }, { "cell_type": "code", "execution_count": null, "id": "58d7ea41-0784-4427-a587-130d5b04d649", "metadata": {}, "outputs": [], "source": [ "layer = pdk.Layer(\n", " 'ScatterplotLayer', # Change the `type` positional argument here\n", " UK_ACCIDENTS_DATA,\n", " get_position=['lng', 'lat'],\n", " auto_highlight=True,\n", " get_radius=1000, # Radius is given in meters\n", " get_fill_color=[180, 0, 200, 140], # Set an RGBA value for fill\n", " pickable=True)\n", "\n", "# Set the viewport location\n", "view_state = pdk.ViewState(\n", " longitude=-1.415,\n", " latitude=52.2323,\n", " zoom=6,\n", " min_zoom=5,\n", " max_zoom=15,\n", " pitch=40.5,\n", " bearing=-27.36)\n", "\n", "# Combined all of it and render a viewport\n", "r = pdk.Deck(layers=[layer], initial_view_state=view_state)" ] }, { "cell_type": "code", "execution_count": null, "id": "06d2ef47-cb72-4027-a0c1-78eb65def1cf", "metadata": {}, "outputs": [], "source": [ "r" ] }, { "cell_type": "code", "execution_count": null, "id": "4a0f6ef3-a7a6-4617-a70e-0d6ceac4b900", "metadata": {}, "outputs": [], "source": [ "import pydeck as pdk\n", "import pandas as pd\n", "import json\n", "\n", "# H3_HEX_DATA = \"https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/sf.h3cells.json\"\n", "\n", "H3_HEX_DATA = \"https://dggs-api-bozea3cspa-ew.a.run.app/dggs-api/collections/srtm_30m_estonia_h3/zones?resolution=6&limit=10000&bbox=0.7829255156250007,54.890482222074525,49.364468484375,62.14597996607117\"\n", "\n", "df = pd.read_json(H3_HEX_DATA)\n", "\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "26c38cbd-d99b-412a-8b24-402f78a0717b", "metadata": {}, "outputs": [], "source": [ "df['hex'] = df['features'].apply(lambda d: d['geometry'][0])" ] }, { "cell_type": "code", "execution_count": null, "id": "f077c9bd-1d12-4ef9-bbba-9f2afc5d4179", "metadata": {}, "outputs": [], "source": [ "df['elevation'] = df['features'].apply(lambda d: d['properties']['elevation'])\n", "df['elevation'] = pd.to_numeric(df['elevation'], downcast=\"integer\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5bbd683b-a759-4624-ac1f-5415c37f476d", "metadata": {}, "outputs": [], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "61181c47-1d82-4cfc-8236-76453b3bac1a", "metadata": {}, "outputs": [], "source": [ "# Define a layer to display on a map\n", "layer = pdk.Layer(\n", " \"H3HexagonLayer\",\n", " df,\n", " pickable=True,\n", " stroked=True,\n", " filled=True,\n", " extruded=False,\n", " get_hexagon=\"hex\",\n", " get_fill_color=\"[255 - elevation, 255, elevation]\",\n", " get_line_color=[255, 255, 255],\n", " line_width_min_pixels=2,\n", ")\n", "\n", "# Set the viewport location\n", "view_state = pdk.ViewState(latitude=58.7749295, longitude=26.4194155, zoom=11, bearing=0, pitch=30)\n", "\n", "\n", "# Render\n", "r = pdk.Deck(layers=[layer], initial_view_state=view_state, tooltip={\"text\": \"Count: {count}\"})" ] }, { "cell_type": "code", "execution_count": null, "id": "601057b5-7b88-4764-b0be-f0696f5709f8", "metadata": {}, "outputs": [], "source": [ "r" ] }, { "cell_type": "code", "execution_count": null, "id": "66374a9c-ee00-4e3a-8798-89ebdfb81700", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "GlobeView\n", "=========\n", "\n", "Over 33,000 power plants of the world plotted by their production capacity (given by height)\n", "and fuel type (green if renewable) on an experimental deck.gl GlobeView.\n", "\"\"\"\n", "import pydeck as pdk\n", "import pandas as pd\n", "\n", "COUNTRIES = \"https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_0_scale_rank.geojson\"\n", "POWER_PLANTS = \"https://raw.githubusercontent.com/ajduberstein/geo_datasets/master/global_power_plant_database.csv\"\n", "\n", "df = pd.read_csv(POWER_PLANTS)\n", "\n", "\n", "def is_green(fuel_type):\n", " \"\"\"Return a green RGB value if a facility uses a renewable fuel type\"\"\"\n", " if fuel_type.lower() in (\"nuclear\", \"water\", \"wind\", \"hydro\", \"biomass\", \"solar\", \"geothermal\"):\n", " return [10, 230, 120]\n", " return [230, 158, 10]\n", "\n", "\n", "df[\"color\"] = df[\"primary_fuel\"].apply(is_green)\n", "\n", "view_state = pdk.ViewState(latitude=51.47, longitude=0.45, zoom=2, min_zoom=2)\n", "\n", "# Set height and width variables\n", "view = pdk.View(type=\"_GlobeView\", controller=True, width=1000, height=700)\n", "\n", "\n", "layers = [\n", " pdk.Layer(\n", " \"GeoJsonLayer\",\n", " id=\"base-map\",\n", " data=COUNTRIES,\n", " stroked=False,\n", " filled=True,\n", " get_fill_color=[200, 200, 200],\n", " ),\n", " pdk.Layer(\n", " \"ColumnLayer\",\n", " id=\"power-plant\",\n", " data=df,\n", " get_elevation=\"capacity_mw\",\n", " get_position=[\"longitude\", \"latitude\"],\n", " elevation_scale=100,\n", " pickable=True,\n", " auto_highlight=True,\n", " radius=20000,\n", " get_fill_color=\"color\",\n", " ),\n", "]\n", "\n", "deck = pdk.Deck(\n", " views=[view],\n", " initial_view_state=view_state,\n", " tooltip={\"text\": \"{name}, {primary_fuel} plant, {country}\"},\n", " layers=layers,\n", " # Note that this must be set for the globe to be opaque\n", " parameters={\"cull\": True},\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9e08e10c-c290-4ae2-8ea5-7390986236b6", "metadata": {}, "outputs": [], "source": [ "deck" ] }, { "cell_type": "code", "execution_count": null, "id": "acfbb585-8e13-4a01-a39a-b663cb9120e8", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "geopy2021a", "language": "python", "name": "geopy2021a" }, "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }