Skip to main content

rsmf (right-size my figures) helps you prepare publication-ready figures with matplotlib.

Project description

Documentation Status

rsmf (right-size my figures)

When I am writing a paper I am a bit picky about the figures. It is especially important for me that the fonts and font sizes match the surrounding document. As I usually plot with matplotlib I created this library to help with that. This library provides a means to automatically adjust figure sizes and font sizes in matplotlib to fit the ones in commonly used scientific journals. Currently quantumarticle and revtex4 are supported.

Install

You can get the latest release version from PyPI.

pip install rsmf

To get the latest development version you have to install the package from GitHub.

pip install git+https://www.github.com/johannesjmeyer/rsmf

The package depends on matplotlib's PGF backend. To be able to use it, you need to have a working TeX distribution with pdflatex installed (see further Issue #2).

Usage

A detailed explanation of usage is given in the docs.

Setup

You need to tell rsmf how you set up your document by invoking rsmf.setup. This can be done in two ways. Either, you give rsmf the \documentclass string used for setting up the document, as in

import rsmf
formatter = rsmf.setup(r"\documentclass[a4paper,12pt,noarxiv]{quantumarticle}")

The r in front of the string is necessary so that \d is not mistaken for an escape sequence. If you have your document stored locally, there is an even more convenient way: you can just supply rsmf with the path to your main tex file (the one containing the document setup) and it will find that out for itself:

formatter = rsmf.setup("example.tex")

This is especially cool because rsmf will automatically adjust the plots when the underlying document class is changed without any needs to change python code! This makes swapping journals a lot easier.

If the document class you're targeting is not supported by rsmf, you can still use it. In that case you have to extract the relevant measurements yourself and use rsmf.CustomFormatter. A detailed explanation is given in the docs.

Figures

The setup routine will return a formatter. This formatter can then be used to create matplotlib figure objects by invoking the method formatter.figure. It has three arguments:

  • aspect_ratio (float, optional): the aspect ratio (width/height) of your plot. Defaults to the golden ratio.
  • width_ratio (float, optional): the width of your plot in multiples of \columnwidth. Defaults to 1.0.
  • wide (bool, optional): indicates if the figures spans two columns in twocolumn mode, i.e. if the figure* environment is used, has no effect in onecolumn mode . Defaults to False.

This is the place where you set the width of your plots, not in the LaTeX document. If you include the resulting figure with a different width, the font sizes will not match the surrounding document!

For example, a regular figure is created via

fig = formatter.figure(aspect_ratio=.5)

# ... some plotting ...
plt.savefig("example.pdf")

and included via

\begin{figure}
	\centering
	\includegraphics{example}
	\caption{...}
\end{figure}

A wide figure that spans 80% of the page on the other hand is created by

fig = formatter.figure(width_ratio=.8, wide=True)

# ... some plotting ...
plt.savefig("example_wide.pdf")

and included via the multi-column figure* environment:

\begin{figure*}
	\centering
	\includegraphics{example_wide}
	\caption{...}
\end{figure*}

Note that you should always save your figures in some sort of vectorized format, like pdf and that calling plt.tight_layout() before saving usually makes your plots nicer.

It is also possible to create the figure objects by hand, using formatter.columnwidth and formatter.wide_columnwidth, the formatter.figure routine is a convenience wrapper around this.

Other features

You can access the underlying fontsizes via formatter.fontsizes. The nomenclature follows that of LaTeX itself, so we have e.g. formatter.fontsizes.tiny or formatter.fontsizes.Large. This is especially useful if you want to tweak titles, legends and annotations while still having matching fontsizes.

Using rsmf with other frameworks

You can use rsmf together with your favorite plotting framework, for example seaborn. There is only one catch: if you use matplotlib styles or seaborn styles, you might overwrite the settings imposed by rsmf, especially regarding fontsize. To this end, the formatters have a method formatter.set_default_fontsizes that only change the underlying fontsizes. An example use would be

fig = formatter.figure(wide=True)
sns.set(style="ticks")
formatter.set_default_fontsizes()

# ... some plotting ...

Sometimes these styles also overwrite other things, like the font family (serif/sans-serif). There is no correction method for that yet.

Example

An example document alongside with a notebook for making the plots used can be found in the examples folder.

How it works

Under the hood, rsmf sets the matplotlib backend to pgf, which allows the use of LaTeX. For each supported document class, the specific column widths and font sizes are stored in code, alongside with packages that are loaded that change fonts. For quantumarticle, for example, the package lmodern is loaded into the pgf backend to get the right sans-serif font.

When calling rsmf.setup, matplotlib's rcParams are adjusted to make the fontsizes match the surrounding document. Note that formatter.figure does not mess with rcParams.

Contribute

Do you have trouble setting up plots for your favorite document class and it is not supported here? Do not hesitate to make a PR!

A big thanks for contributions goes to: Samuel J. Palmer

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

rsmf-0.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

rsmf-0.2-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file rsmf-0.2.tar.gz.

File metadata

  • Download URL: rsmf-0.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.5

File hashes

Hashes for rsmf-0.2.tar.gz
Algorithm Hash digest
SHA256 f9e7a99de829a4b53f6fbfd286e3895364df5eea91c0221701cbce4c66912fab
MD5 f4db77b062ceacadfc28f7cff13088ed
BLAKE2b-256 33ed8c53186d09a3fb9e3f53a28cfcca7bc6fa7f11b1988242097c06f7602dc6

See more details on using hashes here.

File details

Details for the file rsmf-0.2-py3-none-any.whl.

File metadata

  • Download URL: rsmf-0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.5

File hashes

Hashes for rsmf-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 212aff986f01febd36b52adf39f9f3e29b39acf40345c4be278cce8073412a77
MD5 23ce6df51de2641a9e721795b22856db
BLAKE2b-256 a51ed87a651039156920de90177051dddb53bb0e058cc24d6ca4c62517222799

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