如何设置 pgfplots 条形图中的轴?

如何设置 pgfplots 条形图中的轴?

我正在尝试设置一个pgfplots条形图,使其看起来像我用 Powerpoint 制作的图表。有人能帮我吗?谢谢

\documentclass{article}
\usepackage{pgfplots} \pgfplotsset{width=7cm,compat=1.8} 
\usepackage{graphicx}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
[ xbar, y axis line style = { opacity = 0 }, axis x line = none, tickwidth = 0pt, enlarge y limits = 0.2, enlarge x limits = 0.02, nodes near coords, symbolic y coords = {Tool cost, Material cost,Processing cost }, legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},] 
\addplot coordinates { (1.43 ,Processing cost) (0.01,Material cost) (43.69,Tool cost)};
\addplot coordinates {  (2.43,Processing cost)  (0.01,Material cost) (8.81,Tool cost)};
\legend{CNC machining, AM}
\end{axis} 
\end{tikzpicture}
  \caption[Cost per part comparison with CNC machined and AM inserts]{Cost per part comparison with CNC machined and AM inserts}
\label{fig:brass_piechart}  
\end{figure}

\end{document}

此外,我在每个图形下画一条线,像这样

\decoRule{\widthof{\includegraphics[width=\textwidth]{CNCvsAM}}}

我该如何调整它以使用 7cm 的 pgfplot 宽度?

在此处输入图片描述

答案1

由于 所做的\decoRule只是画一条黑线,最简单的方法是将其作为 的一部分tikzpicture,例如添加

\draw [line width=0.4pt] ([yshift=-2pt]current bounding box.south west) -- (current bounding box.south east);

之后\end{axis}。根据您的喜好调整,或者如果不需要,则完全-2pt删除。[yshift=<value>]

对于其他部分,您只需对选项进行一些添加axis,请参阅下面代码中的注释。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8} 
\usepackage{eurosym} % for euro symbol
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
[
  xbar,
  y axis line style = { opacity = 0 },
  xmajorgrids=true, % added 
  xticklabel={\geneuro\pgfmathprintnumber{\tick}}, % added
  xlabel={Cost per part}, % added 
  tickwidth = 0pt,
  enlarge y limits = 0.2,
  enlarge x limits = 0.02,
  nodes near coords style={/pgf/number format/.cd,fixed,precision=2}, % added
  nodes near coords,
  symbolic y coords = {Tool cost, Material cost,Processing cost },
  legend style={
    at={(0.5,-0.25)}, % modified
    anchor=north,
    legend columns=-1,
    draw=none}
] 
\addplot coordinates { (1.43 ,Processing cost) (0.01,Material cost) (43.69,Tool cost)};
\addplot coordinates {  (2.43,Processing cost)  (0.01,Material cost) (8.81,Tool cost)};
\legend{CNC machining, AM}
\end{axis} 
\draw [line width=0.4pt] ([yshift=-2pt]current bounding box.south west) -- (current bounding box.south east);    
\end{tikzpicture}
  \caption[Cost per part comparison with CNC machined and AM inserts]{Cost per part comparison with CNC machined and AM inserts}
\label{fig:brass_piechart}  
\end{figure}

\end{document}

相关内容