{ "cells": [ { "cell_type": "markdown", "id": "b7de4c22-d9d7-426e-9d27-bafa5ba12741", "metadata": {}, "source": [ "# HCL color model.\n", "\n", "Lets-Plot now uses the `HCL` color model instead of `HLV`. This change affects hue scale and gradient scales (both color and greyscale).\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "437cbe49-4f7b-413a-82ed-9c5db230bf24", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "fd1026f5-35e9-4465-a723-1861118f838e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d05a6fe6-b4bb-48f0-aa19-ca222a135a2b", "metadata": {}, "outputs": [], "source": [ "data = { 'x': ['A', 'B', 'C', 'D', 'E', 'F'] }\n", "p = ggplot(data) \\\n", " + geom_boxplot(aes(x='x', fill='x'), stat='identity', lower=25, middle=50, upper=75, ymin=0, ymax=100)" ] }, { "cell_type": "markdown", "id": "cf5b6cc0-76e3-42e6-973a-86cea6e76aa2", "metadata": {}, "source": [ "Palette for discrete data:" ] }, { "cell_type": "code", "execution_count": 4, "id": "da72d537-6d52-4a2c-94a0-0679e3a54b44", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_fill_hue()" ] }, { "cell_type": "markdown", "id": "b34f1a5c-7a71-405e-91a4-fd9543ce8801", "metadata": {}, "source": [ "Hue gradient:" ] }, { "cell_type": "code", "execution_count": 5, "id": "dcdd08ed-d31e-4b45-a365-42be3aed321e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'x': list(range(256))\n", "}\n", "\n", "ggplot(data) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_hue() \\\n", " + scale_color_hue() \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 200)" ] }, { "cell_type": "markdown", "id": "cd5dbc51-42fe-484e-b9e8-11607ce4d68f", "metadata": {}, "source": [ "Gradient:" ] }, { "cell_type": "code", "execution_count": 6, "id": "b1cb4d66-9f3a-4a0b-91b1-e680cb7f9acf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { \n", " 'x': list(range(256)) \n", "}\n", "\n", "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_gradient(low=\"#00FF00\", high=\"#FF0000\") \\\n", " + scale_color_gradient(low=\"#00FF00\", high=\"#FF0000\") \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 200)\n" ] }, { "cell_type": "markdown", "id": "39b53593-111a-4c12-9d42-1f1c08f9327a", "metadata": {}, "source": [ "Gradientn" ] }, { "cell_type": "code", "execution_count": 7, "id": "88713594-531a-4c87-ae4c-a1182bf94182", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { \n", " 'x': list(range(256)) \n", "}\n", "\n", "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_gradientn(colors=[\"#00FF00\", \"#FF0000\", \"#0000FF\"]) \\\n", " + scale_color_gradientn(colors=[\"#00FF00\", \"#FF0000\", \"#0000FF\"]) \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 200)" ] }, { "cell_type": "markdown", "id": "62174e32-01e6-4142-87d6-7da5b4b47a0a", "metadata": {}, "source": [ "Greyscale gradient:" ] }, { "cell_type": "code", "execution_count": 8, "id": "03f1a44c-b903-4268-98e6-ce33a623c584", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { \n", " 'x': list(range(256)) \n", "}\n", "\n", "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_color_grey(start=0.1, end=0.9) \\\n", " + scale_fill_grey(start=0.1, end=0.9) \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 200)" ] }, { "cell_type": "markdown", "id": "50184eff-508c-4938-8465-95aff147aafc", "metadata": {}, "source": [ "Viridis" ] }, { "cell_type": "code", "execution_count": 9, "id": "7cdf1c36-a3f5-4d9b-95f1-4b385bfe0aa5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { \n", " 'x': list(range(256)) \n", "}\n", "\n", "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_viridis() \\\n", " + scale_color_viridis() \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 200)" ] }, { "cell_type": "markdown", "id": "17f0548a-ae3d-4e41-a811-449367c245f0", "metadata": {}, "source": [ "Parameter `h_start` now works with descrete data:" ] }, { "cell_type": "code", "execution_count": 10, "id": "37e65f9b-7c07-450e-82f2-354ee43ffc77", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_fill_hue(h_start=180)" ] }, { "cell_type": "markdown", "id": "8ef1e8c8-460f-420c-9b43-6673d3e4964c", "metadata": {}, "source": [ "Parameter `l` now correctly controls lightness:" ] }, { "cell_type": "code", "execution_count": 11, "id": "ace5f30e-dd7f-4bdc-b962-45867921d751", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_fill_hue(c=95, l=75) + ggtitle('scale_fill_hue(c=95, l=75)')" ] }, { "cell_type": "markdown", "id": "296617b4-8403-4e7b-8678-3b1ade222ec5", "metadata": {}, "source": [ "# SANDBOX" ] }, { "cell_type": "code", "execution_count": 12, "id": "2c86ceb4-4744-43db-997d-3cb6e0d1e0a7", "metadata": {}, "outputs": [], "source": [ "def dump_spec(plot, display=None):\n", " import json\n", "\n", " try:\n", " import clipboard\n", " except:\n", " clipboard = None\n", " \n", " from lets_plot._type_utils import standardize_dict\n", " \n", " plot_dict = standardize_dict(plot.as_dict())\n", " plot_json = json.dumps(plot_dict, indent=2)\n", " \n", " if clipboard:\n", " clipboard.copy('')\n", " clipboard.copy(str(plot_json))\n", " else:\n", " if display is None:\n", " display = True\n", " \n", " if display:\n", " print(plot_json)\n", "\n", " return plot" ] }, { "cell_type": "code", "execution_count": 13, "id": "71769866-ca0a-4e2d-a6d7-dd32168c53bd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'x': list(range(60))\n", "}\n", "\n", "dump_spec(ggplot(data) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_hue() \\\n", " + scale_color_hue() \\\n", " + coord_cartesian() \\\n", " + ggsize(600, 200))" ] }, { "cell_type": "code", "execution_count": 14, "id": "fc3e3839-151d-434d-a6ca-fe651db75d85", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dump_spec(ggplot(data) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_grey() \\\n", " + scale_color_grey() \\\n", " + coord_cartesian() \\\n", " + ggsize(600, 200))" ] }, { "cell_type": "code", "execution_count": 15, "id": "10a82931-757f-4fc4-a0a9-8d6c4fe48866", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { 'x': list(range(59)) }\n", "dump_spec(\n", " ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1, height=40) \\\n", " + scale_fill_gradient(low=\"#00FF00\", high=\"#FF0000\") \\\n", " + scale_color_gradient(low=\"#00FF00\", high=\"#FF0000\")\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "id": "cc27b75d-9c0d-43fe-b726-22ffe748a73f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) + geom_tile(aes(x='x', fill='x'), height=40) + scale_fill_gradient(low = \"yellow\", high = \"red\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "412cfae1-ead8-4ad2-b3db-4aa441182386", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dump_spec(p + scale_fill_hue(h = [15, 375], c = 100, l = 100))" ] }, { "cell_type": "code", "execution_count": 18, "id": "9020db19-8be9-4c7d-8b2e-0eceef111bc2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = { \n", " 'x': list(range(256)) \n", "}\n", "\n", "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_viridis() \\\n", " + scale_color_viridis() \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 400)" ] }, { "cell_type": "code", "execution_count": 19, "id": "1c0a2fe3-65da-4a14-b720-8f3ea900b30f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) \\\n", " + geom_tile(aes(x='x', fill='x', color='x'), size=1) \\\n", " + scale_fill_viridis() \\\n", " + scale_color_viridis(option='A') \\\n", " + coord_cartesian() \\\n", " + ggsize(800, 400)" ] }, { "cell_type": "code", "execution_count": null, "id": "945a81f1-1a8c-418e-9a3c-f36ee84c7a40", "metadata": {}, "outputs": [], "source": [] } ], "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.15" } }, "nbformat": 4, "nbformat_minor": 5 }