从 csv 导入准备好的箱线图的值吗?

从 csv 导入准备好的箱线图的值吗?

我参考了这里发布的问题:从表中读取箱线图准备好的值。但是,它不能很好地处理外部.csv文件。

我有以下Table.csv内容:

Product,Min,Lower,Median,Upper,Max
Blue,12.5,16,25,32,35
Green,28,32.2,35.5,37,45
Yellow,6.6,15,25.5,30.2,70
Magenta,10,12.2,14.4,16.6,19.1

我尝试了以下代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepackage{pgfplotstable}

\begin{document}


\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y,
ytick={1,2,3,4},
yticklabels={Blue,Green,Yellow,Magenta}]
%Blue
\addplot+[
  boxplot prepared from table={
    table=Table.csv,
    row=1,
    lower whisker=Min,
    lower quartile= Lower,
    median= Median,
    upper quartile= Upper,
    upper whisker= Max,
  }, boxplot prepared
  ]
  coordinates {};
%Green
\addplot+[
 boxplot prepared from table={
    table=Table.csv,
    row=2,
    lower whisker=Min,
    lower quartile= Lower,
    median= Median,
    upper quartile= Upper,
    upper whisker= Max,
  }, boxplot prepared
  ]
  coordinates {};

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

我收到以下错误:

Package pgfkeys Error: I do not know the key '/tikz/boxplot prepared from tab
le', to which you passed 'table=Table.csv, row=1, lower whisker=Min, lower quar
tile= Lower, median= Median, upper quartile= Upper, upper whisker= Max,', and I
am going to ignore it. Perhaps you misspelled it.

答案1

好吧,如果你仔细看看你提到的帖子,你会发现它为boxplot prepared from table默认情况下未定义的选项提供了一个定义。因此,你也需要将此定义复制到你的代码中,否则 PGF 会报错。

另外,您不能直接从 CSV 读取内容,但您需要通过 进行解析\pgfplotstableread。老实说,我不确定为什么会这样。

除此之外,你可以稍微整理一下你的序言,因为pgfplotstable已经加载了pgfplots。我认为,你还需要调整轴,这个我留给你了。

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}

% credit to https://tex.stackexchange.com/a/117439/47927
\makeatletter
\pgfplotsset{
    boxplot prepared from table/.code={
        \def\tikz@plot@handler{\pgfplotsplothandlerboxplotprepared}%
        \pgfplotsset{
            /pgfplots/boxplot prepared from table/.cd,
            #1,
        }
    },
    /pgfplots/boxplot prepared from table/.cd,
    table/.code={\pgfplotstablecopy{#1}\to\boxplot@datatable},
    row/.initial=0,
    make style readable from table/.style={
        #1/.code={
            \pgfplotstablegetelem{\pgfkeysvalueof{/pgfplots/boxplot prepared from table/row}}{##1}\of\boxplot@datatable
            \pgfplotsset{boxplot/#1/.expand once={\pgfplotsretval}}
        }
    },
    make style readable from table=lower whisker,
    make style readable from table=upper whisker,
    make style readable from table=lower quartile,
    make style readable from table=upper quartile,
    make style readable from table=median,
    make style readable from table=lower notch,
    make style readable from table=upper notch
}
\makeatother
% ===

\begin{filecontents}[noheader]{Table.csv}
Product,Min,Lower,Median,Upper,Max
Blue,12.5,16,25,32,35
Green,28,32.2,35.5,37,45
Yellow,6.6,15,25.5,30.2,70
Magenta,10,12.2,14.4,16.6,19.1
\end{filecontents}

\pgfplotstableread[col sep=comma]{Table.csv}\mydata

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    boxplot/draw direction=y,
    ytick={1,2,3,4},
    yticklabels={Blue,Green,Yellow,Magenta}
]
%Blue
\addplot+[
    boxplot prepared from table={
        table=\mydata,
        row=1,
        lower whisker=Min,
        lower quartile= Lower,
        median= Median,
        upper quartile= Upper,
        upper whisker= Max,
    }, boxplot prepared
]
coordinates {};
%Green
\addplot+[
    boxplot prepared from table={
        table=\mydata,
        row=2,
        lower whisker=Min,
        lower quartile= Lower,
        median= Median,
        upper quartile= Upper,
        upper whisker= Max,
    }, boxplot prepared
]
coordinates {};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容