fybdthemes


Namefybdthemes JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryColor palettes and plot themes
upload_time2023-10-21 18:30:05
maintainer
docs_urlNone
author
requires_python>=3.9
license
keywords color palettes themes
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fybdthemes

[![Project Status](http://www.repostatus.org/badges/latest/concept.svg)](http://www.repostatus.org/#concept)
[![ci](https://github.com/dirmeier/fybdthemes/workflows/ci/badge.svg)](https://github.com/dirmeier/fybdthemes/actions?query=workflow%3Aci)
[![version](https://img.shields.io/pypi/v/fybdthemes.svg?colorB=black&style=flat)](https://pypi.org/project/fybdthemes/)

## About 
 
This package contains some of my color palettes for usage in Python. One can choose between qualitative, diverging and sequential color palettes depending on the type of variable to be visualized 

## Installation

To install from PyPI:

```bash
pip install fybdthemes
```

To install the latest GitHub <RELEASE>, just call the following on the command line:

```bash
pip install git+https://github.com/dirmeier/fybdthemes@<RELEASE>
```

## Usage

`fybdthemes` works with both `matplotlib` or `seaborn`. You can either manually specify colors or provide matplotlib color maps. Below, we briefly demonstrate how to use the package.


```python
import numpy as np
import numpy.random

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

import fybdthemes
from fybdthemes.plot import plot_palette
```

Set a custom theme for matplotlib:


```python
fybdthemes.set_theme()
```

For visualization we use the well-known diamonds data:


```python
diamonds = sns.load_dataset("diamonds")
```

### Sequential colors 

Sequential colors are usually chosen for continuous variables. The sequential colors here revolve around the following three colors:


```python
plot_palette(fybdthemes.discrete_sequential_colors())
```




    
![png](README_files/README_11_0.png)
    



In the visualization above we use three discrete colors, but you can also choose more:


```python
plot_palette(fybdthemes.discrete_sequential_colors(10))
```




    
![png](README_files/README_13_0.png)
    




```python
plot_palette(fybdthemes.discrete_sequential_colors(10, True))
```




    
![png](README_files/README_14_0.png)
    



Usually for continuous variables, we want a continous scale though:


```python
plot_palette(fybdthemes.continuous_sequential_colors())
```




    
![png](README_files/README_16_0.png)
    



In a plot, we use the color scheme like this:


```python
_, ax = plt.subplots(figsize=(8, 4))
sns.scatterplot(
    x="carat", y="price",
    hue="price",
    palette=fybdthemes.continuous_sequential_colors(),
    data=diamonds, ax=ax,
    marker="+"
)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
plt.show()
```


    
![png](README_files/README_18_0.png)
    


### Diverging colors 

Diverging colors are usually chosen for continuous variables that can deviate in one of two directions relative to some midpoint. The diverging colors here revolve around the following four colors:


```python
plot_palette(fybdthemes.discrete_diverging_colors())
```




    
![png](README_files/README_20_0.png)
    



As above, you can specify more colors, too, and reverse them:


```python
plot_palette(fybdthemes.discrete_diverging_colors(10, True))
```




    
![png](README_files/README_22_0.png)
    



Since the visualized variable is usually continuous again, you want a continous scale again:


```python
plot_palette(fybdthemes.continuous_diverging_colors())
```




    
![png](README_files/README_24_0.png)
    



For plotting, we use the palette as before. In this case the midpoint would be if a diamond has 3 carats.


```python
_, ax = plt.subplots(figsize=(8, 4))
sns.scatterplot(
    x="carat", y="price",
    hue="carat",
    palette=fybdthemes.continuous_diverging_colors(),
    data=diamonds, ax=ax,
    marker="+"
)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()
```


    
![png](README_files/README_26_0.png)
    


### Qualitative colors 

Qualitative colors are usually chosen for categorical variables. The qualitative colors palette in this package has the following colors:


```python
plot_palette(fybdthemes.discrete_qualitative_colors())
```




    
![png](README_files/README_28_0.png)
    




```python
def sqeuclidean_cdist(X):
    X = X.reshape(-1, 1)    
    X2 = np.sum(np.square(X), 1)    
    dist = -2.0 * np.dot(X, np.transpose(X)) + (
        np.reshape(X2, (-1, 1)) + np.reshape(X2, (1, -1))
    )
    return  np.exp(-0.5 * np.clip(dist, 0.0, np.inf))

X = np.linspace(0, 10, 100)
K = sqeuclidean_cdist(X)
```

You can, for instance, use it like this:


```python
cols = fybdthemes.discrete_qualitative_colors()

_, ax = plt.subplots(figsize=(8, 4))
for i in range(5):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, label=i)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()
```


    
![png](README_files/README_31_0.png)
    


If you need less colors, manually specificying the colors to avoid a too colorful figure is usually a good idea. For instance, for plots with three lines you could use these colors:


```python
plot_palette(np.array(cols)[[0, 1, 3]])
```




    
![png](README_files/README_33_0.png)
    




```python
plot_palette(np.array(cols)[[0, 1, 4]])
```




    
![png](README_files/README_34_0.png)
    



In that case we merely need to specify the indexes of the colors:


```python
idxs = 0, 1, 4
_, ax = plt.subplots(figsize=(8, 4))
for i in idxs:
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, label=i)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()
```


    
![png](README_files/README_36_0.png)
    


The palette has a maximum of 6 colors. I've chosen to use only 6, because humans are usually not good at congitively processing more then 4-5 colors in a plot, so 6 is a hard maximum. 

For qualitative variables with more than 6 levels, I usually prefer a light/transarent blue sequential scale, since one cannot distinguish the colors effectively any more anyways. Alternatively one can use a greyscale for the variables, and highlight some few with colors. 

See the plot below as an example of a blue sequential scale. Note that in the plot below, we don't use a legend, cause we don't want to emphasize/highlight the separate lines, but rather show the general trend.


```python
cols = fybdthemes.discrete_sequential_colors(5)

_, ax = plt.subplots(figsize=(8, 4))
for i in range(5):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, alpha=.65)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()
```


    
![png](README_files/README_39_0.png)
    


A grey color scale to do the same as above can be created from seaborn:


```python
plot_palette(sns.color_palette("light:black", as_cmap=False))
```




    
![png](README_files/README_41_0.png)
    



If we want to put emphasis on a *single* line, we plot all *other* lines, for instance, in grey, and highlight the one we are interest in afterwards.


```python
cols = sns.color_palette("light:black", as_cmap=False, n_colors=9)
highlight_color = fybdthemes.discrete_sequential_colors(3)[1]

_, ax = plt.subplots(figsize=(8, 4))
for i in range(9):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=1, alpha=.65)
f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
plt.plot(X, f, color=highlight_color, linewidth=2)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()
```


    
![png](README_files/README_43_0.png)
    


## Author

Simon Dirmeier <a href="mailto:sfyrbnd @ pm me">sfyrbnd @ pm me</a>

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "fybdthemes",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "color palettes,themes",
    "author": "",
    "author_email": "Simon Dirmeier <sfyrbnd@pm.me>",
    "download_url": "",
    "platform": null,
    "description": "# fybdthemes\n\n[![Project Status](http://www.repostatus.org/badges/latest/concept.svg)](http://www.repostatus.org/#concept)\n[![ci](https://github.com/dirmeier/fybdthemes/workflows/ci/badge.svg)](https://github.com/dirmeier/fybdthemes/actions?query=workflow%3Aci)\n[![version](https://img.shields.io/pypi/v/fybdthemes.svg?colorB=black&style=flat)](https://pypi.org/project/fybdthemes/)\n\n## About \n \nThis package contains some of my color palettes for usage in Python. One can choose between qualitative, diverging and sequential color palettes depending on the type of variable to be visualized \n\n## Installation\n\nTo install from PyPI:\n\n```bash\npip install fybdthemes\n```\n\nTo install the latest GitHub <RELEASE>, just call the following on the command line:\n\n```bash\npip install git+https://github.com/dirmeier/fybdthemes@<RELEASE>\n```\n\n## Usage\n\n`fybdthemes` works with both `matplotlib` or `seaborn`. You can either manually specify colors or provide matplotlib color maps. Below, we briefly demonstrate how to use the package.\n\n\n```python\nimport numpy as np\nimport numpy.random\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport fybdthemes\nfrom fybdthemes.plot import plot_palette\n```\n\nSet a custom theme for matplotlib:\n\n\n```python\nfybdthemes.set_theme()\n```\n\nFor visualization we use the well-known diamonds data:\n\n\n```python\ndiamonds = sns.load_dataset(\"diamonds\")\n```\n\n### Sequential colors \n\nSequential colors are usually chosen for continuous variables. The sequential colors here revolve around the following three colors:\n\n\n```python\nplot_palette(fybdthemes.discrete_sequential_colors())\n```\n\n\n\n\n    \n![png](README_files/README_11_0.png)\n    \n\n\n\nIn the visualization above we use three discrete colors, but you can also choose more:\n\n\n```python\nplot_palette(fybdthemes.discrete_sequential_colors(10))\n```\n\n\n\n\n    \n![png](README_files/README_13_0.png)\n    \n\n\n\n\n```python\nplot_palette(fybdthemes.discrete_sequential_colors(10, True))\n```\n\n\n\n\n    \n![png](README_files/README_14_0.png)\n    \n\n\n\nUsually for continuous variables, we want a continous scale though:\n\n\n```python\nplot_palette(fybdthemes.continuous_sequential_colors())\n```\n\n\n\n\n    \n![png](README_files/README_16_0.png)\n    \n\n\n\nIn a plot, we use the color scheme like this:\n\n\n```python\n_, ax = plt.subplots(figsize=(8, 4))\nsns.scatterplot(\n    x=\"carat\", y=\"price\",\n    hue=\"price\",\n    palette=fybdthemes.continuous_sequential_colors(),\n    data=diamonds, ax=ax,\n    marker=\"+\"\n)\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nax.legend(title=\"lines\", bbox_to_anchor=(1.2, 0.5))\nplt.show()\n```\n\n\n    \n![png](README_files/README_18_0.png)\n    \n\n\n### Diverging colors \n\nDiverging colors are usually chosen for continuous variables that can deviate in one of two directions relative to some midpoint. The diverging colors here revolve around the following four colors:\n\n\n```python\nplot_palette(fybdthemes.discrete_diverging_colors())\n```\n\n\n\n\n    \n![png](README_files/README_20_0.png)\n    \n\n\n\nAs above, you can specify more colors, too, and reverse them:\n\n\n```python\nplot_palette(fybdthemes.discrete_diverging_colors(10, True))\n```\n\n\n\n\n    \n![png](README_files/README_22_0.png)\n    \n\n\n\nSince the visualized variable is usually continuous again, you want a continous scale again:\n\n\n```python\nplot_palette(fybdthemes.continuous_diverging_colors())\n```\n\n\n\n\n    \n![png](README_files/README_24_0.png)\n    \n\n\n\nFor plotting, we use the palette as before. In this case the midpoint would be if a diamond has 3 carats.\n\n\n```python\n_, ax = plt.subplots(figsize=(8, 4))\nsns.scatterplot(\n    x=\"carat\", y=\"price\",\n    hue=\"carat\",\n    palette=fybdthemes.continuous_diverging_colors(),\n    data=diamonds, ax=ax,\n    marker=\"+\"\n)\nax.legend(title=\"lines\", bbox_to_anchor=(1.2, 0.5))\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nplt.show()\n```\n\n\n    \n![png](README_files/README_26_0.png)\n    \n\n\n### Qualitative colors \n\nQualitative colors are usually chosen for categorical variables. The qualitative colors palette in this package has the following colors:\n\n\n```python\nplot_palette(fybdthemes.discrete_qualitative_colors())\n```\n\n\n\n\n    \n![png](README_files/README_28_0.png)\n    \n\n\n\n\n```python\ndef sqeuclidean_cdist(X):\n    X = X.reshape(-1, 1)    \n    X2 = np.sum(np.square(X), 1)    \n    dist = -2.0 * np.dot(X, np.transpose(X)) + (\n        np.reshape(X2, (-1, 1)) + np.reshape(X2, (1, -1))\n    )\n    return  np.exp(-0.5 * np.clip(dist, 0.0, np.inf))\n\nX = np.linspace(0, 10, 100)\nK = sqeuclidean_cdist(X)\n```\n\nYou can, for instance, use it like this:\n\n\n```python\ncols = fybdthemes.discrete_qualitative_colors()\n\n_, ax = plt.subplots(figsize=(8, 4))\nfor i in range(5):\n    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) \n    plt.plot(X, f, color=cols[i], linewidth=2, label=i)\nax.legend(title=\"lines\", bbox_to_anchor=(1.2, 0.5))\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nplt.show()\n```\n\n\n    \n![png](README_files/README_31_0.png)\n    \n\n\nIf you need less colors, manually specificying the colors to avoid a too colorful figure is usually a good idea. For instance, for plots with three lines you could use these colors:\n\n\n```python\nplot_palette(np.array(cols)[[0, 1, 3]])\n```\n\n\n\n\n    \n![png](README_files/README_33_0.png)\n    \n\n\n\n\n```python\nplot_palette(np.array(cols)[[0, 1, 4]])\n```\n\n\n\n\n    \n![png](README_files/README_34_0.png)\n    \n\n\n\nIn that case we merely need to specify the indexes of the colors:\n\n\n```python\nidxs = 0, 1, 4\n_, ax = plt.subplots(figsize=(8, 4))\nfor i in idxs:\n    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) \n    plt.plot(X, f, color=cols[i], linewidth=2, label=i)\nax.legend(title=\"lines\", bbox_to_anchor=(1.2, 0.5))\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nplt.show()\n```\n\n\n    \n![png](README_files/README_36_0.png)\n    \n\n\nThe palette has a maximum of 6 colors. I've chosen to use only 6, because humans are usually not good at congitively processing more then 4-5 colors in a plot, so 6 is a hard maximum. \n\nFor qualitative variables with more than 6 levels, I usually prefer a light/transarent blue sequential scale, since one cannot distinguish the colors effectively any more anyways. Alternatively one can use a greyscale for the variables, and highlight some few with colors. \n\nSee the plot below as an example of a blue sequential scale. Note that in the plot below, we don't use a legend, cause we don't want to emphasize/highlight the separate lines, but rather show the general trend.\n\n\n```python\ncols = fybdthemes.discrete_sequential_colors(5)\n\n_, ax = plt.subplots(figsize=(8, 4))\nfor i in range(5):\n    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) \n    plt.plot(X, f, color=cols[i], linewidth=2, alpha=.65)\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nplt.show()\n```\n\n\n    \n![png](README_files/README_39_0.png)\n    \n\n\nA grey color scale to do the same as above can be created from seaborn:\n\n\n```python\nplot_palette(sns.color_palette(\"light:black\", as_cmap=False))\n```\n\n\n\n\n    \n![png](README_files/README_41_0.png)\n    \n\n\n\nIf we want to put emphasis on a *single* line, we plot all *other* lines, for instance, in grey, and highlight the one we are interest in afterwards.\n\n\n```python\ncols = sns.color_palette(\"light:black\", as_cmap=False, n_colors=9)\nhighlight_color = fybdthemes.discrete_sequential_colors(3)[1]\n\n_, ax = plt.subplots(figsize=(8, 4))\nfor i in range(9):\n    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) \n    plt.plot(X, f, color=cols[i], linewidth=1, alpha=.65)\nf = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) \nplt.plot(X, f, color=highlight_color, linewidth=2)\nax.set_xlabel(r'$x$')\nax.set_ylabel(r'$f(x)$')\nplt.show()\n```\n\n\n    \n![png](README_files/README_43_0.png)\n    \n\n\n## Author\n\nSimon Dirmeier <a href=\"mailto:sfyrbnd @ pm me\">sfyrbnd @ pm me</a>\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Color palettes and plot themes",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/dirmeier/fybdthemes"
    },
    "split_keywords": [
        "color palettes",
        "themes"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "529e1d3cf825062f705b6ff445df773773a22258977253c9eea81647f0f88df0",
                "md5": "47a2575360fd49f7ad555fc2096f9f67",
                "sha256": "bdeaccc3104014cfbe086f1321446e8f32451cdfac1b11f56eaba980fb11cdd6"
            },
            "downloads": -1,
            "filename": "fybdthemes-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "47a2575360fd49f7ad555fc2096f9f67",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 6968,
            "upload_time": "2023-10-21T18:30:05",
            "upload_time_iso_8601": "2023-10-21T18:30:05.689851Z",
            "url": "https://files.pythonhosted.org/packages/52/9e/1d3cf825062f705b6ff445df773773a22258977253c9eea81647f0f88df0/fybdthemes-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-21 18:30:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dirmeier",
    "github_project": "fybdthemes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fybdthemes"
}
        
Elapsed time: 0.15683s