Docs for mpl_bsic - Version 1.2#

mpl_bsic helps you style matplotlib plots in BSIC style.

Setting up#

To make sure the plots are styled correctly, you must make sure that the fonts are installed on your computer and that matplotlib can recognize them. For now the process has been tested only on Macos. If it doesn’t work on Windows, shoot me a message.

  1. Download and install the fonts
    1. Download the fonts from the fonts folder in the main repository.

    2. Install the fonts (double click on the font files and click on “Install Font”).

  2. Clear your matplotlib cache.
    1. Go on your pc > users > [your-user] > .matplotlib

    2. If you cannot see the .matplotlib folder,

      press cmd + shift + . to show hidden files.

    3. Delete the fontlist-vXXX.json file.

[IMPORTANT] Plotting for BSIC Articles#

This section will explain how to correctly create plots for BSIC articles.

Choosing the figsize

Word documents have a maximum width of 7.32 inches. Any image larger than that will be resized to fit the page. If you end up resizing your image after exporting (or if Word does it for you), the font size of the Title of the plot won’t match the font size of the subsection titles in the article. Also, you won’t have consistent font sizes if you include more than 1 plot in the document.

To avoid this, you should always set the width of the figure to at most 7.32 inches before exporting. To get correct figsizes to use, and check that you set them correctly, you can use check_figsize().

Plotting the data correctly

In your python file, you will call apply_bsic_style() (and apply_bsic_logo()) on the plot you want to style.

Be sure to read the docs for apply_bsic_style() and apply_bsic_logo() thoroughly to see how to use them correctly.

from mpl_bsic import apply_bsic_style

fig, ax = plt.subplots(1,1)
apply_bsic_style(fig, ax)
ax.set_title('your title')

... # plot your data and apply the style
# fig.tight_layout() # DO NOT call this before exporting

# notice the arguments specified
fig.savefig("your_filename.svg", dpi=1200, bbox_inches="tight")

Exporting the plot

When exporting, we need to make sure that the figure is not resized automatically by matplotlib and that the font sizes stay consistent. To do that, you must not call fig.tight_layout() before exporting.

Additionally, you must export the figure with bbox_inches='tight' to make sure that no parts of the plot are cropped out.

My personal recommendation is to export in svg and set dpi to 1200, so that the quality will be the best possible.

fig, ax = plt.subplots(1,1)

... # plot your data and apply the style
# fig.tight_layout() # DO NOT call this before exporting

fig.savefig("your_filename.svg", dpi=1200, bbox_inches="tight")

Specific Use Cases#

Plotting Yield Curves

When plotting yield curves, to make the x ticks the same distance, regardless of time:

data.index = data.index.astype(str)

Functions#

mpl_bsic.apply_bsic_style(fig, ax[, sources])

Apply the BSIC Style to an existing matplotlib plot.

mpl_bsic.apply_bsic_logo(fig, ax[, scale, ...])

Apply the BSIC Logo to the Plot.

mpl_bsic.plot_trade(underlying, pnl, ...[, ...])

Plot a trade performance vs the underlying.

mpl_bsic.check_figsize([width, height, ...])

Check the validity of the figsize.

mpl_bsic.format_timeseries_axis(ax, ...[, fmt])

Format the x-axis of a timeseries plot.

mpl_bsic.preprocess_dataframe(df)

Handle and preprocess the DataFrame before plotting.

Indices and tables#