Skip to main content

Utilities for generating PGF figures from Matplotlib

Project description

pgfutils

The Portable Graphics Format (PGF) is a language for producing vector graphics within TeX documents. There is also a higher-level language TikZ (TikZ ist kein Zeichenprogramm -- TikZ is not a drawing program) which uses PGF.

Since version 1.2, the Python plotting library Matplotlib has included a PGF backend to generate figures ready for inclusion in a TeX document. In order to get consistent figures that fit with the style of the document, this requires some configuration. The aim of pgfutils is to simplify this configuration and allow figures to be easily generated from a Python script.

The module provides two functions which set up and then save the figure. The actual plotting is performed by standard Matplotlib functions. For example, to generate a plot showing the 1st, 3rd, 5th, 7th, 9th and 11th harmonics of an ideal square wave and the resulting sum:

# Set up the figure environment.
from pgfutils import setup_figure, save
setup_figure(width=0.9, height=0.4)

import numpy as np
from matplotlib import pyplot as plt

# Generate a square wave from a few terms of its Fourier series.
f = 3
t = np.linspace(0, 1, 501)
square = np.zeros(t.shape)
for n in range(1, 12, 2):
    # Create this harmonic and plot it as a dashed
    # partially-transparent line.
    component = np.sin(2 * n * np.pi * f * t) / n
    plt.plot(t, component, '--', alpha=0.4)

    # Add it to the overall signal.
    square += component

# Scale the final sum.
square *= 4 / np.pi

# Plot and label the figure.
plt.plot(t, square, 'C0')
plt.xlim(0, 1)
plt.ylim(-1.2, 1.2)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude (V)")

# Save as a PGF image.
save()

Requirements

pgfutils follows NEP29 when determining supported Python versions. Any minor version of Python released within the 42 months prior to a pgfutils release is supported.

The only required external dependency is Matplotlib. Any minor version of Matplotlib released within the 24 months prior to a pgfutils release is supported.

Examples

pgfutils comes with some examples which demonstrate its usage and integration into a build system. Depending on your installation method, these may be present somewhere in your filesystem (e.g., on a Linux system, they might be at /usr/share/matplotlib-pgfutils/examples). They can also be found in the extras/examples directory.

Documentation

Documentation for pgfutils can be found online at https://matplotlib-pgfutils.readthedocs.io/

Alternatively, you can find the source of this documentation in Markdown format in the doc/ directory of the source:

An example configuration file showing the default settings is given in extras/pgfutils.cfg.

Unit testing

Test Status codecov

Extensive unit tests are included in the tests/ directory of the source. Each commit to the source repository is automatically tested using GitHub actions. The test coverage (that is, how many of the lines of code in the source were executed during the tests) is monitored by Codecov. The badges above show the status of the last commit made to the source.

You can also run the tests on a local copy of the code. They are designed to be run with the pytest framework and employ the Coverage.py package via the pytest-cov plugin to measure the coverage. If you have these packages installed, run pytest from the top-level directory to execute the tests. A basic test coverage report will be printed on the terminal, and the full report can be viewed by opening the htmlcov/index.html file in your web browser.

License

pgfutils is released under the three-clause BSD license. The terms of this license can be found in the LICENSE file in the source, or online at https://opensource.org/licenses/BSD-3-Clause

The Cotham Sans font used in some unit tests is copyright (c) 2015 Sebastien Sanfilippo and is licensed under the SIL Open Font License, Version 1.1. The license can be found in the source at tests/sources/fonts/Cotham/OFL.txt or online at https://scripts.sil.org/OFL and the font itself can be found at https://github.com/sebsan/Cotham

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

matplotlib-pgfutils-1.7.0.tar.gz (323.5 kB view details)

Uploaded Source

Built Distribution

matplotlib_pgfutils-1.7.0-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file matplotlib-pgfutils-1.7.0.tar.gz.

File metadata

  • Download URL: matplotlib-pgfutils-1.7.0.tar.gz
  • Upload date:
  • Size: 323.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.10.2

File hashes

Hashes for matplotlib-pgfutils-1.7.0.tar.gz
Algorithm Hash digest
SHA256 2870b8081f5e4aed267aa45ae4eb15a8466a0c64a2f49c7cda428a88c24785d5
MD5 a4c7a5f7c88aee0a72874806a9154b55
BLAKE2b-256 711aaf936d84f30ab8ae6366951a6ea2f24110dbd34bfe8b3fc05a7e3641f273

See more details on using hashes here.

File details

Details for the file matplotlib_pgfutils-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: matplotlib_pgfutils-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.10.2

File hashes

Hashes for matplotlib_pgfutils-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0700b872217d271402f04acf26268a365a016b93299a679bb0e360623e1135e0
MD5 bb346ea141145eafe531ecd464490e6f
BLAKE2b-256 a1907f8fb80371893eab88a7da48d3e1c91f7a39df5ff37ee7dcd764b989eb19

See more details on using hashes here.

Supported by

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