考虑以下代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{filecontents}{test.csv}
name,a,b
name1,1000,1000000
name2,1000,1000000
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma]{test.csv}\datatable
\begin{axis}[
xbar stacked, % Stacked horizontal bars
ytick=data, % Use as many tick labels as y coordinates
yticklabels from table={\datatable}{name},
scaled x ticks=false,
]
\addplot table [x=a, meta=name, col sep=comma, y expr=\coordindex] {test.csv};
\addplot table [x=b, meta=name, col sep=comma, y expr=\coordindex] {test.csv};
\legend{a,b}
\end{axis}
\end{tikzpicture}
\end{document}
我明白了
与普通绘图不同,x 刻度仍按比例缩放。如何让它们不按比例缩放,而是显示为 200,000 之类的值?
答案1
为此,您可以将number format
样式更改为fixed
。但这样一来,您就会遇到其他问题,如下面的结果所示。解决该问题(并保持该数字格式)的方法是更改刻度标签(字体)大小、 或axis
width
,tick distance
具体由您决定。
(此外,我还对您的代码做了一些其他改进,以防您不知道这些内容。)
% used PGFPlots v1.17
\begin{filecontents}{test.csv}
name,a,b
name1,1000,1000000
name2,1000,1000000
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% (just to make it look better)
width=\axisdefaultwidth,
height=3cm,
enlarge y limits={abs=0.5},
% ---
xbar stacked,
ytick=data,
% (you can state the file name here as well)
yticklabels from table={test.csv}{name},
scaled x ticks=false,
% with the "number format" you can choose how they show up
xticklabel style={
/pgf/number format/fixed,
},
% (moved common stuff here so you don't need to repeat it)
table/col sep=comma,
table/y expr=\coordindex,
table/meta=name,
]
\addplot table [x=a] {test.csv};
\addplot table [x=b] {test.csv};
\legend{a,b}
\end{axis}
\end{tikzpicture}
\end{document}