{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from datetime import datetime\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "economics_url = 'https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv'\n", "economics = pd.read_csv(economics_url)\n", "economics['date'] = pd.to_datetime(economics['date'])\n", "start = datetime(2000, 1, 1)\n", "economics = economics.loc[economics['date'] >= start]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p_dt = ggplot(economics, aes('date', 'unemploy')) + geom_line()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Default\n", "p_dt + scale_x_datetime()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define format of the axis labels\n", "p_dt + scale_x_datetime(format=\"%b %Y\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ggplot(economics, aes('date', 'date')) + geom_line()\\\n", "+ scale_x_datetime(format=\"%B\")+ scale_y_datetime(format=\"%Y\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bar_dict = {\n", " 'time': ['Lunch', 'Dinner', 'Night'],\n", " 'total_bill': [14.89, 17.23, 40]\n", "}\n", "ggplot(bar_dict, aes('time', 'total_bill', fill='time')) + geom_bar(stat='identity') + scale_x_discrete(format='time is {}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N = 21\n", "x = [v for v in range(N)]\n", "y0 = [pow(10, v / 10.) for v in range(N)]\n", "y1 = [v * 5 for v in range(N)]\n", "formula = ['10^(x/10)'] * N + ['5*x'] * N\n", "data = dict(x=x * 2, y=y0 + y1, formula=formula)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = ggplot(data) + geom_point(aes('x', 'y', color='formula', size='formula')) + scale_size_manual(values=[7, 3])\n", "p" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p + scale_x_continuous(format='x={.1f}') + scale_y_continuous(format='y={.1f}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Skip 'format' if labels are specified \n", "\n", "breaks = [0, 10, 20]\n", "labels = ['0', '10', '20']\n", "\n", "p + scale_x_continuous(breaks=breaks, labels=labels, format='is {.2f}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p + scale_x_log10(format='x={.2f}') + scale_y_log10(format='y={.3f}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p + scale_x_reverse(format='.2f')" ] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }