如何匯入科學格式的表格

如何匯入科學格式的表格

這是我不工作的 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樣式聲明,因為您的所有輸入資料都是數字。

  • 您需要\pgfplotstableread使用適當的選項 ( col sep, ignore chars) 來呼叫來解析 CSV 數據可能用於\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}

在此輸入影像描述

相關內容