{ "cells": [ { "cell_type": "markdown", "id": "attempted-paragraph", "metadata": {}, "source": [ "# colorbars\n", "\n", "How to specify the levels of a colorbar ?" ] }, { "cell_type": "code", "execution_count": null, "id": "serious-stamp", "metadata": {}, "outputs": [], "source": [ "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "\n", "from osdyn.config import data_sample\n", "from osdyn.grcm.generic import moawi\n", "from osdyn.utils.plot.colorbar import auto_scale, histo\n", "from osdyn.utils.plot.colormap import get_cmap" ] }, { "cell_type": "code", "execution_count": null, "id": "crude-claim", "metadata": {}, "outputs": [], "source": [ "mc = moawi(data_sample(\"mars_siggen.nc\"), model=\"mars\", varnames=[\"temp_sfc\", \"temp_bot\"])\n", "var = mc.ds.temp_bot" ] }, { "cell_type": "code", "execution_count": null, "id": "vertical-accreditation", "metadata": {}, "outputs": [], "source": [ "mc.ds" ] }, { "cell_type": "markdown", "id": "southeast-feelings", "metadata": {}, "source": [ "## Classic quick search\n", "\n", "The data are used to get the minimum and maximum values" ] }, { "cell_type": "code", "execution_count": null, "id": "polar-reflection", "metadata": {}, "outputs": [], "source": [ "scale = auto_scale(var)\n", "scale" ] }, { "cell_type": "code", "execution_count": null, "id": "british-stationery", "metadata": {}, "outputs": [], "source": [ "def plot(scale):\n", " cmap = get_cmap(\"cmo.thermal\")\n", " norm = mpl.colors.BoundaryNorm(scale, ncolors=cmap.N, clip=True)\n", " plt.figure()\n", " c = plt.imshow(var[-1,...], cmap=\"cmo.thermal\", norm=norm)\n", " # plt.contour(mc.ds.temp_sfc.values[-1,...], cmap='cmo.thermal', levels=scale)\n", " plt.colorbar(c)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "unsigned-fiction", "metadata": {}, "outputs": [], "source": [ "plot(scale)" ] }, { "cell_type": "code", "execution_count": null, "id": "basic-independence", "metadata": {}, "outputs": [], "source": [ "scale = auto_scale(var, nmax=11)\n", "scale" ] }, { "cell_type": "code", "execution_count": null, "id": "dominican-breathing", "metadata": {}, "outputs": [], "source": [ "scale_n19 = auto_scale(var, nmax=19)\n", "scale_n19" ] }, { "cell_type": "code", "execution_count": null, "id": "false-vienna", "metadata": {}, "outputs": [], "source": [ "plot(scale_n19)" ] }, { "cell_type": "markdown", "id": "crucial-principle", "metadata": {}, "source": [ "## The colorbar separators flatten the histogram of the data" ] }, { "cell_type": "markdown", "id": "alpine-education", "metadata": {}, "source": [ "### Get the range" ] }, { "cell_type": "code", "execution_count": null, "id": "driving-netscape", "metadata": {}, "outputs": [], "source": [ "scalehist = auto_scale(var, nmax=12, flathisto=True, **{\"show\": False})\n", "scalehist" ] }, { "cell_type": "code", "execution_count": null, "id": "whole-psychiatry", "metadata": {}, "outputs": [], "source": [ "scalehist = auto_scale(var, nmax=20, flathisto=True, **{\"show\": True})\n", "scalehist" ] }, { "cell_type": "code", "execution_count": null, "id": "virtual-prototype", "metadata": {}, "outputs": [], "source": [ "len(scalehist)" ] }, { "cell_type": "code", "execution_count": null, "id": "eastern-lancaster", "metadata": {}, "outputs": [], "source": [ "plot(scalehist)" ] }, { "cell_type": "markdown", "id": "threatened-assistant", "metadata": {}, "source": [ "The scale without flattening the histogram differs although we ask for the same number of separators and the same bounds" ] }, { "cell_type": "code", "execution_count": null, "id": "otherwise-dressing", "metadata": {}, "outputs": [], "source": [ "scale_n19 = auto_scale(var, nmax=19)\n", "scale_n19" ] }, { "cell_type": "code", "execution_count": null, "id": "tired-brooks", "metadata": {}, "outputs": [], "source": [ "scale_n19 = auto_scale(var, nmax=19, vmin=11, vmax=17)\n", "scale_n19" ] }, { "cell_type": "markdown", "id": "different-nebraska", "metadata": {}, "source": [ "### Check the repartition of the values" ] }, { "cell_type": "code", "execution_count": null, "id": "damaged-transfer", "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "hist, bins = histo(var, bins=scalehist, show=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "micro-marsh", "metadata": {}, "outputs": [], "source": [ "hist, bins = histo(var, bins=scale_n19, show=True)" ] } ], "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.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }