# plotreset
PlotReset is a Python package that provides a simple way to reset and customize `matplotlib` plot styles. Comes with a sensible set of defaults for academic use. You can also extend the styles by adding more templates. Save and load templates in a JSON format to reuse.
## Installation
```bash
pip install plotreset
```
## Usage
```python
from plotreset import Styles
style=Styles('reset')
```
Then create a style object. Note that when you create the object with a specific style template name(`reset`, `academic` etc,.) it sets the style for you plots. All the plots you make after this will have the style applied.
```python
style=Styles('academic')
```
Where `academic` is a `plotreset` style(you can write your own or modify `academic` defaults) where latex font and settings are preloaded.
**To revert back to `matplotlib` default template simply create the object without any arguments**
```python
style=Styles()
```
### Example:
## Example.1 using the `reset` style template:
```python
import matplotlib.pyplot as plt
import numpy as np
from plotreset import Styles
style = Styles("reset")
plt.figure()
x = np.linspace(-5, 5, 200)
y = 1 / (np.sqrt(2 * np.pi)) * np.exp(-(x**2) / 2)
y2 = 1 / (np.sqrt(2 * np.pi * 2)) * np.exp(-(x**2) / (2 * 0.5))
y3 = 1 / (np.sqrt(2 * np.pi * 2)) * np.exp(-(x**2) / (2 * 1.5))
plt.plot(x, y, label="Gaussian Distribution")
plt.plot(x, y2, label="Gaussian Distribution")
plt.plot(x, y3, label="Gaussian Distribution")
plt.savefig("examples/simple.svg")
plt.show()
```
<img src="https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/simple.svg" alt="simple" role="img">
You can update the style settings:
```python
style.font.size = 18
style.font.family = "serif"
style.axes.grid = True
style.grid.alpha: 0.7
plt.plot(x, y, label="Gaussian Distribution")
```
### Example.12 using the `academic` style template and cycles:
`plotreset` also comes with a predefined set of defaults that can be used for example:
to cycle through colors, linestyles, markers, etc.
```python
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from cycler import cycler
from plotreset import Styles, defaults
style = Styles("reset")
c1 = cycler(color=defaults.COLORS[:4])
c2 = cycler(linestyle=defaults.LINE_STYLES[:4])
c3 = cycler(marker=defaults.MARKERS[:4])
x = np.linspace(0, 2 * np.pi, 50)
offsets = np.linspace(0, 2 * np.pi, 8, endpoint=False)
yy = np.transpose([np.sin(x + phi) for phi in offsets])
fig = plt.figure(figsize=(10, 4))
with mpl.rc_context({"axes.prop_cycle": c1}):
ax1 = fig.add_subplot(3, 1, 1)
ax1.plot(yy)
ax1.set_title("changing_colors")
with mpl.rc_context({"axes.prop_cycle": c1 + c2}):
ax2 = fig.add_subplot(3, 1, 2)
ax2.plot(yy)
ax2.set_title("changing linestyle and color")
with mpl.rc_context({"axes.prop_cycle": c1 + c2 + c3}):
ax3 = fig.add_subplot(3, 1, 3)
ax3.plot(yy)
ax3.set_title("changing linestyle, color and marker")
plt.show()
```
<img src="https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/cycles.svg" alt="cycles" role="img">
You can create your own templates and cycles and use them in the same way. (check the **Add more styles** section below for more details)
### Example.3 script using the `rc_context`:
Changing the style using dot notation(`style.font.size=10`) will change the plot settings in thje global scope but if you would like to change the settings for a specific plot you can use the `rc_context` method.
```python
import matplotlib as mpl
from plotreset import Styles
style = Styles("academic")
# Create example data
n = 30 * 75
p = 1 / 900
x = np.array(range(0, 15))
def res(n, p, x):
return math.comb(n, x) * p**x * (1 - p) ** (n - x)
p_x = np.array([res(n, p, i) for i in x])
color = ["tab:blue"] * len(x)
color[0] = "tab:orange"
# The default behavior of academic style is not to draw a grid on the axes.
# We can change this behavior by using the `rc_context` method so that this setting only affect this plot
rc_context = {"axes.grid": True, "axes.axisbelow": True}
with mpl.rc_context(rc_context):
plt.bar(x, p_x, color=color)
plt.xticks(x)
plt.ylabel("$\\mathrm{P(X)}$")
plt.xlabel("$\\mathrm{X}$")
plt.annotate(
"$\\mathrm{P(X=0) = 0.082}$",
xy=(x.max() / 2.0, p_x.max() / 2),
ha="left",
va="center",
)
plt.annotate(
"$\\mathrm{P(X\\geq1) = 0.918}$",
xy=(x.max() / 2.0, p_x.max() / 2 - 0.03),
ha="left",
va="center",
)
plt.annotate(
"$\\mathrm{E(X) = 2.49}$",
xy=(x.max() / 2.0, p_x.max() / 2 - 0.06),
ha="left",
va="center",
)
plt.show()
```
<img src="https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/binomial.svg" alt="binomial" role="img">
## Add more styles
You can add more styles in your scrit by creating a dictionary of style settings and call the `register_template` method to register the style.
```python
import plotreset
my_template = {
"axes.facecolor": "lightgray",
"font.size": 14,
# ... other style settings
}
plotreset.register_template("my_custom_style", my_template)
# Use custom template
styles = Styles("my_custom_style")
```
## Save and Load templates from a file
You can load custom templates from a JSON file and save the current template to a JSON file. When initializing the `Styles` object, you can specify the path to the JSON.
The JSON file for custom settings should have the following structure:
```json
{
"templates": {
"style_name1": {
// all the rc parameters as key-value pairs Ex: "text.usetex": true
}
"style_name2": {
// all the rc parameters as key-value pairs Ex: "font.size": 16,
}
}
}
```
You can give your style any name you want. But the **top level JSON key should be `templates`.**
```python
from plotreset import Styles
# Load a custom style(style_name1) from a JSON file(custom_style_templates.json)
style = Styles("style_name1", path="path/to/custom_style_templates.json")
```
You can also save the current style to a JSON:
```python
style.save_current_template("my_new_style_name", path="path/to/custom_style_templates.json")
```
If you sepecify `overwrite=True` in the `save_current_template` method and the same template name exist in the JSON file it will overwrite the template with updated style.
Here is an example JSON file with custom templates(this is the same as `academic` style):
```json
{
"templates": {
"academic": {
"text.usetex": true,
"mathtext.fontset": "cm",
"mathtext.fallback": "cm",
"mathtext.default": "regular",
"font.size": 16,
"font.family": "cmr10",
"axes.axisbelow": "line",
"axes.unicode_minus": false,
"axes.formatter.use_mathtext": true,
"axes.prop_cycle": {
"color": [
"tab:red",
"tab:blue",
"tab:green",
"tab:orange",
"tab:purple",
"tab:brown",
"tab:pink",
"tab:gray",
"tab:olive",
"tab:cyan",
"k"
]
},
"axes.grid": false,
"grid.linewidth": 0.6,
"grid.alpha": 0.5,
"grid.linestyle": "--",
"xtick.top": false,
"xtick.direction": "in",
"xtick.minor.visible": false,
"xtick.major.size": 6.0,
"xtick.minor.size": 4.0,
"ytick.right": false,
"ytick.direction": "in",
"ytick.minor.visible": false,
"ytick.major.size": 6.0,
"ytick.minor.size": 4.0,
"figure.constrained_layout.use": true
}
}
}
```
Raw data
{
"_id": null,
"home_page": null,
"name": "plotreset",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "plot, academic, matplotlib, latex, plotreset, mpl, style, wrapper, plotting, plotting-library",
"author": null,
"author_email": "Anoop K Chandran <anoopkcn@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/db/88/76ffcda5da78d4ec71a4de1303026142bf8aca9e8cc0e00540c868f464bc/plotreset-0.5.2.tar.gz",
"platform": null,
"description": "# plotreset\nPlotReset is a Python package that provides a simple way to reset and customize `matplotlib` plot styles. Comes with a sensible set of defaults for academic use. You can also extend the styles by adding more templates. Save and load templates in a JSON format to reuse.\n\n## Installation\n```bash\npip install plotreset\n```\n## Usage\n```python\nfrom plotreset import Styles\nstyle=Styles('reset')\n```\nThen create a style object. Note that when you create the object with a specific style template name(`reset`, `academic` etc,.) it sets the style for you plots. All the plots you make after this will have the style applied.\n\n```python\nstyle=Styles('academic')\n```\nWhere `academic` is a `plotreset` style(you can write your own or modify `academic` defaults) where latex font and settings are preloaded.\n\n**To revert back to `matplotlib` default template simply create the object without any arguments**\n```python\nstyle=Styles()\n```\n### Example:\n\n## Example.1 using the `reset` style template:\n\n```python\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom plotreset import Styles\n\nstyle = Styles(\"reset\")\n\nplt.figure()\nx = np.linspace(-5, 5, 200)\ny = 1 / (np.sqrt(2 * np.pi)) * np.exp(-(x**2) / 2)\ny2 = 1 / (np.sqrt(2 * np.pi * 2)) * np.exp(-(x**2) / (2 * 0.5))\ny3 = 1 / (np.sqrt(2 * np.pi * 2)) * np.exp(-(x**2) / (2 * 1.5))\nplt.plot(x, y, label=\"Gaussian Distribution\")\nplt.plot(x, y2, label=\"Gaussian Distribution\")\nplt.plot(x, y3, label=\"Gaussian Distribution\")\nplt.savefig(\"examples/simple.svg\")\nplt.show()\n```\n<img src=\"https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/simple.svg\" alt=\"simple\" role=\"img\">\n\nYou can update the style settings:\n```python\nstyle.font.size = 18\nstyle.font.family = \"serif\"\nstyle.axes.grid = True\nstyle.grid.alpha: 0.7\n\nplt.plot(x, y, label=\"Gaussian Distribution\")\n```\n\n### Example.12 using the `academic` style template and cycles:\n\n`plotreset` also comes with a predefined set of defaults that can be used for example:\n\nto cycle through colors, linestyles, markers, etc.\n\n```python\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom cycler import cycler\n\nfrom plotreset import Styles, defaults\n\nstyle = Styles(\"reset\")\nc1 = cycler(color=defaults.COLORS[:4])\nc2 = cycler(linestyle=defaults.LINE_STYLES[:4])\nc3 = cycler(marker=defaults.MARKERS[:4])\n\n\nx = np.linspace(0, 2 * np.pi, 50)\noffsets = np.linspace(0, 2 * np.pi, 8, endpoint=False)\nyy = np.transpose([np.sin(x + phi) for phi in offsets])\n\nfig = plt.figure(figsize=(10, 4))\nwith mpl.rc_context({\"axes.prop_cycle\": c1}):\n ax1 = fig.add_subplot(3, 1, 1)\n ax1.plot(yy)\n ax1.set_title(\"changing_colors\")\n\nwith mpl.rc_context({\"axes.prop_cycle\": c1 + c2}):\n ax2 = fig.add_subplot(3, 1, 2)\n ax2.plot(yy)\n ax2.set_title(\"changing linestyle and color\")\n\nwith mpl.rc_context({\"axes.prop_cycle\": c1 + c2 + c3}):\n ax3 = fig.add_subplot(3, 1, 3)\n ax3.plot(yy)\n ax3.set_title(\"changing linestyle, color and marker\")\n\nplt.show()\n```\n<img src=\"https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/cycles.svg\" alt=\"cycles\" role=\"img\">\n\nYou can create your own templates and cycles and use them in the same way. (check the **Add more styles** section below for more details)\n\n### Example.3 script using the `rc_context`:\nChanging the style using dot notation(`style.font.size=10`) will change the plot settings in thje global scope but if you would like to change the settings for a specific plot you can use the `rc_context` method.\n\n```python\nimport matplotlib as mpl\nfrom plotreset import Styles\n\nstyle = Styles(\"academic\")\n\n# Create example data\nn = 30 * 75\np = 1 / 900\nx = np.array(range(0, 15))\n\ndef res(n, p, x):\n return math.comb(n, x) * p**x * (1 - p) ** (n - x)\n\np_x = np.array([res(n, p, i) for i in x])\n\ncolor = [\"tab:blue\"] * len(x)\ncolor[0] = \"tab:orange\"\n\n# The default behavior of academic style is not to draw a grid on the axes.\n# We can change this behavior by using the `rc_context` method so that this setting only affect this plot\nrc_context = {\"axes.grid\": True, \"axes.axisbelow\": True}\n\nwith mpl.rc_context(rc_context):\n plt.bar(x, p_x, color=color)\n plt.xticks(x)\n plt.ylabel(\"$\\\\mathrm{P(X)}$\")\n plt.xlabel(\"$\\\\mathrm{X}$\")\n plt.annotate(\n \"$\\\\mathrm{P(X=0) = 0.082}$\",\n xy=(x.max() / 2.0, p_x.max() / 2),\n ha=\"left\",\n va=\"center\",\n )\n plt.annotate(\n \"$\\\\mathrm{P(X\\\\geq1) = 0.918}$\",\n xy=(x.max() / 2.0, p_x.max() / 2 - 0.03),\n ha=\"left\",\n va=\"center\",\n )\n plt.annotate(\n \"$\\\\mathrm{E(X) = 2.49}$\",\n xy=(x.max() / 2.0, p_x.max() / 2 - 0.06),\n ha=\"left\",\n va=\"center\",\n )\nplt.show()\n```\n<img src=\"https://raw.githubusercontent.com/anoopkcn/plotreset/refs/heads/main/examples/binomial.svg\" alt=\"binomial\" role=\"img\">\n\n## Add more styles\n\nYou can add more styles in your scrit by creating a dictionary of style settings and call the `register_template` method to register the style.\n\n```python\nimport plotreset\nmy_template = {\n \"axes.facecolor\": \"lightgray\",\n \"font.size\": 14,\n # ... other style settings\n}\nplotreset.register_template(\"my_custom_style\", my_template)\n\n# Use custom template\nstyles = Styles(\"my_custom_style\")\n```\n\n## Save and Load templates from a file\n\nYou can load custom templates from a JSON file and save the current template to a JSON file. When initializing the `Styles` object, you can specify the path to the JSON.\n\nThe JSON file for custom settings should have the following structure:\n```json\n{\n \"templates\": {\n \"style_name1\": {\n // all the rc parameters as key-value pairs Ex: \"text.usetex\": true\n }\n \"style_name2\": {\n // all the rc parameters as key-value pairs Ex: \"font.size\": 16,\n }\n }\n}\n```\nYou can give your style any name you want. But the **top level JSON key should be `templates`.**\n\n```python\nfrom plotreset import Styles\n\n# Load a custom style(style_name1) from a JSON file(custom_style_templates.json)\nstyle = Styles(\"style_name1\", path=\"path/to/custom_style_templates.json\")\n```\n\nYou can also save the current style to a JSON:\n```python\nstyle.save_current_template(\"my_new_style_name\", path=\"path/to/custom_style_templates.json\")\n```\nIf you sepecify `overwrite=True` in the `save_current_template` method and the same template name exist in the JSON file it will overwrite the template with updated style.\n\nHere is an example JSON file with custom templates(this is the same as `academic` style):\n```json\n{\n \"templates\": {\n \"academic\": {\n \"text.usetex\": true,\n \"mathtext.fontset\": \"cm\",\n \"mathtext.fallback\": \"cm\",\n \"mathtext.default\": \"regular\",\n \"font.size\": 16,\n \"font.family\": \"cmr10\",\n \"axes.axisbelow\": \"line\",\n \"axes.unicode_minus\": false,\n \"axes.formatter.use_mathtext\": true,\n \"axes.prop_cycle\": {\n \"color\": [\n \"tab:red\",\n \"tab:blue\",\n \"tab:green\",\n \"tab:orange\",\n \"tab:purple\",\n \"tab:brown\",\n \"tab:pink\",\n \"tab:gray\",\n \"tab:olive\",\n \"tab:cyan\",\n \"k\"\n ]\n },\n \"axes.grid\": false,\n \"grid.linewidth\": 0.6,\n \"grid.alpha\": 0.5,\n \"grid.linestyle\": \"--\",\n \"xtick.top\": false,\n \"xtick.direction\": \"in\",\n \"xtick.minor.visible\": false,\n \"xtick.major.size\": 6.0,\n \"xtick.minor.size\": 4.0,\n \"ytick.right\": false,\n \"ytick.direction\": \"in\",\n \"ytick.minor.visible\": false,\n \"ytick.major.size\": 6.0,\n \"ytick.minor.size\": 4.0,\n \"figure.constrained_layout.use\": true\n }\n }\n}\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A wrapper for matplotlib library to make academic plots with ease.",
"version": "0.5.2",
"project_urls": {
"Documentation": "https://github.com/anoopkcn/plotreset",
"Homepage": "https://github.com/anoopkcn/plotreset",
"Issues": "https://github.com/anoopkcn/plotreset/issues",
"Repository": "https://github.com/anoopkcn/plotreset.git"
},
"split_keywords": [
"plot",
" academic",
" matplotlib",
" latex",
" plotreset",
" mpl",
" style",
" wrapper",
" plotting",
" plotting-library"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "07c76f8523574bb4eba9abf849b4261b43047ea7b30bd3babb65330d63ed2495",
"md5": "4f2b8079f0d429a08fe19acffc2e5d39",
"sha256": "83533d58d8fec2df8efada8a60fe6ff04b3bdd555c882d05ec31c346f6946a13"
},
"downloads": -1,
"filename": "plotreset-0.5.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4f2b8079f0d429a08fe19acffc2e5d39",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10122,
"upload_time": "2024-10-17T15:52:35",
"upload_time_iso_8601": "2024-10-17T15:52:35.462607Z",
"url": "https://files.pythonhosted.org/packages/07/c7/6f8523574bb4eba9abf849b4261b43047ea7b30bd3babb65330d63ed2495/plotreset-0.5.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "db8876ffcda5da78d4ec71a4de1303026142bf8aca9e8cc0e00540c868f464bc",
"md5": "70f466b3d4dcb65d0086d19315dc9353",
"sha256": "261e2181d72949c6da8a13155fbf2191c13f4a9e88cddf6f9dce74345a957e61"
},
"downloads": -1,
"filename": "plotreset-0.5.2.tar.gz",
"has_sig": false,
"md5_digest": "70f466b3d4dcb65d0086d19315dc9353",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12371,
"upload_time": "2024-10-17T15:52:37",
"upload_time_iso_8601": "2024-10-17T15:52:37.164813Z",
"url": "https://files.pythonhosted.org/packages/db/88/76ffcda5da78d4ec71a4de1303026142bf8aca9e8cc0e00540c868f464bc/plotreset-0.5.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-17 15:52:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "anoopkcn",
"github_project": "plotreset",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "plotreset"
}