{ "cells": [ { "cell_type": "markdown", "id": "1401e426-caf4-43eb-b971-84c1fd723315", "metadata": {}, "source": [ "# Color Scales Using Matplotlib's Colormap \n", "\n", "Use `scale_color_cmapmpl()`, `scale_fill_cmapmpl()`, or `scale_cmapmpl()` to extract colors from a colormap and apply them to your chart.\n", "\n", "If a colormap is registered with Matplotlib, you can access it by its name. Otherwise, you can pass a cmap object directly.\n", "\n", "**Note:** These functions require Matplotlib to be installed in your environment." ] }, { "cell_type": "code", "execution_count": 1, "id": "f44cb9f5-5516-479d-affa-256e980ba092", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "8fdbc059-3ee7-4636-8687-499eacfff2d9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "6f7532e8-1d2f-4bbb-86c9-c951283f4d94", "metadata": {}, "outputs": [], "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", "\n", "p = ggplot({'x': x, 'y': y}) \\\n", " + geom_point(aes(x='x', y='y', fill='y'), shape=21, size=7, color='white')\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "42a30ed5-b719-4c1c-a64f-7caeb65c13be", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_fill_cmapmpl('magma')" ] }, { "cell_type": "code", "execution_count": 5, "id": "095ea20f-98ae-4fe9-b2e9-ace9d5ae1027", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_fill_cmapmpl('Dark2')" ] } ], "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.19" } }, "nbformat": 4, "nbformat_minor": 5 }