我正在尝试执行以下操作:
- 从文件[CSV]读取数据
- 向其中添加计算行 [C]
- 转置数据
- 绘制数据
- 在表中显示数据
到目前为止一切正常,但是计算出的行(而且只有计算出的行)是以科学数字格式写入的,尽管没有理由这样做(至少我没有看到 pgfplotstable 应该这样做的明确理由……)。 我试图摆脱科学数字来获取浮点数或简单整数(因为没有小数位)。
我有以下最小工作示例:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preamble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use A4 paper
\documentclass[a4paper]{article}
% Use english language
\usepackage[english]{babel}
% Use UTF8 encoding
\usepackage[utf8]{inputenc}
% TikZ and PGF libraries for graphs
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
% Booktabs for layout of tables
\usepackage{booktabs}
% Enable library for date-plotting
\usetikzlibrary{pgfplots.dateplot}
% Enable library for statistics [boxplot]
\usepgfplotslibrary{statistics}
% Always use newest version
\pgfplotsset{compat=newest,compat/show suggested version=false}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pre - Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create data to test
\begin{filecontents}{testData.csv}
day, A, B
01, 10, 5
02, 20, 10
03, 100, 5
\end{filecontents}
% Create 'C' by calculating 'A - B'
\pgfplotstableset{
create on use/C/.style={
create col/expr={\thisrow{A}-\thisrow{B}},
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%% Setup
% Read data and save it to 'dataTable'
\pgfplotstableread[
col sep=comma,
]{testData.csv}\dataTable
% Create second, in memory, table, that transposes the table, so that on the left side are the labels, followed by corresponding numbers, ordered by the date.
% Save it to 'dataTransposed'
\pgfplotstabletranspose[
col sep=comma,
string type,
colnames from=day,
input colnames to=day,
columns={day, A, B, C}
]\dataTransposed{\dataTable}
%%% Visualizing data
% Plot data from 'dataTable'
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
xmin=01,
xmax=31,
ymin=0,
ymax=180,
xtick=data,
xticklabel style={
rotate=90,
anchor=near xticklabel,
},
transpose legend,
legend style={at={(0.5,-0.1)},anchor=north},
legend columns=-1,
]
\addplot table [col sep=comma,x=day,y=A] {\dataTable};
\addplot table [col sep=comma,x=day,y=B] {\dataTable};
\addplot table [col sep=comma,x=day,y=C] {\dataTable}; % the generated data
\legend{A, B, C}
\end{axis}
\end{tikzpicture}
% Create tables from 'dataTransposed'
\begin{center} % Center table
\pgfplotstabletypeset[
col sep=comma,
string type,
% columns={day, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10},
every head row/.style={
before row=\toprule,
after row=\midrule
},
every last row/.style={
after row=\bottomrule
}
]{\dataTransposed}
\end{center}
\end{document}
我们可以看到“C”行以科学数字显示。我该如何更改?
我已经尝试使用以下命令设置数字格式:
\pgfkeys{/pgf/number format/int trunc}
[在序言、文档中、表格之前、读取的数据之前等;也可以作为方括号内的参数]哪些没有做任何事情?
有趣的是
\pgfmathprintnumber{5}
即使不设置任何内容,也能以正确的数字格式打印数字。
提前致谢。
答案1
正如@JohnKormylo 指出的那样,用 替换string type
可以columns/day/.style={string type}
解决问题。
完整且可用的版本:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preamble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use A4 paper
\documentclass[a4paper]{article}
% Use english language
\usepackage[english]{babel}
% Use UTF8 encoding
\usepackage[utf8]{inputenc}
% TikZ and PGF libraries for graphs
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
% Booktabs for layout of tables
\usepackage{booktabs}
% Enable library for date-plotting
\usetikzlibrary{pgfplots.dateplot}
% Enable library for statistics [boxplot]
\usepgfplotslibrary{statistics}
% Always use newest version
\pgfplotsset{compat=newest,compat/show suggested version=false}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pre - Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create data to test
\begin{filecontents}{testData.csv}
day, A, B
01, 10, 5
02, 20, 10
03, 100, 5
\end{filecontents}
% Create 'C' by calculating 'A - B'
\pgfplotstableset{
create on use/C/.style={
create col/expr={\thisrow{A}-\thisrow{B}},
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%% Setup
% Read data and save it to 'dataTable'
\pgfplotstableread[
col sep=comma,
]{testData.csv}\dataTable
% Create second, in memory, table, that transposes the table, so that on the left side are the labels, followed by corresponding numbers, ordered by thedate.
% Save it to 'dataTransposed'
\pgfplotstabletranspose[
col sep=comma,
string type,
colnames from=day,
input colnames to=day,
columns={day, A, B, C}
]\dataTransposed{\dataTable}
%%% Visualizing data
% Plot data from 'dataTable'
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
xmin=01,
xmax=31,
ymin=0,
ymax=180,
xtick=data,
xticklabel style={
rotate=90,
anchor=near xticklabel,
},
transpose legend,
legend style={at={(0.5,-0.1)},anchor=north},
legend columns=-1,
]
\addplot table [col sep=comma,x=day,y=A] {\dataTable};
\addplot table [col sep=comma,x=day,y=B] {\dataTable};
\addplot table [col sep=comma,x=day,y=C] {\dataTable}; % the generated data
\legend{A, B, C}
\end{axis}
\end{tikzpicture}
% Create tables from 'dataTransposed'
\begin{center} % Center table
\pgfplotstabletypeset[
col sep=comma,
columns/day/.style={string type}, % <--- HERE IS THE CHANGE
% columns={day, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10},
every head row/.style={
before row=\toprule,
after row=\midrule
},
every last row/.style={
after row=\bottomrule
}
]{\dataTransposed}
\end{center}
\end{document}
谢谢您的帮助!