TeX - LaTeX Asked on May 15, 2021
I have bar chart where my node labels are overflowing. What is the best way to solve this?
documentclass{article}
usepackage{pgfplots}
begin{document}
begin{tikzpicture}
begin{axis}[
title=sample count per class,
xbar, xmin=0,
width=8cm, height=6cm, enlarge y limits=0.15,
symbolic y coords={{,},{.},{-},{:}, {?}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
point meta=rawx,
nodes near coords style={/pgf/number format/.cd,fixed},
]
addplot [xbar] coordinates {
(1290282,{.})
(2208970,{,})
(81710,{-})
(51192,{:})
(40511,{?})
};
end{axis}
end{tikzpicture}
end{document}
documentclass{article}
usepackage{pgfplots}
begin{document}
begin{tikzpicture}
begin{axis}[
title=sample count per class,
xbar, xmin=0,xmax=3000000,
width=8cm, height=6cm, enlarge y limits=0.15,
symbolic y coords={{,},{.},{-},{:}, {?}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
point meta=rawx,
nodes near coords style={/pgf/number format/.cd,fixed},
]
addplot [xbar] coordinates {
(1290282,{.})
(2208970,{,})
(81710,{-})
(51192,{:})
(40511,{?})
};
end{axis}
end{tikzpicture}
end{document}
Correct answer by SebGlav on May 15, 2021
Here I present an alternative, if you don't want to enlarge the ymax
. For details, please have a look at the comments in the code.
% used PGFPlots v1.17
documentclass[border=5pt]{standalone}
% we need PGFPlotsTable ...
usepackage{pgfplotstable}
% ... to store the data in an inline table
% (alternatively one could store it in a file)
pgfplotstableread{
x y
1290282 {.}
2208970 {,}
81710 {-}
51192 {:}
40511 {?}
}{MyData}
begin{document}
begin{tikzpicture}
begin{axis}[
title=sample count per class,
xbar, xmin=0,
width=8cm, height=6cm, enlarge y limits=0.15,
% % this avoids the need for `symbolic y coords` ...
% symbolic y coords={{,},{.},{-},{:},{?}},
% ... and instead we can load them from the table
yticklabels from table={MyData}{y},
ytick=data,
% ---------------------------------------------------------------------
% adjusted from <https://tex.stackexchange.com/a/335742/95441>
% ---------------------------------------------------------------------
% #1: the THRESHOLD after which we switch to a special display.
nodes near coords custom/.style={
small value/.style={
% everything is fine here, so I think there is no need
% to change something
},
large value/.style={
color=white,
anchor=east,
},
every node near coord/.style={
/pgf/number format/fixed,
check for zero/.code={%
pgfmathfloatifflags{pgfplotspointmeta}{0}{%
% If meta=0, make the node a coordinate (which doesn't have text)
pgfkeys{/tikz/coordinate}%
}{%
begingroup
% this group is merely to switch to FPU locally. Might be
% unnecessary, but who knows.
pgfkeys{/pgf/fpu}%
pgfmathparse{pgfplotspointmeta<#1}%
globalletresult=pgfmathresult
endgroup
%
% simplifies debugging:
%showresult
%
pgfmathfloatcreate{1}{1.0}{0}%
letONE=pgfmathresult
ifxresultONE
% AH : our condition 'y < #1' is met.
pgfkeysalso{/pgfplots/small value}%
else
% ok, proceed as usual.
pgfkeysalso{/pgfplots/large value}%
fi
}
},
check for zero,
},
},
nodes near coords={pgfmathprintnumber{pgfplotspointmeta}},
nodes near coords custom=5e5,
% ---------------------------------------------------------------------
]
% adjusted `coordinates` to `table`
addplot table [x=x,y expr=coordindex] {MyData};
end{axis}
end{tikzpicture}
end{document}
Answered by Stefan Pinnow on May 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP