Native and external processing tools

plot engine comparison.svg

Modify spines of the x or y axis

plot spines.svg

Add legend

  • Show legend at best determined location inside data, see 2.1
  • Position legend outside data, see 2.2
  • Position legend entries horizontally or in a grid, see 2.2
  • Annotate the lines directly within the data, see 2.3
  • See source code examples: Function legend - Show formulas for multiple functions

plot legend.svg

Differentiate functions with color or line style

See source code examples: Cycles - Differentiate data set with colors or line style

plot cycles.svg

Text

annotate interval
Multiline titles: Wrap overlong title

def wrap_title(axes):
    import textwrap as tw
    long_title = axes.get_title()
    axes.set_title(tw.fill(long_title, 20))

Terminology

Explicit Axes through Figure fig , Axes ax

Implicit Axes through PyPlot plt

  • figure: entire canvas
  • axes: subplot
  • spines: connecting lines between ticks
  • savefig.bbox: add tight padding around figure to prevent cropped legends
  • constrained_layout: Place elements next to each other instead of overlaying
  • titlesize 10: default fontsize for latex figures, allows longer titles
  • grid linesstyle: less visual dominant that solid lines, to keep focus on function line
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({'savefig.transparent':True, 'savefig.bbox':'tight', 
    'figure.constrained_layout.use':True, 'svg.fonttype':'none',
    'axes.titlesize': 10, 'axes.grid':True, 'grid.linestyle':':'})
plt.rc('axes.spines', left=False, top=False, right=False, bottom=False)
def plot_example_function(axes):
    x = np.linspace(0, 2.5, 100)
    axes.set_xlabel("x")
    axes.plot(x, np.cos(np.pi*x)*np.exp(-x), 
        label=r"$f(x) = \dfrac{\cos(\pi x)}{e^x}$")
    axes.legend()
 
fig, ax = plt.subplots(figsize=(6,2.2))
plot_example_function(ax)
# plt.savefig(@vault_path + '/attachments/plot .svg')
plt.show()

Dufte

Size

Legend

Color gradients

Layout multiple subfigures


Sources:

Related:

Tags: