{ "cells": [ { "cell_type": "markdown", "id": "c284f51c-72ac-4e19-981d-43fc79570b16", "metadata": {}, "source": [ "# `geom_spoke()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "45bd1cf4-dcec-460e-8a92-8e7cb9b15ea6", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "6a9633c3-f3fa-43b6-bd85-5c55169c223b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "3dcaba37-31b4-49c8-b2c4-7b9f9d57e453", "metadata": {}, "outputs": [], "source": [ "def get_data(n, a, b, f):\n", " d = (b - a) / (n - 1)\n", " xrange = np.linspace(a, b, n)\n", " yrange = np.linspace(a, b, n)\n", " X, Y = np.meshgrid(xrange, yrange)\n", " x, y = X.reshape(-1), Y.reshape(-1)\n", " Z = f(X, Y)\n", " z = Z.reshape(-1)\n", " dY, dX = np.gradient(Z, d)\n", " R = np.sqrt(dX**2 + dY**2)\n", " normalized_R = R / R.max() * d\n", " A = np.arctan2(dY, dX)\n", " radius, angle = normalized_R.reshape(-1), A.reshape(-1)\n", " return pd.DataFrame({'x': x, 'y': y, 'z': z, 'radius': radius, 'angle': angle})" ] }, { "cell_type": "code", "execution_count": 4, "id": "37cdc646-d4cc-44ce-bff2-71306299c3e5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(441, 5)\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", "
xyzradiusangle
0-6.283185-6.2831851.0000000.478721-0.314159
1-5.654867-6.2831851.5877850.396934-0.381905
2-5.026548-6.2831851.9510570.204153-0.810479
3-4.398230-6.2831851.9510570.204153-2.331114
4-3.769911-6.2831851.5877850.396934-2.759688
\n", "
" ], "text/plain": [ " x y z radius angle\n", "0 -6.283185 -6.283185 1.000000 0.478721 -0.314159\n", "1 -5.654867 -6.283185 1.587785 0.396934 -0.381905\n", "2 -5.026548 -6.283185 1.951057 0.204153 -0.810479\n", "3 -4.398230 -6.283185 1.951057 0.204153 -2.331114\n", "4 -3.769911 -6.283185 1.587785 0.396934 -2.759688" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = get_data(n=21, a=-2*np.pi, b=2*np.pi, f=lambda xarray, yarray: np.sin(xarray) + np.cos(yarray))\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "52db3359-3841-4bc4-9a63-56126df011a6", "metadata": {}, "outputs": [], "source": [ "p = ggplot(df, aes('x', 'y')) + coord_fixed() + theme_void() + scale_viridis(['color', 'fill'])" ] }, { "cell_type": "code", "execution_count": 6, "id": "b4ede698-83b4-42df-8446-7cf309ebb0f3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bin2d(aes(fill='z'), stat='identity')" ] }, { "cell_type": "markdown", "id": "6f14a6ee-1173-4ee1-a0e6-e1212bed151f", "metadata": {}, "source": [ "#### 1. Use `geom_spoke()` to Indicate the Direction and Distance (or Speed)" ] }, { "cell_type": "code", "execution_count": 7, "id": "32043345-6a07-4699-b29b-bbc685745a6d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_point(size=1.5) + geom_spoke(aes(angle='angle', radius='radius', color='z'))" ] }, { "cell_type": "markdown", "id": "3232e0ce-b989-4374-a749-3dceed9fd9af", "metadata": {}, "source": [ "#### 2. Parameter `pivot`" ] }, { "cell_type": "code", "execution_count": 8, "id": "799e4da4-222f-471c-891e-8c11f2a37d5b", "metadata": {}, "outputs": [], "source": [ "def get_plot(pivot):\n", " n = 4\n", " a, b = -2, 2\n", " r = .75\n", " pivot_df = get_data(n=n, a=a, b=b, f=lambda xarray, yarray: xarray**2 + yarray**2)\n", " title = \"pivot={0}{1}\".format(pivot, \" (default)\" if pivot == 'tail' else \"\")\n", " return ggplot(pivot_df, aes('x', 'y')) + \\\n", " geom_spoke(aes(angle='angle'), radius=r, pivot=pivot) + \\\n", " geom_point() + \\\n", " coord_fixed() + \\\n", " xlim(a - r, b + r) + ylim(a - r, b + r) + \\\n", " ggtitle(title) + \\\n", " theme_void() + theme(plot_title=element_text(hjust=0.5))" ] }, { "cell_type": "code", "execution_count": 9, "id": "386e5ccf-1cf9-4297-aa6a-1c382198e70e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([get_plot('tail'), get_plot('mid'), get_plot('tip')], ncol=3)" ] } ], "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.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }