Skip to content

Figures

Neal W Morton edited this page Sep 6, 2020 · 13 revisions

Resources for making figures

This wiki page will have posted examples of code for different figures along with general tips for lab figures.

Matplotlib

You can set defaults for all the plots you make using Matplotlib in Python. You can change the default settings by defining options in ~/.matplotlib/matplotlibrc. Here are some good defaults to put in that file, which have been used to make publication-ready figures:

figure.autolayout : False
axes.labelsize : large
axes.spines.top : False
axes.spines.right : False
lines.linewidth : 1
xtick.labelsize : small
ytick.labelsize : small
font.sans-serif : Helvetica Neue LT Com
font.weight : 100
font.size : 18
savefig.bbox : tight
savefig.pad_inches : 0.02
pdf.fonttype: 42
ps.fonttype: 42

These settings are designed to match the Preston Lab house style, and to play well with Illustrator (the fonttype=42 setting makes sure the fonts are editable in Illustrator). The way MPL sets font weights is a little wonky, so it doesn't play well with the many options available for Helvetica Neue LT Com.

macOS

On macOS, he easiest way to make sure the right one gets used is to only install HelveticaNeueLTCom-Lt.ttf using Font Book; to remove the other weights, right click on one of the fonts in Font Book, and select "Show in Finder". Then move the other fonts out of that directory. After installing the font, in the Terminal run:

sudo fc-cache -fv
cd ~/.matplotlib
mkdir bak
mv font* bak

Restart ipython or Jupyter if you have a session running, and then make a test plot to make sure the settings are correct:

import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.savefig('test.pdf')

If you open test.pdf in Illustrator, the text should be editable; that is, if you select one of the text labels it should be an actual text object rather than just a shape. The font should be Helvetica Neue Lt Com, 45 Light.

Finally, move the other versions of Helvetica Neue LT Com back into the fonts directory. This should allow other programs to see all available weights, even though Matplotlib only sees one.

Linux (TACC)

Make a directory called $HOME/.local/share/fonts/. Then upload the .ttf font files you need to that directory. Run fc-cache to update your system font list. Use fc-list to display all installed fonts. You can place custom matplotlib options in a file at $HOME/.config/matplotlib/matplotlibrc. When saving plots using a job (rather than interactively in an idev session or on the virtual login node), configure matplotlib to make and save plots without displaying anything to the screen:

import matplotlib
matplotlib.use('Agg')

Alternatively, you can set Agg as the default backend by adding this line to your matplotlibrc file:

backend : Agg

Seaborn

Seaborn is a python package that is built on top of the matplotlib package. If you don't have either package installed in your python environment you can use the command pip install seaborn matplotlib
The easiest way to use seaborn is with pandas!
If you import a csv using fig_df = pd.read_csv('data_file.csv') you can call individual column names in seaborn
Example jointplot figure
Code used to generate figure sns.jointplot('PHC Post Class Acc','HIP RS Intx',data=fig_df,kind='reg',ci=95)

Clone this wiki locally