2

I am making bar charts using pgfplots and I would like to change the font of the "data labels" to a sans-serif font (preferably Myriad Pro), but I'm having no luck. I am compiling with XeLaTeX

I've tried to use the package mathastext as suggested here (pgfplots - How to get the y axis in sans serif), but my data labels are displayed as serif.

Here is a minimum working example: Any thoughts on how to change the data labels to sans-serif? Thank you!!

\documentclass{article}
\usepackage{fontspec}
\usepackage{pgfplots}
\defaultfontfeatures{Ligatures=TeX}
\setromanfont{Myriad Pro}
\setsansfont{Myriad Pro}
\renewcommand\familydefault{\sfdefault}
\usepackage{mathastext}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
symbolic y coords={
    {Disability},
    {Death},
    {Congenital Anomaly}
}, % name of categorical data
ytick=data,
]
\addplot[
xbar, 
fill={black},  
nodes near coords, % adds data labels on each bar
nodes near coords align={horizontal}, % aligns data labels on each bar
nodes near coords style={font=\large}
] coordinates {
    (10,{Congenital Anomaly})
    (1199,{Death})
    (285,{Disability})
};
\end{axis}
\end{tikzpicture}

\end{document}

Here is the output: serif data labels

3

You don't need extra packages for this. The reason why numbers are still serif is because they are typeset in math mode i.e. every number has $...$ around them because that's what you generally want and pgfplots is trying to help you. However you can turn off this behavior by using assume math mode for the number printing such that it assumes that it is already inside math mode and prints the number as is. Example without any fonts (notice I don't redefine any macro and neither should you)

\documentclass[]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\sffamily

\begin{tikzpicture}
\begin{axis}[
symbolic y coords={
    {Disability},
    {Death},
    {Congenital Anomaly}
}, % name of categorical data
ytick=data,
tick label style={/pgf/number format/assume math mode}
]
\addplot[
xbar, 
fill={black},  
nodes near coords, % adds data labels on each bar
nodes near coords align={horizontal}, % aligns data labels on each bar
nodes near coords style={font=\large,/pgf/number format/assume math mode}
] coordinates {
    (10,{Congenital Anomaly})
    (1199,{Death})
    (285,{Disability})
};
\end{axis}
\end{tikzpicture}
\end{document}

Same should also be applied to tick labels too but that's a minor addition

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.