plotagain


Nameplotagain JSON
Version 1.0.5 PyPI version JSON
download
home_page
SummaryA helper tool for matplotlib. Provides a contextmanager which automatically saves data used to make plots and automatically generates a script to re-load and re-plot this data at a later stage.
upload_time2023-07-23 14:15:45
maintainer
docs_urlNone
author
requires_python>=3.0
licenseBSD-3-Clause
keywords save data matplotlib plt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PlotAgain

This package provides a simple contextmanager which automatically saves data used to make matplotlib plots and automatically generates a script to reproduce the plots at a later stage.

## Install

`pip install plotagain`

## Basic Usage

A SavePlotContext contextmanager can be used in conjunction with a *with* block as shown below. 

```python
from plotagain import SavePlotContext


with SavePlotContext("./data-save-dir", locals(), overwrite=True) as spc:
    spc.plot( ... )
    ...
    spc.title( ... )
    spc.show()
```

The resulting context manager `spc` support all matplotlib.pyplot function calls.
All calls made to `spc` are passed onto matplotlib.pyplot as-is and recorded.
The data save directory is dynamically created if it does not exist and is populated with pickle files containing the data used to make the plot.
The automatically generated `make_plot.py` script contains code which reloads all data and contains the necessary calls to matplotlib.pyplot used to create the original plot.
This script may then be modified to adjust the plot.
If the data save directory is not empty, an exception is raised unless `overwrite=True`.
Passing in `locals()` allows SavePlotContext to infer the original variable names for the objects used for re-use in `make_plot.py`.

Note that only calls directly to spc are recorded, if you use figures and axes directly, you're on your own for now.
It is suggested that plots are kept simple in the scope of the code which generates them.
Once the individual plots have been generated it is easy to move around data and edit the autogenerated scripts to merge the individual plots using subplots.

## Example Usage

```python
import numpy as np
from plotagain import SavePlotContext


x_data = np.linspace(0, 2 * np.pi, 1000)
y_data = np.sin(x_data)
with SavePlotContext("./data-save-dir", locals(), overwrite=True) as spc:
    spc.plot(x_data, y_data, c='k', label='sin')
    spc.plot(x_data, np.cos(x_data), c='b', label='cos')
    spc.xlabel('xaxis')
    spc.ylabel('yaxis')
    spc.title('Title')
    spc.legend()
    spc.savefig('plot.pdf')
    spc.show()
```

This will generate the following directory structure

```
data-save-dir/
  make_plot.py
  unnamed_arg.pkl
  x_data.pkl
  y_data.pkl
```

The contents of `make_plot.py` would be

```python
import numpy as np
import matplotlib.pyplot as plt
from plotagain import load_pickle


x_data = load_pickle('x_data.pkl')
y_data = load_pickle('y_data.pkl')
unnamed_arg = load_pickle('unnamed_arg.pkl')

plt.plot(x_data, y_data, c='k', label='sin')
plt.plot(x_data, unnamed_arg, c='b', label='cos')
plt.xlabel('xaxis')
plt.ylabel('yaxis')
plt.title('Title')
plt.legend()
plt.savefig('plot.pdf')
plt.show()
```

which should perfectly reproduce the original plot.
Note that the variable names `x_data` and `y_data` are arbitrary and were inferred by the SavePlotContext instance using the locals() dict.
Good code structure will always allow SavePlotContext to infer the correct variable names.
Anonymous arguments, such as the y data of the `np.cos` plot are simply saved and named "unnamed_arg" with an integer postfix if multiple unnamed arguments are present.

## Misc

