I'm having an issue with setting the figure and font sizes in my seaborn plots to make sure that everything is the expected size and can be pasted into a Latex file for publication. Fig_size is set to \textwidth in the Latex document, i.e. 7 inches, but the resulting figure looks much smaller and very squished (s. first attached image). When I double the figure size it looks like what I'd expect. I would like to avoid doubling figure size because I'd like the font sizes to be the same as that in the main text and not have to do any conversions for that. It almost seems like fig_size uses cm as a unit but I looked it up and it should be inches. Does anyone have any insight into what's happening? Thank you in advance!
Figure created with fig_size=(textwidth, 3)
Figure created with fig_size=(textwidth x 2, 3 x 2)
textwidth = 7.00925
y_vars = ['test_1', 'test_2', 'test_3']
titles = ['Test 1', 'Test 2', 'Test 3']
fig, axes = plt.subplots(1, 3, figsize=(textwidth, 3), sharey=True)
for i, (y, title) in enumerate(zip(y_vars, titles)):
ax = axes[i]
# Stripplot
sns.stripplot(
x='diagnosis_grouped', y=y, data=plot_input, ax=ax,
palette='viridis', alpha=0.7, jitter=True
)
# Boxplot
sns.boxplot(
x='group', y=y, data=plot_input, ax=ax, showfliers=False,
width=0.3,
boxprops=dict(facecolor='none', edgecolor='black'),
medianprops=dict(color='black'),
whiskerprops=dict(color='black'),
capprops=dict(color='black')
)
ax.set_title(title, fontsize=font_size)
ax.set_xlabel('Group',fontsize=font_size)
if i == 0:
ax.set_ylabel('Score',fontsize=font_size)
else:
ax.set_ylabel('')
ax.tick_params(axis='x', rotation=20,labelsize=font_size)
ax.tick_params(axis='y', labelsize=font_size)
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig('/path/figure_1.eps', dpi=800)
It's unsurprising that when the figsize
is larger that then the points are more spread out. When setting the font_size=11
parameter (not set in your example) the labels are the correct size for me. I would also recommend using the text.usetex
parameter to ensure the axis are also rendered in Latex as follows
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import rcParams
rcParams.update({"text.usetex": True}) # use LaTeX for text rendering!
font_size = 11
fig, axes = plt.subplots(1, 3, figsize=(textwidth_in, height_in), sharey=True)
# Generate example data
plot_input = pd.DataFrame(dict(
{"diagnosis_grouped": np.random.choice(range(4), 200)},
**{y: np.random.rand(200) for y in y_vars}
))
.... # plotting code
# I'd recommend saving as a pdf
plt.savefig(f"/{path}/figure_1.pdf", bbox_inches="tight")
plt.show()
As a tip you can set all the font sizes globally with rcParams.update
for example
from matplotlib import rcParams
rcParams.update({
# match document font size
"font.size": 11,
"axes.labelsize": 11,
"axes.titlesize": 11,
"xtick.labelsize": 11,
"ytick.labelsize": 11,
"legend.fontsize": 11,
})
If this is still too cramped, you can set the size of the dots in sns.stripplot with the size
parameter. The default is 5, so you try smaller values.