{ "cells": [ { "cell_type": "markdown", "id": "c4c21432", "metadata": {}, "source": [ "## Support for marker rotation" ] }, { "cell_type": "code", "execution_count": 1, "id": "96f6596f", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "09704322", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "7ccad20f", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'x': [1, 2, 3, 4],\n", " 'y': [1, 1, 1, 1],\n", " 'angle': [-30, 0, 30, 60]\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "4bddfeb1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) +\\\n", "geom_point(aes(x='x', y='y', angle='angle'), size=20, shape=3.0) +\\\n", "geom_point(x=5, y=1.0, angle=90, size=20, shape=2.0, color='red')" ] }, { "cell_type": "markdown", "id": "9b166f8f", "metadata": {}, "source": [ "### Circles must not rotate in target SVG" ] }, { "cell_type": "code", "execution_count": 5, "id": "d4da95ee", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p = ggplot(data) +\\\n", "geom_point(aes(x='x', y='y', angle='angle'), size=20, shape='SOLID_CIRCLE_2') +\\\n", "geom_point(x=5, y=1.0, angle=90, size=20, shape=2.0, color='red')\n", "#p.to_svg('1.svg')\n", "p.show()" ] }, { "cell_type": "code", "execution_count": 6, "id": "0abb7272", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
statefound namegeometry
0CACaliforniaMULTIPOLYGON (((-124.32694 40.61620, -124.3118...
\n", "
" ], "text/plain": [ " state found name geometry\n", "0 CA California MULTIPOLYGON (((-124.32694 40.61620, -124.3118..." ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "from sklearn.datasets import fetch_california_housing\n", "\n", "california_housing_bunch = fetch_california_housing()\n", "data = pd.DataFrame(california_housing_bunch.data, columns=california_housing_bunch.feature_names)\n", "\n", "# Add $-value field to the dataframe.\n", "# dataset.target: numpy array of shape (20640,)\n", "# Each value corresponds to the average house value in units of 100,000.\n", "data['Value($)'] = california_housing_bunch.target * 100000\n", "data['angle'] = (data['HouseAge'] - 25)/ 50 * 60\n", "data.head()\n", "\n", "data = data.sample(n=50)\n", "\n", "from lets_plot.geo_data import *\n", "\n", "CA = geocode_states('CA').scope('US').inc_res(2).get_boundaries()\n", "CA.head()" ] }, { "cell_type": "code", "execution_count": 7, "id": "c71ad3aa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The plot base \n", "p = ggplot()\\\n", " + theme(axis='blank', panel_grid='blank')\\\n", " + scale_fill_brewer(palette=\"PiYG\", direction=-1)\\\n", " + scale_size(range=[5, 15])\\\n", " \n", "\n", "# The points layer\n", "points = geom_point(aes(x='Longitude',\n", " y='Latitude',\n", " angle='angle',\n", " size='Value($)', \n", " color='HouseAge'), \n", " data=data,\n", " shape = 24,\n", " alpha=0.8)\n", "\n", "# The map\n", "p + geom_polygon(data=CA, fill='#F8F4F0', color='#B71234')\\\n", " + points\\\n", " + ggsize(650, 600)" ] }, { "cell_type": "code", "execution_count": 8, "id": "a0ed086a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 10\n", "np.random.seed(42)\n", "x = np.random.normal(0, 1, n)\n", "y = np.random.normal(1, 2, n)\n", "angle = 180 * x ** 2 + np.random.normal(n)\n", "\n", "ggplot({'x': x, 'y': y, 'angle': angle}) + geom_qq2(aes(x='x', y='y', angle='angle'), shape=24)" ] }, { "cell_type": "code", "execution_count": 9, "id": "da2ab63f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([139.36026591, 192.15507684, -76.45467352, 82.01110959,\n", " 199.96606615, -27.17884394, 247.6256433 , 71.88503581,\n", " 127.49512504, 71.6928098 ])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.normal(180, 180, n)" ] }, { "cell_type": "code", "execution_count": 10, "id": "11279926", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(42)\n", "n = 100\n", "x = np.random.uniform(-1, 1, size=n)\n", "y = 25 * x ** 2 + np.random.normal(size=n)\n", "angle = 180 * y / 25\n", "\n", "ggplot({'x': x, 'y': y, 'angle': angle}) + \\\n", " geom_point(aes(x='x', y='y', fill='y', angle='angle'), \\\n", " shape=22, size=5, color='white')" ] } ], "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.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }