Skip to main content

Enhance your matplotlib plots with publication-quality style

Project description

deploy-docs

texplot is a python package to enhance matplotlib plots with publication-quality style.

Install

This package requires \(\LaTeX\) to be installed. Ensure that the latex executable is available on your system’s PATH.

Install with pip

pypi

pip install texplot

Install with conda

conda-version

conda install s-ameli::texplot

Set Theme

texplot can set theme either globally or locally:

Set Theme Globally

Call texplot.set_theme function to set the texplot theme globally in your script:

>>> import matplotlib.pyplot as plt
>>> import texplot
>>> texplot.set_theme()

>>> # Plot an example function
>>> fig, ax = plt.subplots()
>>> texplot.examples.plot_function(ax)
>>> plt.show()
https://github.com/ameli/texplot/raw/main/docs/source/_static/images/plots/function.png

The theme set as described above will affect your entire Python script for its duration. However, you can revert to the default matplotlib theme at any time by calling the texplot.reset_theme function as shown below:

>>> # Resetting to default matplotlib theme
>>> texplot.reset_theme()

>>> # Plot another example function
>>> fig2, ax2 = plt.subplots()
>>> texplot.examples.plot_function(ax2)
>>> plt.show()
https://github.com/ameli/texplot/raw/main/docs/source/_static/images/plots/function_no_theme.png

Set Theme Within a Local Scope

The texplot.theme function acts as a context manager, allowing you to apply the texplot theme within a specific local scope or function. The example below demonstrates setting the theme in a local scope. Outside of this scope, the default matplotlib theme remains unchanged.

>>> import matplotlib.pyplot as plt
>>> import texplot

>>> with texplot.theme():
>>>     fig, ax = plt.subplots()
>>>     texplot.examples.plot_function(ax)
>>>     plt.show()

Similarly, you can use the context manager with a function. In the example below, the texplot theme is applied only within the plot() function. Outside this function, the default matplotlib theme remains unchanged.

>>> import matplotlib.pyplot as plt
>>> import texplot

>>> @texplot.theme()
>>> def plot():
>>>     fig, ax = plt.subplots()
>>>     texplot.examples.plot_function(ax)
>>>     plt.show()

>>> plot()

Theme Options

You can customize the theme by passing arguments to either the texplot.set_theme or texplot.theme functions. The parameters for both functions are identical and detailed in the API reference. The available arguments are as follows:

Argument

Value

Description

context

'paper', 'notebook' (default), 'talk', or 'poster'

Adjusts font size and scales of the plot depending on the context.

style

See matplotlib.style.available

Sets matplotlib style

font_scale

float (default is 1)

Scales the fonts.

use_latex

boolean (default is None)

If True, the mathematical symbols are rendered with \(\LaTeX\).

rc

dictionary (default is {})

Passes any additional matplotlib’s rcParam dictionary.

In the example below, we configure a dark background style, increase the font size by a factor of 1.2, and set the font family to sans-serif:

>>> import matplotlib.pyplot as plt
>>> import texplot

>>> with texplot.theme(
...         rc={'font.family': 'sans-serif'},
...         style='dark_background',
...         font_scale=1.2):
>>>
>>>     # Plot an example diagram
>>>     fig, ax = plt.subplots()
>>>     texplot.examples.plot_bifurcation_diagram(ax)
>>>     plt.show()
https://github.com/ameli/texplot/raw/main/docs/source/_static/images/plots/logistic.png

Show and Save Plots

When working on a machine without display graphics, such as a remote server that lacks X11, displaying plots is not possible. Instead, plots should be saved. The texplot.save_plot function provides a simple wrapper around matplotlib.pyplot.savefig to facilitate this. Additionally, the texplot.show_or_save_plot function attempts to display plots initially. If no graphical backend is available, it saves the plot instead. Additionally, you can configure it to both show and save the plot. Here is an example:

>>> import matplotlib.pyplot as plt
>>> import texplot

>>> with texplot.theme(rc={'font.family': 'sans-serif'}):
>>>
>>>     # Plot an example function
>>>     fig, ax = plt.subplots()
>>>     texplot.examples.lorenz(ax)
>>>
>>>     # Show and save plot
>>>     texplot.show_or_save_plot(plt, default_filename='lorenz.pdf',
...                               transparent_background=True, dpi=200,
...                               show_and_save=True, verbose=True)
plot saved to '/home/user/lorenz.pdf'.
https://github.com/ameli/texplot/raw/main/docs/source/_static/images/plots/lorenz.png

Test Package

build-linux codecov-devel

To test the package, first clone the source code from the repository and install the required test packages by:

git clone https://github.com/ameli/texplot.git
cd texplot
python -m pip install -r tests/requirements.txt
python -m pip install .

Then, test with pytest:

pytest

How to Contribute

We welcome contributions via GitHub’s pull request. If you do not feel comfortable modifying the code, we also welcome feature requests and bug reports as GitHub issues.

License

license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

texplot-0.0.11.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

texplot-0.0.11-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file texplot-0.0.11.tar.gz.

File metadata

  • Download URL: texplot-0.0.11.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for texplot-0.0.11.tar.gz
Algorithm Hash digest
SHA256 804bfa1f97c71a2770c6656c3b6b117bf09b9da1cc149de70f96f0130812f32d
MD5 241426e421fd39c1d27efa9120b1aaa2
BLAKE2b-256 b082f4e4a3373b2d58dffad39f6d5bc8565c0ed2994410307d97d8394fd2e7a7

See more details on using hashes here.

File details

Details for the file texplot-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: texplot-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for texplot-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 631144702e6a62b75ae789bac47c52c4663bd9cc43eecd93f8b00792dd9f896f
MD5 94a321b8fadc09b67d04df0d7a0d693b
BLAKE2b-256 87b504fd866a2aea43bd1b86572cb6e89b46c5d846b0f5ee7b4ff817a3988e2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page