这是我的无法正常工作的 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}