{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default Presentation" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg, aes(x=\"displ\", y=\"hwy\")) + geom_point(aes(color=\"cty\", shape=\"drv\"), size=5)\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiline Legend" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scale_labels = scale_color_continuous(\n", " name=\"City mileage\",\n", " breaks=[30,20,10],\n", " labels=[\"30 (mpg)\",\"2\\n0\\n(mpg)\",\"10\\n(mpg)\"]\n", " ) + \\\n", " scale_shape(\n", " name=\"Drive type\",\n", " limits=[\"f\", \"r\", \"4\"],\n", " labels=[\"Front-wheel\\ndrive\", \"Rear \\n wheel \\n drive\", \"4 wheel drive\"])\n", "p + scale_labels " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Change Legend Direction" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_labels + theme(legend_position=\"bottom\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### No Overlapping" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_color_continuous(\n", " breaks=[35,30,25,20,15,10],\n", " labels=[\"35\\n(mpg)\",\"30\\n(mpg)\",\"25\\n(mpg)\",\"20\\n(mpg)\",\"15\\n(mpg)\",\"10\\n(mpg)\"])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_color_continuous(\n", " breaks=[35,30,25,20,15,10],\n", " labels=[\"35\\n(mpg)\",\"30\\n(mpg)\",\"25\\n(mpg)\",\"20\\n(mpg)\",\"15\\n(mpg)\",\"10\\n(mpg)\"]\n", ") + theme(legend_position=\"bottom\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from scipy.stats import multivariate_normal\n", "\n", "delta = 0.5\n", "center_x = - 100\n", "center_y = 35\n", "x = np.arange(-5.0, 5.0, delta)\n", "y = np.arange(-5.0, 5.0, delta)\n", "X, Y = np.meshgrid(x, y)\n", "\n", "mu = np.array([1, 0])\n", "sigma = np.diag([1, 4])\n", "mu1 = np.array([0, 0])\n", "sigma1 = np.diag([4, 1])\n", "\n", "Z = multivariate_normal.pdf(np.dstack((X, Y)), mean=mu, cov=sigma)\n", "Z = Z - multivariate_normal.pdf(np.dstack((X, Y)), mean=mu1, cov=sigma1)\n", "\n", "x = X.reshape(-1) + center_x\n", "y = Y.reshape(-1) + center_y\n", "z = Z.reshape(-1)\n", "dat = dict(x=x, y=y, z=z)\n", "\n", "ggplot(dat, aes('x', 'y')) + geom_tile(aes(fill='z'), width=.5, height=.5) +\\\n", " geom_contour(aes(z='z'), alpha=0.5) +\\\n", " scale_fill_gradient('blue', 'red') + \\\n", " theme(legend_position='top')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }