TransWikia.com

How to use arbitrary font in pgfplots?

TeX - LaTeX Asked by Thev on January 10, 2021

I produce the following chart:

enter image description here

With the following code (compiled with XeLaTeX):

documentclass[border=5pt]{standalone}
usepackage[table]{xcolor} % more control over colors
usepackage{tikz}
usepackage{pgfplots}
usepackage{pgfplotstable}
usepackage{amsmath}
usepackage{amssymb}

% fonts
usepackage{mathspec}
setmainfont{[opensans.ttf]}

begin{document}

begin{tikzpicture}

pgfplotstableread[col sep=comma]{
country, 2Q20, 3Q20
US, 2, -1
Euro Area, 2, -2
UK, 2,-3
PR China, 2,-4
Korea, 2,-5
Chinese Taipei, 2,-6
Singapore, 2,-7
Indonesia, 2,-8
Philippines, 2,-9
Malaysia, 2,-10
}chart

begin{axis}[
font = footnotesize,
width = 10cm,
height = 6cm,
ybar,
bar width=2mm,
axis lines=left,
enlarge x limits=0.1,
enlarge y limits={.1, upper},
%
% y ticks style and label
ymin=-13,
ylabel={year-on-year change (%)},
ylabel shift = 1pt,
ytick distance = 5,
%
% x axis ticks and style
xtick=data,
xticklabels from table={chart}{country}, 
axis x line shift={pgfkeysvalueof{/pgfplots/ymin}},       
xticklabel shift={-pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = coordindex,                   
x tick label style = {rotate=90},]
%
% done with the axis, now the plots
addplot [black, fill, draw opacity = 0] table [y=2Q20]  {chart};
addplot [red, fill, draw opacity = 0, nodes near coords] table [y=3Q20]  {chart};
end{axis}
end{tikzpicture}

end{document}

As you can see, I am using fontspec to use Open Sans as my typeface (to download the opensans.ttf file I use, go to the Overleaf for this chart).

However, it applies only to the text, and not to the axis labels or nodes.

How can I use the otf/ttf file of my choice and have it apply to all those as well?

One Answer

The axis labels are typeset in math mode and you're not declaring the math font.

documentclass[border=5pt]{standalone}
usepackage[table]{xcolor} % more control over colors
usepackage{tikz}
usepackage{pgfplots}
usepackage{pgfplotstable}
usepackage{amsmath}
usepackage{amssymb}

% fonts
usepackage{mathspec}
setmainfont{Open Sans}
setmathsfont(Digits,Latin,Greek){Open Sans}

begin{document}

begin{tikzpicture}

pgfplotstableread[col sep=comma]{
country, 2Q20, 3Q20
US, 2, -1
Euro Area, 2, -2
UK, 2,-3
PR China, 2,-4
Korea, 2,-5
Chinese Taipei, 2,-6
Singapore, 2,-7
Indonesia, 2,-8
Philippines, 2,-9
Malaysia, 2,-10
}chart

begin{axis}[
font = footnotesize,
width = 10cm,
height = 6cm,
ybar,
bar width=2mm,
axis lines=left,
enlarge x limits=0.1,
enlarge y limits={.1, upper},
%
% y ticks style and label
ymin=-13,
ylabel={year-on-year change (%)},
ylabel shift = 1pt,
ytick distance = 5,
%
% x axis ticks and style
xtick=data,
xticklabels from table={chart}{country}, 
axis x line shift={pgfkeysvalueof{/pgfplots/ymin}},       
xticklabel shift={-pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = coordindex,                   
x tick label style = {rotate=90},]
%
% done with the axis, now the plots
addplot [black, fill, draw opacity = 0] table [y=2Q20]  {chart};
addplot [red, fill, draw opacity = 0, nodes near coords] table [y=3Q20]  {chart};
end{axis}
end{tikzpicture}

end{document}

enter image description here

Correct answer by egreg on January 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP