{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib Styles and Colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You need to first install matplotlib with\n", "\n", "`conda install matplotlib`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# numpy\n", "import numpy as np\n", "# matplotlib\n", "import matplotlib.pyplot as plt\n", "import matplotlib.cbook\n", "from matplotlib.colors import ListedColormap\n", "# To show the plots in the notebook\n", "%matplotlib inline\n", "# Cycler\n", "from cycler import cycler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Styles\n", "\n", "On style sheets in Matplotlib: \n", "- https://matplotlib.org/users/customizing.html\n", "- https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See the default cycle of colors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.rcParams['axes.prop_cycle']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(5)\n", "y = np.arange(5)\n", "fig, ax = plt.subplots(figsize=(5, 5))\n", "for i in range(6):\n", " ax.plot(x+i, y-i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change colors of the default cycle and add line styles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.rcParams['axes.prop_cycle'] = (cycler('color', ['r', 'g', 'b', 'y']) + cycler(linestyle=['-', '--', ':', '-.']))\n", "plt.rcParams['axes.prop_cycle']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(5, 5))\n", "for i in range(6):\n", " ax.plot(x+i, y-i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function that plots a number of colored circles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plot_colored_circles(num_circles=6):\n", " \"\"\"Plot circle patches. Adapted from \n", " https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html\n", " \"\"\"\n", " prng = np.random.RandomState(123)\n", " fig, ax = plt.subplots(figsize=(5, 5))\n", " for sty_dict, j in zip(plt.rcParams['axes.prop_cycle'], range(num_circles)):\n", " ax.add_patch(plt.Circle(prng.uniform(low=-5, high=5, size=2),\n", " radius=1.0, color=sty_dict['color']))\n", " ax.set_xlim([-6, 6])\n", " ax.set_ylim([-6, 6])\n", " ax.set_aspect('equal', adjustable='box') # to plot circles as circles\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_colored_circles()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List all styles in Matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.style.available" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change styles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.style.use('ggplot')\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.style.use('dark_background')\n", "plot_colored_circles()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The 'dark_background' style should not have gridlines - they are left over from the 'ggplot' style. We need to reset the style first. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function to properly reset the style" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def change_style(style_name='default'):\n", " # To reset the style\n", " plt.rcParams.update(plt.rcParamsDefault) \n", " # To restart the inline backend \n", " %matplotlib inline \n", " plt.style.use(style_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "change_style('dark_background')\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "change_style('fivethirtyeight')\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "change_style('tableau-colorblind10')\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "change_style('Solarize_Light2')\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "change_style('fast')\n", "plot_colored_circles()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colors\n", "\n", "On color maps in Matplotlib:\n", "- https://matplotlib.org/stable/users/explain/colors/colormaps.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function that plots a discretized color map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plot_color_map(cmap, num_colors=7):\n", " \"\"\"Plot the color map with the given number of colors\"\"\"\n", " data = np.atleast_2d(np.arange(num_colors))\n", " plt.imshow(data, cmap=cmap, vmin=0, vmax=num_colors-1)\n", " plt.yticks([])\n", " plt.show() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose a categorical color map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_color_map(plt.get_cmap('Set1_r'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define categorical colorblind safe colors as in http://mkweb.bcgsc.ca/colorblind/palettes.mhtml#conservative-8-color-palette-for-colorbliness" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List the colors\n", "safe_colors = [\n", " ( 0, 0, 0),\n", " (230, 159, 0),\n", " ( 86, 180, 233),\n", " ( 0, 158, 115),\n", " (240, 228, 66),\n", " ( 0, 114, 178),\n", " (213, 94, 0),\n", " (204, 121, 167),\n", "]\n", "# Normalize [0, 255]^3 RGB space to [0, 1]^3\n", "for i in range(len(safe_colors)): \n", " r, g, b = safe_colors[i] \n", " safe_colors[i] = (r / 255., g / 255., b / 255.) \n", "# Create a listed color map\n", "safe_map = ListedColormap(safe_colors, name='colorblind-safe')\n", "plot_color_map(safe_map)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose a sequential color map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_color_map(plt.get_cmap('viridis'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_color_map(plt.get_cmap('viridis_r'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose a diverging color map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_color_map(plt.get_cmap('RdBu'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_color_map(plt.get_cmap('RdBu_r'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use existing color maps to draw the colored circles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmap = plt.get_cmap('viridis')\n", "num_colors = 5\n", "plt.rcParams['axes.prop_cycle'] = cycler('color', [cmap(i) for i in range(num_colors + 1)])\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmap = plt.get_cmap('viridis')\n", "num_colors = 5\n", "plt.rcParams['axes.prop_cycle'] = cycler('color', [cmap(1.0 * i / num_colors) for i in range(num_colors + 1)])\n", "plot_colored_circles()" ] }, { "cell_type": "code", "execution_count": null, "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.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }