tikzpicture 轴上的货币价值

tikzpicture 轴上的货币价值

我如何获得以下内容;

(a)每个轴标签前面的欧元符号。

(b)每个数字都保留两位小数,因此 145.50 显示为 145.50,而不是 14.5

\documentclass[12pt]{article}
\usepackage{marvosym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{fillbetween}
\usepackage[gen]{eurosym}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[width=20cm, height=20cm, axis lines = center, xlabel = {Expiration Value}, ylabel = {Profit/Loss/Payoff}, scale = 0.7,  xtick={ 145.50, 115.78,291,175.22}, ytick={0,145.50,-115.78,29.72,-145.5}]
\addplot [domain=0:145.5, samples=100, color=red, ]{x};
\addplot [domain=145.5:300, samples=100, color=red, ]{145.5};
\addplot [domain=0:1, samples=100, color=black, ]{177};
\addplot [domain=0:145.5, samples=100, color=blue, ]{x-115.78};
\addplot [domain=145.5:300, samples=100, color=blue, ]{29.72};
\addplot [domain=0:300, samples=100, color=green, ]{x-145.5};
\addplot [dashed, gray!40] coordinates {(0,145.5) (145,145.5)};
\addplot [dashed, gray!40] coordinates {(175.22,0) (175.22,29.72)};
\addplot [dashed, gray!40] coordinates {(0,29.72) (145,29.72)};
\addplot [dashed, gray!40] coordinates {(145.5,145) (145.5,0)};
\addplot [dashed, gray!40] coordinates {(291,145) (291,0)};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

答案1

在 a) 中,我假设您指的是刻度标签。可以使用以下方法实现该修改

   xticklabel={\euro\pgfmathprintnumber\tick},
   yticklabel={\euro\pgfmathprintnumber\tick},

为了改变刻度标签的精度,我添加了

   ticklabel style={/pgf/number format/.cd,
                    fixed,precision=2,fixed zerofill,
                    /pgfplots/.cd},
   xticklabel style={font=\scriptsize}

/pgf/number format/.cd表示在组中搜索以下键/pgf/number formatcd代表更改目录)。同样,/pgfplots/.cd切换回pgfplots“命名空间”。我减小了 xticklabels 的字体大小,因为它们在添加欧元符号后有点重叠。另一种可能性可能是xticklabel style={rotate=30},稍微旋转一下刻度标签。

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{marvosym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{fillbetween}
\usepackage[gen]{eurosym}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[
   width=0.98\textwidth,
   height=\textwidth,
   axis lines=center,
   xlabel = {Expiration Value},
   ylabel = {Profit/Loss/Payoff},
%   scale = 0.7,
   xtick={ 145.50, 115.78,291,175.22},
   ytick={0,145.50,-115.78,29.72,-145.5},
   xticklabel={\euro\pgfmathprintnumber\tick},
   yticklabel={\euro\pgfmathprintnumber\tick},
   ticklabel style={/pgf/number format/.cd,
                    fixed,precision=2,fixed zerofill,
                    /pgfplots/.cd},
  xticklabel style={font=\scriptsize},
  samples=2,
]

\addplot [domain=0:145.5, color=red, ]{x};
\addplot [domain=145.5:300, color=red, ]{145.5};
\addplot [domain=0:1,  color=black, ]{177};
\addplot [domain=0:145.5, color=blue, ]{x-115.78};
\addplot [domain=145.5:300, color=blue, ]{29.72};
\addplot [domain=0:300, color=green, ]{x-145.5};
\addplot [dashed, gray!40] coordinates {(0,145.5) (145,145.5)};
\addplot [dashed, gray!40] coordinates {(175.22,0) (175.22,29.72)};
\addplot [dashed, gray!40] coordinates {(0,29.72) (145,29.72)};
\addplot [dashed, gray!40] coordinates {(145.5,145) (145.5,0)};
\addplot [dashed, gray!40] coordinates {(291,145) (291,0)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容