通过 CSV 百分比数字创建图表

通过 CSV 百分比数字创建图表

我正在尝试用 LaTeX 绘制此图:

在此处输入图片描述

这是我的尝试:

\documentclass[12pt,a4paper]{article}

\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.17}
\usepackage{csvsimple}
\pgfplotstableread[col sep=comma]{res/operating-profit-margin-data.csv}{\operatingprofitmargindata}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot table[x index = {1}, y index = {1}]{\operatingprofitmargindata};
  \end{axis}
\end{tikzpicture}

\end{document}

csv文件中的数据如下:

,2018,2019,2020,2021,2022
Industry benchmark,28.83,14.11,20.57,25.25,20.55
Bioventix PLC,78.08,74.61,79.35,73.99,79.13
NIOX Group PLC,-83.64,-2.16,-72.38,-15.41,5.75
Oxford Biomedica PLC,20.84,-22.58,-6.49,14.54,-21.59

不确定如何使我的图表看起来符合预期。

答案1

数据转置后事情就变得简单了:

\documentclass[border=10pt]{standalone}

\begin{filecontents}{operating-profit-margin-data.csv}
Year,2018,2019,2020,2021,2022
Industry benchmark,28.83,14.11,20.57,25.25,20.55
Bioventix PLC,78.08,74.61,79.35,73.99,79.13
NIOX Group PLC,-83.64,-2.16,-72.38,-15.41,5.75
Oxford Biomedica PLC,20.84,-22.58,-6.49,14.54,-21.59
\end{filecontents}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\pgfplotstableread[col sep=comma]{operating-profit-margin-data.csv}
    {\operatingprofitmargindata}

\pgfplotstabletranspose[string type,
    colnames from=Year,
    input colnames to=Year
]\operatingprofitmargindatatransposed{\operatingprofitmargindata}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xticklabel style={
        /pgf/number format/1000 sep={}
    },
    yticklabel style={
        /pgf/number format/fixed zerofill,
        /pgf/number format/precision={2}
    },
    yticklabel={\pgfmathprintnumber{\tick}\,$\%$},
    ymin=-100,
    ymax=100
  ]
    \addplot table[y={Industry benchmark}] {\operatingprofitmargindatatransposed};
    \addplot table[y={Bioventix PLC}] {\operatingprofitmargindatatransposed};
    \addplot table[y={NIOX Group PLC}] {\operatingprofitmargindatatransposed};
    \addplot table[y={Oxford Biomedica PLC}] {\operatingprofitmargindatatransposed};
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

尝试这个:

输出:
在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={Operating Profit Margin},
    xlabel={},
    ylabel={},
    xmin=2017.5, xmax=2022.5,
    ymin=-100, ymax=100,
    xtick={2018,2019,2020,2021,2022},
    xticklabel style={/pgf/number format/1000 sep=},
    ytick={-100,-80,...,100},
    yticklabel={$\pgfmathprintnumber{\tick}\%$},
    ymajorgrids=true,
    grid style={solid},
    axis line style={draw=none},
    tick style={draw=none}, % This will remove the tick marks
    legend style={at={(0.5,-0.15)},anchor=north,legend columns=2,draw=none}
]

\addplot[
    color=green,
    mark=*,
    ]
    coordinates {
    (2018,28.83)(2019,14.11)(2020,20.57)(2021,25.25)(2022,20.55)
    };
    \addlegendentry{Industry benchmark}

\addplot[
    color=orange,
    mark=*,
    ]
    coordinates {
    (2018,78.08)(2019,74.61)(2020,79.35)(2021,73.99)(2022,79.13)
    };
    \addlegendentry{Bioventix PLC}

\addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
    (2018,-83.64)(2019,-2.16)(2020,-72.38)(2021,-15.41)(2022,5.75)
    };
    \addlegendentry{NIOX Group PLC}

\addplot[
    color=purple,
    mark=*,
    ]
    coordinates {
    (2018,20.84)(2019,-22.58)(2020,-6.49)(2021,14.54)(2022,-21.59)
    };
    \addlegendentry{Oxford Biomedica PLC}

\end{axis}
\end{tikzpicture}
\end{document}

相关内容