科学的書式の表をインポートする方法

科学的書式の表をインポートする方法

動作しない MWE を以下に示します。科学的な形式でテーブルをインポートしてプロットするにはどうすればよいでしょうか?

\documentclass[border=0mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{filecontents}{data-export-scientific.csv}
"x";"y1";"y2";"y3"
"   1";"8.649e+01";"3.501e+01";"1.013e+01"
"   2";"8.597e+01";"3.672e+01";"6.306e+00"
"   3";"8.667e+01";"4.348e+01";"9.170e+00"
"   4";"8.287e+01";"4.270e+01";"1.052e+01"
"   5";"8.747e+01";"4.081e+01";"1.118e+01"
\end{filecontents}

\begin{document}
\begin{tikzpicture}

\pgfplotstabletypeset[columns/x/.style={string type},
                      columns/y1/.style={string type},
                      columns/y2/.style={string type},
                      columns/y3/.style={string type}]{data-export-scientific.csv}

\pgfplotstableread[col sep=semicolon]{data-export-scientific.csv}\myLoadedTable

\begin{axis}
\addplot[color=blue, only marks]table[x=x, y=y1]{\myLoadedTable};   
\end{axis}

\end{tikzpicture}
\end{document}

答え1

  • 著者によればpgfplotstable入力データ内の二重引用符を処理するための推奨される方法は、 で宣言することですignore chars={"}

  • string type入力データはすべて数値なので、スタイル宣言を削除しました。

  • CSVデータを解析するには、\pgfplotstableread適切なオプション(col sep、 )を指定して呼び出す必要があります。ignore chars前に\pgfplotstabletypesetおそらく、生のデータを複数回解析しない限り、ドキュメント内のテーブルをタイプセットするために使用するのでしょうが、これはあまり意味がないと私は思います。

  • 次のスタイルを適用しました。

    my numeric col/.style={
      sci, sci zerofill, sci sep align, precision=2, sci 10e
    }
    

    タイプセットテーブルの最初の列を除くすべての列に適用されます。これは次のように行います。

    every column/.code={
      \ifnum\pgfplotstablecol>0\relax
        \pgfkeysalso{my numeric col}
      \fi
    }
    
  • booktabsパッケージと次のスタイル情報を使用して、テーブルの適切なフォーマットを実装しました。

    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule}
    
  • pgfplots念のため、次の行を使用して互換性レベルを 1.16 に 上げました。
    \pgfplotsset{compat=1.16}
    
\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}

\begin{filecontents}{data-export-scientific.csv}
"x";"y1";"y2";"y3"
"   1";"8.649e+01";"3.501e+01";"1.013e+01"
"   2";"8.597e+01";"3.672e+01";"6.306e+00"
"   3";"8.667e+01";"4.348e+01";"9.170e+00"
"   4";"8.287e+01";"4.270e+01";"1.052e+01"
"   5";"8.747e+01";"4.081e+01";"1.118e+01"
\end{filecontents}

\pgfplotstableread[col sep=semicolon, ignore chars={"}]
  {data-export-scientific.csv}\myLoadedTable

\begin{document}

\begin{table}
  \centering
  \pgfplotstabletypeset[
    my numeric col/.style={
      sci, sci zerofill, sci sep align, precision=2, sci 10e
    },
    every column/.code={
      \ifnum\pgfplotstablecol>0\relax
        \pgfkeysalso{my numeric col}
      \fi
    },
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule}
    ]{\myLoadedTable}
  \caption{My table data}
\end{table}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}
    \addplot[color=blue, only marks] table[x=x, y=y1] {\myLoadedTable};
    \end{axis}
  \end{tikzpicture}
  \caption{My plot}
\end{figure}

\end{document}

ここに画像の説明を入力してください

ここに画像の説明を入力してください

改良として、 の値内の列番号に基づいて適切な下付き文字を持つ LaTeX 数式を使用して、テーブル ヘッダーを改善することもできます/pgfplots/table/column name

\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}

\begin{filecontents}{data-export-scientific.csv}
"x";"y1";"y2";"y3"
"   1";"8.649e+01";"3.501e+01";"1.013e+01"
"   2";"8.597e+01";"3.672e+01";"6.306e+00"
"   3";"8.667e+01";"4.348e+01";"9.170e+00"
"   4";"8.287e+01";"4.270e+01";"1.052e+01"
"   5";"8.747e+01";"4.081e+01";"1.118e+01"
\end{filecontents}

\pgfplotstableread[col sep=semicolon, ignore chars={"}]
  {data-export-scientific.csv}\myLoadedTable

\begin{document}

\begin{table}
  \centering
  \pgfplotstabletypeset[
    my numeric col/.style={
      sci, sci zerofill, sci sep align, precision=2, sci 10e,
      column name={$y_{#1}$}
    },
    every column/.code={
      \ifnum\pgfplotstablecol>0\relax
        \pgfkeysalso{my numeric col/.expanded={\pgfplotstablecol}}
      \fi
    },
    columns/x/.style={column name={$x$}},
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule}
    ]{\myLoadedTable}
  \caption{My table data}
\end{table}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}
    \addplot[color=blue, only marks] table[x=x, y=y1] {\myLoadedTable};
    \end{axis}
  \end{tikzpicture}
  \caption{My plot}
\end{figure}

\end{document}

ここに画像の説明を入力してください

関連情報