brokenaxes


Namebrokenaxes JSON
Version 0.6.2 PyPI version JSON
download
home_pagehttps://github.com/bendichter/brokenaxes
SummaryCreate broken axes
upload_time2024-09-18 21:13:02
maintainerNone
docs_urlNone
authorBen Dichter
requires_pythonNone
licenseNone
keywords data visualization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # brokenaxes

![brokenaxes logo. Reference: http://www.brianhensley.net/2012/02/python-controlling-spi-bus-on.html](https://raw.githubusercontent.com/bendichter/brokenaxes/master/broken_python_snake.png)

brokenaxes makes matplotlib plots with breaks in the axes for showing data across a discontinuous range.

[![PyPI](https://img.shields.io/pypi/v/brokenaxes.svg?style=plastic)](https://pypi.python.org/pypi/brokenaxes)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/brokenaxes.svg?color=purple&label=PyPi)](https://pypistats.org/packages/brokenaxes)
[![codecov](https://codecov.io/gh/bendichter/brokenaxes/graph/badge.svg?token=emsyOdN4YD)](https://codecov.io/gh/bendichter/brokenaxes)

### Features
* Break x and y axes.
* Supports multiple breaks on a single axis.
* Automatically scales axes according to relative ranges.
* Plot multiple lines.
* Legend with positioning relative to entire broken axes object
* x and y label centered to entire plot
* Make brokenaxes object a subplot itself with `matplotlib.GridSpec.subplot_spec`.
* xlims and ylims may be `datetime.datetime` objects
* Supports log scales.

## Installation
I recommend the [Anaconda python distribution](http://continuum.io/downloads) and this package is available via pypi:
```
pip install brokenaxes
```

## Usage
```python
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
import numpy as np

fig = plt.figure(figsize=(5, 2))
bax = brokenaxes(xlims=((0, .1), (.4, .7)), ylims=((-1, .7), (.79, 1)), hspace=.05)
x = np.linspace(0, 1, 100)
bax.plot(x, np.sin(10 * x), label='sin')
bax.plot(x, np.cos(10 * x), label='cos')
bax.legend(loc=3)
bax.set_xlabel('time')
bax.set_ylabel('value')
```
![example1](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example1.png)

### Create subplots

```python
from brokenaxes import brokenaxes
from matplotlib.gridspec import GridSpec
import numpy as np

sps1, sps2 = GridSpec(2,1)

bax = brokenaxes(xlims=((.1, .3), (.7, .8)), subplot_spec=sps1)
x = np.linspace(0, 1, 100)
bax.plot(x, np.sin(x*30), ls=':', color='m')

x = np.random.poisson(3, 1000)
bax = brokenaxes(xlims=((0, 2.5), (3, 6)), subplot_spec=sps2)
bax.hist(x, histtype='bar')
```
![example2](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example2.png)

### Log scales

```python
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
import numpy as np

fig = plt.figure(figsize=(5, 5))
bax = brokenaxes(
    xlims=((1, 500), (600, 10000)),
    ylims=((1, 500), (600, 10000)),
    hspace=.15,
    xscale='log',
    yscale='log',
)

x = np.logspace(0.0, 4, 100)
bax.loglog(x, x, label='$y=x=10^{0}$ to $10^{4}$')

bax.legend(loc='best')
bax.grid(axis='both', which='major', ls='-')
bax.grid(axis='both', which='minor', ls='--', alpha=0.4)
bax.set_xlabel('x')
bax.set_ylabel('y')
plt.show()
```
![example3](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example3.png)


### datetime
```python
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
import numpy as np
import datetime

fig = plt.figure(figsize=(5, 5))
xx = [datetime.datetime(2020, 1, x) for x in range(1, 20)]

yy = np.arange(1, 20)

bax = brokenaxes(
    xlims=(
        (
            datetime.datetime(2020, 1, 1),
            datetime.datetime(2020, 1, 3),
        ),
        (
            datetime.datetime(2020, 1, 6),
            datetime.datetime(2020, 1, 20),
        )
    )
)

bax.plot(xx, yy)

fig.autofmt_xdate()
[x.remove() for x in bax.diag_handles]
bax.draw_diags()

import matplotlib.dates as mdates
for ax in bax.axs:
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b-%d'))
```

![datetime_example](https://raw.githubusercontent.com/bendichter/brokenaxes/master/datetime_example.png)

### text annotation
```python
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes

fig = plt.figure(figsize=(5, 5))
bax = brokenaxes(
    xlims=((0, 0.1), (0.4, 0.7)), ylims=((-1, 0.7), (0.79, 1))
)
bax.text(0.5, 0.5, "hello")
```
![text_example](https://github.com/bendichter/brokenaxes/assets/844306/35d2026a-a9e6-49df-9bf5-199e9a43b447)

## How do I do more?
You can customize brokenaxes outside of the supported features listed above. Brokenaxes works by creating a number of smaller axes objects, with the positions and sizes of those axes dictated by the data ranges used in the constructor. Those individual axes are stored as a list in `bax.axs`. Most customizations will require accessing those inner axes objects. (See the last two lines of [the datetime example](https://github.com/bendichter/brokenaxes#datetime)). There is also a larger invisible axes object, `bax.big_ax`, which spans the entire brokenaxes region and is used for things like x and y axis labels which span all of the smaller axes.


### Gallery
If you make a plot with this tool that you are proud of, send me a png and code and I'll add it to the gallery!

### Life advice
Please use this tool wisely. Any data visualization techique can be used to elucidate trends in the data, but can also be used to manipulate and mislead. The latter is particularly true for broken axes plots, so please try to use them responsibly. Other than that, this software is free to use. See the license file for details.

## Testing
brokenaxes uses `pytest-mpl` to ensure that the plots are created correctly.

To test that the plots are created correctly, run `pytest --mpl --mpl-baseline-path test_baseline test.py` from the root directory.

To generate new test plots, run `pytest --mpl-generate-path test_baseline test.py` from the root directory.

If you are running the tests on a headless server, you may need to set the MPLBACKEND environment variable to Agg.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bendichter/brokenaxes",
    "name": "brokenaxes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "data visualization",
    "author": "Ben Dichter",
    "author_email": "ben.dichter@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fe/24/3b8232880f8413f5e85bab6ecb5440418f071fdcbdc7090d858ee36026bb/brokenaxes-0.6.2.tar.gz",
    "platform": null,
    "description": "# brokenaxes\n\n![brokenaxes logo. Reference: http://www.brianhensley.net/2012/02/python-controlling-spi-bus-on.html](https://raw.githubusercontent.com/bendichter/brokenaxes/master/broken_python_snake.png)\n\nbrokenaxes makes matplotlib plots with breaks in the axes for showing data across a discontinuous range.\n\n[![PyPI](https://img.shields.io/pypi/v/brokenaxes.svg?style=plastic)](https://pypi.python.org/pypi/brokenaxes)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/brokenaxes.svg?color=purple&label=PyPi)](https://pypistats.org/packages/brokenaxes)\n[![codecov](https://codecov.io/gh/bendichter/brokenaxes/graph/badge.svg?token=emsyOdN4YD)](https://codecov.io/gh/bendichter/brokenaxes)\n\n### Features\n* Break x and y axes.\n* Supports multiple breaks on a single axis.\n* Automatically scales axes according to relative ranges.\n* Plot multiple lines.\n* Legend with positioning relative to entire broken axes object\n* x and y label centered to entire plot\n* Make brokenaxes object a subplot itself with `matplotlib.GridSpec.subplot_spec`.\n* xlims and ylims may be `datetime.datetime` objects\n* Supports log scales.\n\n## Installation\nI recommend the [Anaconda python distribution](http://continuum.io/downloads) and this package is available via pypi:\n```\npip install brokenaxes\n```\n\n## Usage\n```python\nimport matplotlib.pyplot as plt\nfrom brokenaxes import brokenaxes\nimport numpy as np\n\nfig = plt.figure(figsize=(5, 2))\nbax = brokenaxes(xlims=((0, .1), (.4, .7)), ylims=((-1, .7), (.79, 1)), hspace=.05)\nx = np.linspace(0, 1, 100)\nbax.plot(x, np.sin(10 * x), label='sin')\nbax.plot(x, np.cos(10 * x), label='cos')\nbax.legend(loc=3)\nbax.set_xlabel('time')\nbax.set_ylabel('value')\n```\n![example1](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example1.png)\n\n### Create subplots\n\n```python\nfrom brokenaxes import brokenaxes\nfrom matplotlib.gridspec import GridSpec\nimport numpy as np\n\nsps1, sps2 = GridSpec(2,1)\n\nbax = brokenaxes(xlims=((.1, .3), (.7, .8)), subplot_spec=sps1)\nx = np.linspace(0, 1, 100)\nbax.plot(x, np.sin(x*30), ls=':', color='m')\n\nx = np.random.poisson(3, 1000)\nbax = brokenaxes(xlims=((0, 2.5), (3, 6)), subplot_spec=sps2)\nbax.hist(x, histtype='bar')\n```\n![example2](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example2.png)\n\n### Log scales\n\n```python\nimport matplotlib.pyplot as plt\nfrom brokenaxes import brokenaxes\nimport numpy as np\n\nfig = plt.figure(figsize=(5, 5))\nbax = brokenaxes(\n    xlims=((1, 500), (600, 10000)),\n    ylims=((1, 500), (600, 10000)),\n    hspace=.15,\n    xscale='log',\n    yscale='log',\n)\n\nx = np.logspace(0.0, 4, 100)\nbax.loglog(x, x, label='$y=x=10^{0}$ to $10^{4}$')\n\nbax.legend(loc='best')\nbax.grid(axis='both', which='major', ls='-')\nbax.grid(axis='both', which='minor', ls='--', alpha=0.4)\nbax.set_xlabel('x')\nbax.set_ylabel('y')\nplt.show()\n```\n![example3](https://raw.githubusercontent.com/bendichter/brokenaxes/master/example3.png)\n\n\n### datetime\n```python\nimport matplotlib.pyplot as plt\nfrom brokenaxes import brokenaxes\nimport numpy as np\nimport datetime\n\nfig = plt.figure(figsize=(5, 5))\nxx = [datetime.datetime(2020, 1, x) for x in range(1, 20)]\n\nyy = np.arange(1, 20)\n\nbax = brokenaxes(\n    xlims=(\n        (\n            datetime.datetime(2020, 1, 1),\n            datetime.datetime(2020, 1, 3),\n        ),\n        (\n            datetime.datetime(2020, 1, 6),\n            datetime.datetime(2020, 1, 20),\n        )\n    )\n)\n\nbax.plot(xx, yy)\n\nfig.autofmt_xdate()\n[x.remove() for x in bax.diag_handles]\nbax.draw_diags()\n\nimport matplotlib.dates as mdates\nfor ax in bax.axs:\n    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b-%d'))\n```\n\n![datetime_example](https://raw.githubusercontent.com/bendichter/brokenaxes/master/datetime_example.png)\n\n### text annotation\n```python\nimport matplotlib.pyplot as plt\nfrom brokenaxes import brokenaxes\n\nfig = plt.figure(figsize=(5, 5))\nbax = brokenaxes(\n    xlims=((0, 0.1), (0.4, 0.7)), ylims=((-1, 0.7), (0.79, 1))\n)\nbax.text(0.5, 0.5, \"hello\")\n```\n![text_example](https://github.com/bendichter/brokenaxes/assets/844306/35d2026a-a9e6-49df-9bf5-199e9a43b447)\n\n## How do I do more?\nYou can customize brokenaxes outside of the supported features listed above. Brokenaxes works by creating a number of smaller axes objects, with the positions and sizes of those axes dictated by the data ranges used in the constructor. Those individual axes are stored as a list in `bax.axs`. Most customizations will require accessing those inner axes objects. (See the last two lines of [the datetime example](https://github.com/bendichter/brokenaxes#datetime)). There is also a larger invisible axes object, `bax.big_ax`, which spans the entire brokenaxes region and is used for things like x and y axis labels which span all of the smaller axes.\n\n\n### Gallery\nIf you make a plot with this tool that you are proud of, send me a png and code and I'll add it to the gallery!\n\n### Life advice\nPlease use this tool wisely. Any data visualization techique can be used to elucidate trends in the data, but can also be used to manipulate and mislead. The latter is particularly true for broken axes plots, so please try to use them responsibly. Other than that, this software is free to use. See the license file for details.\n\n## Testing\nbrokenaxes uses `pytest-mpl` to ensure that the plots are created correctly.\n\nTo test that the plots are created correctly, run `pytest --mpl --mpl-baseline-path test_baseline test.py` from the root directory.\n\nTo generate new test plots, run `pytest --mpl-generate-path test_baseline test.py` from the root directory.\n\nIf you are running the tests on a headless server, you may need to set the MPLBACKEND environment variable to Agg.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Create broken axes",
    "version": "0.6.2",
    "project_urls": {
        "Homepage": "https://github.com/bendichter/brokenaxes"
    },
    "split_keywords": [
        "data",
        "visualization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "987616158f60d2305cae16c07e25e9d7c5912c346071e2440fe6fe231982e0d9",
                "md5": "4e46e4a4fa3d97657cce573d9462897c",
                "sha256": "bc0a269c4a9a9a643504fbb7d734a279629cc2cada5ee90c57645e75c89f38ad"
            },
            "downloads": -1,
            "filename": "brokenaxes-0.6.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4e46e4a4fa3d97657cce573d9462897c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7278,
            "upload_time": "2024-09-18T21:13:01",
            "upload_time_iso_8601": "2024-09-18T21:13:01.440312Z",
            "url": "https://files.pythonhosted.org/packages/98/76/16158f60d2305cae16c07e25e9d7c5912c346071e2440fe6fe231982e0d9/brokenaxes-0.6.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe243b8232880f8413f5e85bab6ecb5440418f071fdcbdc7090d858ee36026bb",
                "md5": "cc5adeaa32bfcc878c2b77ef77840eda",
                "sha256": "bfa68242f003c02fe57ce7b5c5671c78c733ac8cc51a2be0f3ee6906c18e5d49"
            },
            "downloads": -1,
            "filename": "brokenaxes-0.6.2.tar.gz",
            "has_sig": false,
            "md5_digest": "cc5adeaa32bfcc878c2b77ef77840eda",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9783,
            "upload_time": "2024-09-18T21:13:02",
            "upload_time_iso_8601": "2024-09-18T21:13:02.895566Z",
            "url": "https://files.pythonhosted.org/packages/fe/24/3b8232880f8413f5e85bab6ecb5440418f071fdcbdc7090d858ee36026bb/brokenaxes-0.6.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-18 21:13:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bendichter",
    "github_project": "brokenaxes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "brokenaxes"
}
        
Elapsed time: 0.48096s