Please report any bugs or feature requests to `jero.wilkinson@gmail.com`.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "plotagain",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.0",
    "maintainer_email": "",
    "keywords": "save data,matplotlib,plt",
    "author": "",
    "author_email": "Jeremy John Henry Wilkinson <jero.wilkinson@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/11/72/2443a6e4c4fcedb4a598bdbf5d0205d43ba27b15133cdfb6e64807a35263/plotagain-1.0.5.tar.gz",
    "platform": null,
    "description": "# PlotAgain\n\nThis package provides a simple contextmanager which automatically saves data used to make matplotlib plots and automatically generates a script to reproduce the plots at a later stage.\n\n## Install\n\n`pip install plotagain`\n\n## Basic Usage\n\nA SavePlotContext contextmanager can be used in conjunction with a *with* block as shown below. \n\n```python\nfrom plotagain import SavePlotContext\n\n\nwith SavePlotContext(\"./data-save-dir\", locals(), overwrite=True) as spc:\n    spc.plot( ... )\n    ...\n    spc.title( ... )\n    spc.show()\n```\n\nThe resulting context manager `spc` support all matplotlib.pyplot function calls.\nAll calls made to `spc` are passed onto matplotlib.pyplot as-is and recorded.\nThe data save directory is dynamically created if it does not exist and is populated with pickle files containing the data used to make the plot.\nThe automatically generated `make_plot.py` script contains code which reloads all data and contains the necessary calls to matplotlib.pyplot used to create the original plot.\nThis script may then be modified to adjust the plot.\nIf the data save directory is not empty, an exception is raised unless `overwrite=True`.\nPassing in `locals()` allows SavePlotContext to infer the original variable names for the objects used for re-use in `make_plot.py`.\n\nNote that only calls directly to spc are recorded, if you use figures and axes directly, you're on your own for now.\nIt is suggested that plots are kept simple in the scope of the code which generates them.\nOnce the individual plots have been generated it is easy to move around data and edit the autogenerated scripts to merge the individual plots using subplots.\n\n## Example Usage\n\n```python\nimport numpy as np\nfrom plotagain import SavePlotContext\n\n\nx_data = np.linspace(0, 2 * np.pi, 1000)\ny_data = np.sin(x_data)\nwith SavePlotContext(\"./data-save-dir\", locals(), overwrite=True) as spc:\n    spc.plot(x_data, y_data, c='k', label='sin')\n    spc.plot(x_data, np.cos(x_data), c='b', label='cos')\n    spc.xlabel('xaxis')\n    spc.ylabel('yaxis')\n    spc.title('Title')\n    spc.legend()\n    spc.savefig('plot.pdf')\n    spc.show()\n```\n\nThis will generate the following directory structure\n\n```\ndata-save-dir/\n  make_plot.py\n  unnamed_arg.pkl\n  x_data.pkl\n  y_data.pkl\n```\n\nThe contents of `make_plot.py` would be\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom plotagain import load_pickle\n\n\nx_data = load_pickle('x_data.pkl')\ny_data = load_pickle('y_data.pkl')\nunnamed_arg = load_pickle('unnamed_arg.pkl')\n\nplt.plot(x_data, y_data, c='k', label='sin')\nplt.plot(x_data, unnamed_arg, c='b', label='cos')\nplt.xlabel('xaxis')\nplt.ylabel('yaxis')\nplt.title('Title')\nplt.legend()\nplt.savefig('plot.pdf')\nplt.show()\n```\n\nwhich should perfectly reproduce the original plot.\nNote that the variable names `x_data` and `y_data` are arbitrary and were inferred by the SavePlotContext instance using the locals() dict.\nGood code structure will always allow SavePlotContext to infer the correct variable names.\nAnonymous arguments, such as the y data of the `np.cos` plot are simply saved and named \"unnamed_arg\" with an integer postfix if multiple unnamed arguments are present.\n\n## Misc\n\nPlease report any bugs or feature requests to `jero.wilkinson@gmail.com`.\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "A helper tool for matplotlib. Provides a contextmanager which automatically saves data used to make plots and automatically generates a script to re-load and re-plot this data at a later stage.",
    "version": "1.0.5",
    "project_urls": {
        "Homepage": "https://bitbucket.org/jjhw3/plotagain/"
    },
    "split_keywords": [
        "save data",
        "matplotlib",
        "plt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5447f8d13debc03301821d26888b1a7c51a35b14b3129abb5eacfb9831255ea0",
                "md5": "e5463773f57e9968bb23fb8f0469574b",
                "sha256": "fb88fbe1e301602db1c0e5ec6c0754de70e0524d6c83fc7d7e8190789df95bba"
            },
            "downloads": -1,
            "filename": "plotagain-1.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e5463773f57e9968bb23fb8f0469574b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.0",
            "size": 10125,
            "upload_time": "2023-07-23T14:15:43",
            "upload_time_iso_8601": "2023-07-23T14:15:43.988097Z",
            "url": "https://files.pythonhosted.org/packages/54/47/f8d13debc03301821d26888b1a7c51a35b14b3129abb5eacfb9831255ea0/plotagain-1.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "11722443a6e4c4fcedb4a598bdbf5d0205d43ba27b15133cdfb6e64807a35263",
                "md5": "7c231822ebf18970e832ebb832306155",
                "sha256": "eb2f22ccbeca8b24c64c7a543db77230b13264680f7088c6f2d506543c7fd01d"
            },
            "downloads": -1,
            "filename": "plotagain-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "7c231822ebf18970e832ebb832306155",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.0",
            "size": 79562,
            "upload_time": "2023-07-23T14:15:45",
            "upload_time_iso_8601": "2023-07-23T14:15:45.839663Z",
            "url": "https://files.pythonhosted.org/packages/11/72/2443a6e4c4fcedb4a598bdbf5d0205d43ba27b15133cdfb6e64807a35263/plotagain-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-23 14:15:45",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "codeberg": false,
    "bitbucket_user": "jjhw3",
    "bitbucket_project": "plotagain",
    "lcname": "plotagain"
}
        
Elapsed time: 0.09477s