将轴格式化为货币

将轴格式化为货币

这个问题可能在某个地方已经得到解答,但我找不到。它看起来很简单,但我却不明白。

我有一个用 构建的图表pgfplots

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                legend style={draw=none},
                no markers,
                legend style={at={(0.95,0.825)}},
                xlabel = Simulation Day,
                ylabel = Dollars Committed,
                width=\textwidth,
                y tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=2,
                  /tikz/.cd
                },
                x tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                 /tikz/.cd
                }]
            \addplot table[line join=round,col sep=comma, y=Random, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize Random}}
            \addplot table[line join=round,col sep=comma, y=PlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResources, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTETri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostLN, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{LN}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTEExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{expo}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{expo}$}}
        \end{axis}
    \end{tikzpicture}
    \caption{Dollar Commitment versus Time}
    \label{DollarCommitment}
\end{figure}

\end{document}

我的数据文件的第一行和最后几行如下所示:

Day,Random,PlusCost,PlusFTE,PlusPlusCost,PlusPlusFTE,PlusPlusResources,PlusPlusCostTri,PlusPlusFTETri,PlusPlusResourcesTri,PlusPlusCostLN,PlusPlusFTEExpo,PlusPlusResourcesExpo
1,2370.58,2426.6,3837.24,138.07,891.1,704.33,12,142.3,12.3,138.07,1258.2,803.73
2,2500.9,2511.08,3997.04,142.65,919.08,732.31,12,144.06,14.06,142.65,1307.55,831.71
3,2601.19,2561.86,4107.32,146.8,945.58,758.81,13,144.06,14.06,146.8,1346.23,858.21
4,2732.52,2616.11,4269.04,150.18,991.16,764.19,14,144.06,14.06,150.18,1423.23,877.79
5,2795.7,2665.49,4338.42,154.06,1041.76,790.29,14,144.94,14.94,154.06,1490.61,903.89
256,9113.63,7826.68,9755.47,4506.6,9849.94,9740.52,6166.75,9380.47,8305.05,4512,9853.96,9772.93
257,9118.22,7837.27,9755.97,4524.37,9858.53,9753.11,6195.49,9407.53,8334.82,4529.77,9858.55,9782.02
258,9136.1,7838.15,9755.97,4572.79,9858.53,9753.61,6226.09,9408.53,8355.91,4581.78,9863.83,9782.02
259,9140.6,7838.65,9756.97,4633.55,9864.14,9758.61,6309.13,9432.89,8402.86,4636.95,9864.83,9786.52
260,9140.6,7843.03,9756.97,4708.84,9864.64,9760.11,6376.78,9437.86,8436.03,4712.24,9865.33,9804.52

当我制作图表时,我的y轴以科学计数法标记,即值的范围从到0.00000顶部有1.00000一个大符号。我想将轴格式化为货币,但我无论如何也想不出该怎么做。现在感觉很蠢……(dot)10^4y

答案1

您可以使用scaled ticks=false来防止缩放。有关详细信息,请参阅 pgfplots 的第 289 页 4.15.3。要$使用

yticklabel={${\$\pgfmathprintnumber{\tick}}$}

代码:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents*}{DollarCommitment.csv}
Day,Random,PlusCost,PlusFTE,PlusPlusCost,PlusPlusFTE,PlusPlusResources,PlusPlusCostTri,PlusPlusFTETri,PlusPlusResourcesTri,PlusPlusCostLN,PlusPlusFTEExpo,PlusPlusResourcesExpo
1,2370.58,2426.6,3837.24,138.07,891.1,704.33,12,142.3,12.3,138.07,1258.2,803.73
2,2500.9,2511.08,3997.04,142.65,919.08,732.31,12,144.06,14.06,142.65,1307.55,831.71
3,2601.19,2561.86,4107.32,146.8,945.58,758.81,13,144.06,14.06,146.8,1346.23,858.21
4,2732.52,2616.11,4269.04,150.18,991.16,764.19,14,144.06,14.06,150.18,1423.23,877.79
5,2795.7,2665.49,4338.42,154.06,1041.76,790.29,14,144.94,14.94,154.06,1490.61,903.89
256,9113.63,7826.68,9755.47,4506.6,9849.94,9740.52,6166.75,9380.47,8305.05,4512,9853.96,9772.93
257,9118.22,7837.27,9755.97,4524.37,9858.53,9753.11,6195.49,9407.53,8334.82,4529.77,9858.55,9782.02
258,9136.1,7838.15,9755.97,4572.79,9858.53,9753.61,6226.09,9408.53,8355.91,4581.78,9863.83,9782.02
259,9140.6,7838.65,9756.97,4633.55,9864.14,9758.61,6309.13,9432.89,8402.86,4636.95,9864.83,9786.52
260,9140.6,7843.03,9756.97,4708.84,9864.64,9760.11,6376.78,9437.86,8436.03,4712.24,9865.33,9804.52
\end{filecontents*}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                legend style={draw=none},
                no markers,
                legend style={at={(0.95,0.825)}},
                xlabel = Simulation Day,
                ylabel = Dollars Committed,
                width=\textwidth,
                y tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=2,
                  /tikz/.cd
                },
                x tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                 /tikz/.cd
                },
                scaled ticks=false,
                yticklabel={${\$\pgfmathprintnumber{\tick}}$},
                ]
            \addplot table[line join=round,col sep=comma, y=Random, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize Random}}
            \addplot table[line join=round,col sep=comma, y=PlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResources, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTETri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostLN, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{LN}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTEExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{expo}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{expo}$}}
        \end{axis}
    \end{tikzpicture}
    \caption{Dollar Commitment versus Time}
    \label{DollarCommitment}
\end{figure}

\end{document}

在此处输入图片描述

您最好使用 调整图例的位置legend style={at={(0.95,0.825)}},。使用适当的坐标。顺便说一句,使用\pgfplotsset{compat = newest}不是一个好主意,因为它可能会在将来带来麻烦。使用您当前的版本,例如\pgfplotsset{compat = 1.9}

相关内容