Last active
September 22, 2023 16:51
-
-
Save cwindolf/3e9866b2a495d8aa7cd39b6f0c1570e6 to your computer and use it in GitHub Desktop.
Example matplotlib config with latex fonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%config InlineBackend.figure_format = 'retina' | |
import matplotlib.pyplot as plt | |
from matplotlib.markers import MarkerStyle | |
from matplotlib.transforms import offset_copy | |
from matplotlib.patches import Ellipse, Rectangle, ConnectionPatch | |
from matplotlib.lines import Line2D | |
from matplotlib.legend_handler import HandlerTuple | |
import contextlib | |
plt.rc("figure", dpi=300) | |
plt.rc("figure", figsize=(4, 4)) | |
SMALL_SIZE = 5 | |
MEDIUM_SIZE = 7 | |
BIGGER_SIZE = 8 | |
plt.rc('font', size=SMALL_SIZE) | |
plt.rc('axes', titlesize=MEDIUM_SIZE) | |
plt.rc('axes', labelsize=SMALL_SIZE) | |
plt.rc('xtick', labelsize=SMALL_SIZE) | |
plt.rc('ytick', labelsize=SMALL_SIZE) | |
plt.rc('legend', fontsize=SMALL_SIZE) | |
plt.rc('figure', titlesize=BIGGER_SIZE) | |
plt.rcParams.update({ | |
"text.usetex": True, | |
# "font.family": "serif", | |
}) | |
preamble = r""" | |
\renewcommand{\familydefault}{\sfdefault} | |
\usepackage[scaled=1]{helvet} | |
\usepackage[helvet]{sfmath} | |
\usepackage{textgreek} | |
""" | |
plt.rc('text.latex', preamble=preamble) | |
plt.rc('svg', fonttype='none') | |
plt.rc('ps', usedistiller='xpdf') | |
plt.rc('pdf', fonttype=42) | |
def inline_xlabel(ax, label): | |
t = offset_copy( | |
ax.transAxes, | |
y=-(ax.xaxis.get_tick_padding() + ax.xaxis.majorTicks[0].get_pad()), | |
fig=ax.figure, | |
units='points', | |
) | |
ax.xaxis.set_label_coords(.5, 0, transform=t) | |
ax.set_xlabel(label, va='baseline', ha="center") | |
ax.xaxis.get_label().set_bbox(dict(facecolor='white', alpha=0.0, linewidth=0)) | |
def inline_ylabel(ax, label): | |
t = offset_copy( | |
ax.transAxes, | |
# changed to xaxis here bc yaxis has too much tick space? | |
x=-(ax.yaxis.get_tick_padding() + ax.yaxis.majorTicks[0].get_pad()), | |
fig=ax.figure, | |
units='points', | |
) | |
ax.yaxis.set_label_coords(0, .5, transform=t) | |
ax.set_ylabel(label, va='top', ha="center") | |
ax.yaxis.get_label().set_bbox(dict(facecolor='white', alpha=0.0, linewidth=0)) | |
def x_scalebar(ax, length, label=None, unit="s", lw=2, loc="lower left"): | |
if label is None: | |
label = f"{length}{unit}" | |
t_line = offset_copy( | |
trans=ax.get_xaxis_transform(), | |
y=-(ax.xaxis.get_tick_padding()), | |
fig=ax.figure, | |
units='points', | |
) | |
t_text = offset_copy( | |
trans=ax.get_xaxis_transform(), | |
y=-(ax.xaxis.get_tick_padding() + SMALL_SIZE), | |
fig=ax.figure, | |
units='points', | |
) | |
ax.figure.set_facecolor([0, 0, 0, 0]) | |
ax.figure.patch.set_facecolor([0, 0, 0, 0]) | |
line_x = [ax.get_xlim()[0], ax.get_xlim()[0] + length] | |
line_y = [0, 0] | |
line = Line2D(line_x, line_y, color="k", linewidth=lw, transform=t_line, solid_capstyle="butt") | |
ax.figure.add_artist(line) | |
ax.text(sum(line_x) / 2, 0, label, ha="center", va="center", transform=t_text) | |
def y_scalebar(ax, length, label=None, unit="\\textmu{}m", lw=2, loc="lower left"): | |
if label is None: | |
label = f"{length}{unit}" | |
t_line = offset_copy( | |
trans=ax.get_yaxis_transform(), | |
x=-(ax.yaxis.get_tick_padding()), | |
fig=ax.figure, | |
units='points', | |
) | |
t_text = offset_copy( | |
trans=ax.get_yaxis_transform(), | |
x=-(ax.yaxis.get_tick_padding() + SMALL_SIZE / 2), | |
# y=- SMALL_SIZE / 2, | |
fig=ax.figure, | |
units='points', | |
) | |
ax.figure.set_facecolor([0, 0, 0, 0]) | |
ax.figure.patch.set_facecolor([0, 0, 0, 0]) | |
line_x = [0, 0] | |
line_y = [ax.get_ylim()[0], ax.get_ylim()[0] + length] | |
line = Line2D(line_x, line_y, color="k", linewidth=lw, transform=t_line, solid_capstyle="butt") | |
ax.figure.add_artist(line) | |
ax.text(0, sum(line_y) / 2, label, ha="right", va="center", transform=t_text, rotation="vertical") | |
@contextlib.contextmanager | |
def subplots(*args, **kwargs): | |
fig, axes = plt.subplots(*args, **kwargs) | |
try: | |
yield fig, axes | |
finally: | |
plt.show() | |
plt.close(fig) | |
def clearpanel(figure): | |
figure.set_facecolor([0, 0, 0, 0]) | |
figure.patch.set_facecolor([0, 0, 0, 0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.rc("figure", dpi=300) | |
plt.rc("figure", figsize=(4, 4)) | |
SMALL_SIZE = 5 | |
MEDIUM_SIZE = 8 | |
BIGGER_SIZE = 10 | |
plt.rc('font', size=SMALL_SIZE) | |
plt.rc('axes', titlesize=MEDIUM_SIZE) | |
plt.rc('axes', labelsize=SMALL_SIZE) | |
plt.rc('xtick', labelsize=SMALL_SIZE) | |
plt.rc('ytick', labelsize=SMALL_SIZE) | |
plt.rc('legend', fontsize=SMALL_SIZE) | |
plt.rc('figure', titlesize=BIGGER_SIZE) | |
plt.rcParams.update({ | |
"text.usetex": True, | |
"font.family": "serif", | |
}) | |
preamble = r""" | |
\usepackage{textgreek} | |
\usepackage[upint]{stix} | |
\usepackage{newtxtext} | |
""" | |
plt.rc('text.latex', preamble=preamble) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment