{ "cells": [ { "cell_type": "markdown", "id": "e212c275-be6a-483c-82a1-d8fee24be4e3", "metadata": {}, "source": [ "# Hexagonal Heatmap" ] }, { "cell_type": "code", "execution_count": 1, "id": "4a581740-2008-4233-8d09-b746a21764f5", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "9c1e6ecc-f5d2-4839-8510-9c95b903b568", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a4c5fcff-e799-4757-88d3-a3205f2336fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(53940, 10)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
caratcutcolorclaritydepthtablepricexyz
00.23IdealESI261.555.03263.953.982.43
10.21PremiumESI159.861.03263.893.842.31
20.23GoodEVS156.965.03274.054.072.31
30.29PremiumIVS262.458.03344.204.232.63
40.31GoodJSI263.358.03354.344.352.75
\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z\n", "0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43\n", "1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31\n", "2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31\n", "3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63\n", "4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/diamonds.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "id": "94081b7a-fc42-4b87-a17d-26be2a303961", "metadata": {}, "source": [ "## Default View" ] }, { "cell_type": "code", "execution_count": 4, "id": "1da2225c-0123-492b-9fab-126dd7ae3997", "metadata": {}, "outputs": [], "source": [ "p = ggplot(df, aes(\"carat\", \"price\")) + scale_fill_viridis(trans='symlog')" ] }, { "cell_type": "code", "execution_count": 5, "id": "16e06f23-1d9a-458b-a52b-893c951d46e1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_hex()" ] }, { "cell_type": "markdown", "id": "faf451cb-cb67-4e6f-85d5-ce273947aa27", "metadata": {}, "source": [ "### Parameters" ] }, { "cell_type": "code", "execution_count": 6, "id": "79bb6723-7aad-427f-a580-32690649a5b7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_hex(bins=[5, 10]) + ggtitle(\"bins=[5, 10]\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "adb061f6-b1ec-4ae0-8fc8-9326fcca72b4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_hex(binwidth=[1, 5000]) + ggtitle(\"binwidth=[1, 5000]\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "2913abd7-8672-474d-a296-4fa99fc166cb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_hex(binwidth=[1, 5000], drop=False) + ggtitle(\"drop=False\")" ] }, { "cell_type": "markdown", "id": "3ce809ed-1a2f-4f7f-90d3-5fd580f5e13d", "metadata": {}, "source": [ "## `'binhex'` Stat" ] }, { "cell_type": "code", "execution_count": 9, "id": "aaaf82d4-3434-46d7-9646-14400de678c9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"carat\", \"price\")) + \\\n", " geom_point(aes(color='..count..'), stat='binhex',\n", " binwidth=[1, 5000], drop=False,\n", " size=.75, size_unit='x') + \\\n", " coord_cartesian(xlim=[-1, 7], ylim=[-2_500, 25_000])" ] }, { "cell_type": "markdown", "id": "5d5ba84c-c61e-4664-98cc-a2795b2a1905", "metadata": {}, "source": [ "## Hexagons on Map" ] }, { "cell_type": "code", "execution_count": 10, "id": "3b1337a6-e53d-4316-9f6a-dfda278cca7b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(27, 4)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CDInumber_of_responseslatlon
03.1142.3714.07
12.2142.2514.37
22.0142.2014.21
33.1142.2214.28
43.4442.4614.21
\n", "
" ], "text/plain": [ " CDI number_of_responses lat lon\n", "0 3.1 1 42.37 14.07\n", "1 2.2 1 42.25 14.37\n", "2 2.0 1 42.20 14.21\n", "3 3.1 1 42.22 14.28\n", "4 3.4 4 42.46 14.21" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/italy_cdi.csv\")\\\n", " [[\"CDI\", \"No. of responses\", \"Latitude\", \"Longitude\"]]\n", "df.columns = [\"CDI\", \"number_of_responses\", \"lat\", \"lon\"]\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 11, "id": "325d1bf7-c385-4bd8-b5dd-e1b6bbcc068c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) + \\\n", " geom_livemap(location=[15, 41], zoom=7) + \\\n", " geom_hex(aes(x=\"lon\", y=\"lat\", weight=\"number_of_responses\", fill=\"..count..\"),\n", " binwidth=[.5, .5], size=.5, color=\"#081d58\", alpha=.5) + \\\n", " scale_fill_brewer(name=\"Number of responses\", type='seq', palette=\"YlGnBu\") + \\\n", " ggtitle(\"Community Internet Intensity Map\",\n", " \"M 4.7 - 1 km NW of Montagano, Italy\") + \\\n", " theme(plot_title=element_text(face='bold'))" ] } ], "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.9.21" } }, "nbformat": 4, "nbformat_minor": 5 }