Skip to main content

Easy publication-ready matplotlib plots for ML papers and posters.

Project description

A Very Opiniated Publication-Ready Plotting Library

PyPI version codecov pytest mypy-lint ruff-lint ruff-format


I love TikZ (btw). I love tikzplotlib. I've been an advocate for the latter (proof). However, tikzplotlib is as good as dead. I need to move on.

[!IMPORTANT] Here's what I use now for all my publication needs. This library is designed to be very opiniated. Beauty is in the eye of the beholder. Also, it is very simple, just a single file and that's it.

[!NOTE] Of course I still use TikZ whenever possible (e.g. Fig. 1 in a paper, diagrams, etc.)

Examples

Left: ICML (letter size), single-column layout. Right: A0 landscape poster, 3-column layout.

       

import pub_ready_plots as prp

...

prp.get_context(
-   layout=prp.Layout.ICML,
+   layout=prp.Layout.POSTER_LANDSCAPE,

...

Installation

Still with me? Still want to use this library? Here's how:

pip install --upgrade pub-ready-plots

Quick usage

Wrap you current plotting script with the following with statement. By default, this will create a full-width, 0.15\textheight figure that conforms to the specified template.

By doing this, your figure is guaranteed to have the correct scaling, dimensions, font faces, and font sizes. All in all, this makes your figure "blends" with the target venue's main template---your paper overall will look more professional!

import pub_ready_plots as prp

with prp.get_context(layout=prp.Layout.ICLR) as (fig, axs):
    # Do whatever you want with `fig` and `axs`, e.g.:
    x = np.linspace(-1, 1)
    axs.plot(x, np.cos(x))

    # Once your done, save it, but do NOT set `tight_layout=True`!
    fig.savefig("filename.pdf")

Then in your LaTeX file, include the plot as follows:

\includegraphics[width=\linewidth]{filename.pdf}

The argument width=\linewidth is crucial! Also, do not specify the height option! Otherwise, your plot is distorted. (All measurements have been done in pub-ready-plots.)

If you set the width_frac argument when you call prp.get_context(), you need to put the same scaling factor in your LaTeX code. E.g., if you call prp.get_context(..., width_frac=0.5, ...), then you include it via \includegraphics[width=0.5\linewidth]{...}.

[!WARNING] If you want to have multiple subplots in a single figure in your paper, DO NOT create multiple pdf files! Instead, follow this example, while keeping \includegraphics[width=\linewidth] without a scaling factor.

That's it! But you should use TikZ more. Anyway, see the full, runnable example in examples/simple_plot.py See here for available options for get_context()!

[!TIP] I recommend using this library in conjunction with pypalettes to avoid the generic blue-orange Matplotlib colors. Distinguish your plots from others!

Supported Layouts

ICML, NeurIPS, ICLR, AISTATS, UAI, ProbML, JMLR, TMLR, A0 Poster (Landscape), A1 Poster (Portrait), 16:9 Beamer Slides, 16:9 Tuoying (Typst) Slides.

See: this file.

Advanced usages

Creating a figure with multiple subplots

To create a figure with multiple subplots do the following. Note that in your LaTeX doc, you still include the pdf via \includegraphics[width=\linewidth], without any scaling factor.

with prp.get_context(
    layout=prp.Layout.NEURIPS,
    width_frac=1,
    height_frac=0.15,
    nrows=1,
    ncols=2,
    sharey=True,
) as (fig, axs):
    # As an example, we plot sine and cosine functions
    x = np.linspace(-1, 1, 100)

    axs[0].plot(x, np.sin(x))
    axs[0].set_title("Sine")
    axs[0].set_xlabel(r"$x$")
    axs[0].set_ylabel(r"$\mathrm{sin}(x)$")

    axs[1].plot(x, np.cos(x))
    axs[1].plot(x, 2 * np.cos(x))
    axs[1].set_title("Cosine")
    axs[1].set_xlabel(r"$x$")
    axs[1].set_ylabel(r"$\mathrm{cos}(x)$")

    fig.savefig("subplots.pdf")

Creating plots for \wrapfigure

Say we want to have an inline figure of size 0.4\textwidth and height 0.15\textheight in our NeurIPS paper. Then all we have to do is the following:

import pub_ready_plots as prp

with prp.get_context(
    layout=prp.Layout.NEURIPS, width_frac=0.4, height_frac=0.15,
) as (fig, axs):
    # Your plot here!
    ...
    fig.savefig("mywrapfigure.pdf")

In our LaTeX doc, we can then use the wrapfig package and do the following:

Some paragraph.

\begin{wrapfigure}[11]{r}{0.4\textwidth}
  \centering
  \includegraphics[width=\linewidth]{mywrapfigure.pdf}
  ...
\end{wrapfigure}

Some other paragraph.

[!IMPORTANT] In the \begin{wrapfigure} statement, specify the correct figure size (in our case, 0.4\textwidth). Then, in the \includegraphics statement, always specify width=\linewidth without specifying the height.

Usage with Seaborn

This library is compatible with Seaborn, see example. The most important thing to remember is to always pass the axis returned by prp.get_context() to Seaborn's plotting functions. Example:

import seaborn as sns
import pub_ready_plots as prp

with prp.get_context(layout=prp.Layout.SLIDES_196) as (fig, ax):
    sns.histplot(
        data=sns.load_dataset("planets"), x="distance", log_scale=True
        ax=ax  # !! IMPORTANT !!
    )

Fonts too bold?

You might encounter this problem on MacOS, e.g., when using Avenir Next Condensed font. On MacOS, the font format is .ttc and Matplotlib has issue with it. So, please install the more standard .ttf or .otf version.

You can also easily change the font, e.g.:

import pub_ready_plots as prp

with prp.get_context(
    ...
    override_rc_params={"font.sans-serif": "YOUR_FONT_NAME"},
    ...
) as (fig, axs):
    ...

All available options

import pub_ready_plots as prp

with prp.get_context(
    layout=prp.Layout.ICML,  # check `Layout` for all available layouts
    width_frac=1,  # multiplier for `\linewidth`
    height_frac=0.15,  # multiplier for `\textheight`
    single_col=False,  # only works for the ICML, UAI, AISTATS layouts
    nrows=1,  # depending on your subplots, default = 1
    ncols=2,  # depending on your subplots, default = 1
    override_rc_params={"lines.linewidth": 4.123},  # Overriding rcParams
    sharey=True,  # Additional keyword args for `plt.subplots`
) as (fig, axs):
    ...

    fig.savefig("filename.pdf")

Using your own styles

Two options:

  1. Use this library and update the resulting rc_params dict with your styles.
  2. Fork this repo and modify things as you wish.

Dev's Guide

  1. Install uv.
  2. Create a virtualenv:
    uv venv
    
  3. Install dependencies:
    uv sync
    
  4. Run with, e.g.:
    uv run python examples/simple_plot.py
    
  5. If you want to use it in another (local) project:
    uv pip install -e .
    

Other libraries

Check out tueplots if you want a more complex library. My library is designed to achieve what I want in my papers and posters, with as little code as possible. Because of this, it is very forkable and hackable.

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

pub_ready_plots-1.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

pub_ready_plots-1.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file pub_ready_plots-1.5.tar.gz.

File metadata

  • Download URL: pub_ready_plots-1.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pub_ready_plots-1.5.tar.gz
Algorithm Hash digest
SHA256 9adc86de8f3daf8e24fbc97685fe7e4e030b0ef507ca9a5313e1c8c53bb86d42
MD5 049780e6ad01ee249a840ef0f9b9e008
BLAKE2b-256 bd488eda989a8c54e741ee13f0c9f3ad3cfbf72183c83b9cbafc9dc322cd0f74

See more details on using hashes here.

File details

Details for the file pub_ready_plots-1.5-py3-none-any.whl.

File metadata

  • Download URL: pub_ready_plots-1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pub_ready_plots-1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 234b154f8558e4faf9b97a336166442deb6fa61e93f3c31a225a557b0be88cc5
MD5 9bfab08970d3f253f4f51c5096574cdf
BLAKE2b-256 fe573516f74f9f828891ded14b92811f5d38b181b29bb3b0f5858d56edd65ba3

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