Skip to content

Setting math font when using mpl.Figure.figure #3853

@Auerilas

Description

@Auerilas

Hi,
I'm using seaborn objects to generate a custom subplotted grid, but the math font defaults to a serif font and I can't figure out how to change it. For example:

from matplotlib import style
import seaborn.objects as so
from os import chdir
theme = {**style.library['bmh'],
		'xtick.bottom': True,
		'ytick.left': True,
		'mathtext.default': 'regular'}

chdir('/home/nate/Downloads')
df = pd.read_csv('Soil_Ions_March2024.csv')
df_onoff = df.loc[(df['Distance.m']==0.0) | (df['Distance.m']==15.0)]
df_onoff['Trt'] = df_onoff['Distance.m'].map({0.0: 'Carcass', 15.0: 'Matrix'})

g = (so.Plot(x='Trt', y='NH4.mgkg', color='Trt', data=df_onoff)
     .add(so.Bar(), so.Est(), legend=False)
     .add(so.Range(), so.Est(errorbar='se'), legend=False)
     .layout(size=(4,6))
     .label(x='', y='Soil NH$_4$ (mg/kg)')
     .scale(x=so.Nominal(order=['Matrix', 'Carcass']),
            color=so.Nominal('colorblind'))
     .theme(theme)
     )

produces the figure I like (Fig 1).
Image

But then:

import matplotlib as mpl
f = mpl.figure.Figure(figsize=(6.5, 3), dpi=300, layout="constrained")
sf1, sf2, sf3 = f.subfigures(1, 3)

g = (so.Plot(x='Trt', y='NH4.mgkg', color='Trt', data=df_onoff)
     .add(so.Bar(), so.Est(), legend=False)
     .add(so.Range(), so.Est(errorbar='se'), legend=False)
     .layout(size=(4,6))
     .label(x='', y='Soil NH$_4$ (mg/kg)')
     .scale(x=so.Nominal(order=['On', 'Off']),
            color=so.Nominal('colorblind'))
     .theme(theme)
     )
g.on(sf1).plot()

g2 = (so.Plot(x='Trt', y='NO3.mgkg', color='Trt', data=df_onoff)
     .add(so.Bar(), so.Est(), legend=False)
     .add(so.Range(), so.Est(errorbar='se'), legend=False)
     .layout(size=(4,6))
     .label(x='', y='Soil NO$_3$ (mg/kg)')
     .scale(x=so.Nominal(order=['On', 'Off']),
            color=so.Nominal('colorblind'))
     .theme(theme)
     )
g2.on(sf2).plot()

g3 = (so.Plot(x='Trt', y='PO4.mgkg', color='Trt', data=df_onoff)
     .add(so.Bar(), so.Est(), legend=False)
     .add(so.Range(), so.Est(errorbar='se'), legend=False)
     .layout(size=(4,6))
     .label(x='', y='Soil PO$_4$ (mg/kg)')
     .scale(x=so.Nominal(order=['On', 'Off']),
            color=so.Nominal('colorblind'))
     .theme(theme)
     )
g3.on(sf3).plot()

f.savefig('fig2.png', bbox_inches='tight')

produces Fig 2, which has weird serif math fonts for the numbers. I know from the API that g.on() can revert to the underlying rcParams, but I have tried everything:

plt.style.use(so.Plot.config.theme), plt.rcParams = so.Plot.config.theme, etc.

I just can't seem to figure out how to fix the math font.
Image

Is there a way to fix this/perhaps upload an example to the API?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions