{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# From categories\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The :class:`ptvis.color.CategoricalColorConversion` class defines a\nconversion from categories to colors.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly\nimport ptvis.color\n\n\nelements = list(ptvis.Element)\nvalues = [str(i % 3) for i in range(len(elements))]\n\nfig = plotly.graph_objects.Figure()\nptvis.attach_plain_cells(\n fig,\n elements,\n colors=values,\n color_conversion=ptvis.color.CategoricalColorConversion(),\n)\n\nplotly.io.show(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining a conversion explicitly\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `mapping` argument determines an explicit mapping from categories to\ncolors.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly\nimport ptvis.color\n\n\nelements = list(ptvis.Element)\nvalues = [str(i % 3) for i in range(len(elements))]\n\nfig = plotly.graph_objects.Figure()\nptvis.attach_plain_cells(\n fig,\n elements,\n colors=values,\n color_conversion=ptvis.color.CategoricalColorConversion(\n mapping={\"0\": \"red\", \"1\": \"green\", \"2\": \"blue\"},\n ),\n)\n\nplotly.io.show(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colors for categories missing from the mapping\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Categories missing from the `mapping` argument are converted using a preset\ncolorway.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly\nimport ptvis.color\n\n\nelements = list(ptvis.Element)\nvalues = [str(i % 3) for i in range(len(elements))]\n\nfig = plotly.graph_objects.Figure()\nptvis.attach_plain_cells(\n fig,\n elements,\n colors=values,\n color_conversion=ptvis.color.CategoricalColorConversion(\n mapping={\"0\": \"red\"},\n ),\n)\n\nplotly.io.show(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The colorway can be changed by the `missing_colors` argument.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly\nimport ptvis.color\n\n\nelements = list(ptvis.Element)\nvalues = [str(i % 3) for i in range(len(elements))]\n\nfig = plotly.graph_objects.Figure()\nptvis.attach_plain_cells(\n fig,\n elements,\n colors=values,\n color_conversion=ptvis.color.CategoricalColorConversion(\n mapping={\"0\": \"red\"},\n missing_colors=[\"cyan\", \"magenta\", \"yellow\"],\n ),\n)\n\nplotly.io.show(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color for N/A\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A color for N/A is given by the `na_color` argument.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly\nimport ptvis.color\n\n\nelements = list(ptvis.Element)\nvalues = [str(i % 3) if i % 10 else None for i in range(len(elements))]\n\nfig = plotly.graph_objects.Figure()\nptvis.attach_plain_cells(\n fig,\n elements,\n colors=values,\n color_conversion=ptvis.color.CategoricalColorConversion(\n mapping={\"0\": \"red\", \"1\": \"green\", \"2\": \"blue\"},\n na_color=\"white\",\n ),\n)\n\nplotly.io.show(fig)" ] } ], "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.13.3" } }, "nbformat": 4, "nbformat_minor": 0 }