请考虑以下 MWE。相同的代码位于另一个具有较大前言的文档中,因此我遇到的未对齐问题可能xticklabels
是由于冲突造成的packages
。
第一个屏幕截图显示了 MWE 的结果,第二个屏幕截图显示了使用更大的前导码进行编译时的结果。
笔记:对于 MWE,我没有包括特殊格式(例如颜色、破折号等)。
编辑顺便说一句:如果你看一下数据,dataLevelerConverter2.dat
你会看到有一个 Kalenderwoche 42。但是,在第一个屏幕截图中,似乎是xticks
从 Kalenderwoche 43 开始的。
平均能量损失
\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{filecontents}{dataLevelerConverter2.dat}
Wert Kalenderwochen Nachfrage Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1 42 nan nan nan nan nan 128 608
2 43 255 255 nan nan nan 1088 608
3 44 1584 1584 1584 nan nan 960 1265
4 45 1296 nan 1296 1296 nan 1570 1265
5 46 432 nan nan 432 nan 610 610
6 47 972 nan nan 972 nan 610 610
7 48 540 nan nan 540 nan 610 610
8 49 1104 nan nan 1104 1104 610 610
9 50 0 nan nan nan 0 0 0
\end{filecontents}
%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
use comma,%additional code
1000 sep={\,},%additional code
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=6cm,
xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen},
xtick from table = {dataLevelerConverter2.dat}{Wert},
% xtick = {1,...,9},
% xtick = data,
xlabel=Kalenderwoche,
ylabel=Kundennachfrage,
legend style={at={(0.5,-0.3)},anchor=north,draw=none},
legend columns=3
]
\addplot table [x=Wert, y=Nachfrage] {dataLevelerConverter2.dat} ;
\addlegendentry{label1}
\addplot table [x=Wert, y=nachLeveler] {dataLevelerConverter2.dat} ;
\addlegendentry{label2}
\addplot table [x=Wert, y=nachConverter] {dataLevelerConverter2.dat} ;
\addlegendentry{label3}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在这个具体示例中,Luigi 建议Kalenderwochen
直接使用 x 坐标,这是一个好主意。但是,如果Kalenderwochen
确实包含标签(数字或其他),则它将不起作用:在 John 的文本标签示例中,xtick={0,...,9}
必须明确设置。通常,只需使用,但这仅在第一个图中xtick=data
没有 s 时才有效。nan
因此,在极少数情况下,您需要从表中提供标签,但无法使用来xtick=data
确保刻度和刻度标签同步,这里有一种xtick from table
类似于的样式xticklabels from data
:
这种方法的缺点是,您总是使用所有刻度和刻度标签,而不是让 PGFPlots 确定适当的刻度数。要解决这个问题,您可以设置
xticklabel={%
\pgfmathtruncatemacro\datarow{round(\tick)}%
\pgfplotstablegetelem{\datarow}{Kalenderwochen}\of{dataLevelerConverter2.dat}%
\pgfplotsretval%
}
这将从数据表中获取每个刻度位置的适当刻度标签。
第一个例子的代码
\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
xticks from table/.code 2 args={%
\pgfplotstablegetrowsof{#1}
\pgfmathsetmacro\pgfplotstabletotalrows{\pgfplotsretval-1}
\edef\ticklist{}
\pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{%
\edef\ticklist{\ticklist\cell\ifnum\pgfplotstablerow<\pgfplotstabletotalrows,\fi}
}
\pgfplotsset{xtick=\ticklist}
},
}
\begin{filecontents}{dataLevelerConverter2.dat}
Wert Kalenderwochen Nachfrage Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1 42 nan nan nan nan nan 128 608
2 43 255 255 nan nan nan 1088 608
3 44 1584 1584 1584 nan nan 960 1265
4 45 1296 nan 1296 1296 nan 1570 1265
5 46 432 nan nan 432 nan 610 610
6 47 972 nan nan 972 nan 610 610
7 48 540 nan nan 540 nan 610 610
8 49 1104 nan nan 1104 1104 610 610
9 50 0 nan nan nan 0 0 0
10 51 255 255 nan nan nan 1088 608
11 52 1584 1584 1584 nan nan 960 1265
12 1 1296 nan 1296 1296 nan 1570 1265
\end{filecontents}
%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
use comma,%additional code
1000 sep={\,},%additional code
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=6cm,
xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen},
xticks from table = {dataLevelerConverter2.dat}{Wert},
xlabel=Kalenderwoche,
ylabel=Kundennachfrage,
legend style={at={(0.5,-0.3)},anchor=north,draw=none},
legend columns=3
]
\addplot table [x=Wert, y=Nachfrage] {dataLevelerConverter2.dat} ;
\addlegendentry{label1}
\addplot table [x=Wert, y=nachLeveler] {dataLevelerConverter2.dat} ;
\addlegendentry{label2}
\addplot table [x=Wert, y=nachConverter] {dataLevelerConverter2.dat} ;
\addlegendentry{label3}
\end{axis}
\end{tikzpicture}
\end{document}
第二个示例的代码:
\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
xticks from table/.code 2 args={%
\pgfplotstablegetrowsof{#1}
\pgfmathsetmacro\pgfplotstabletotalrows{\pgfplotsretval-1}
\edef\ticklist{}
\pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{%
\edef\ticklist{\ticklist\cell\ifnum\pgfplotstablerow<\pgfplotstabletotalrows,\fi}
}
\pgfplotsset{xtick=\ticklist}
},
}
\begin{filecontents}{dataLevelerConverter2.dat}
Wert Kalenderwochen Nachfrage Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1 42 nan nan nan nan nan 128 608
2 43 255 255 nan nan nan 1088 608
3 44 1584 1584 1584 nan nan 960 1265
4 45 1296 nan 1296 1296 nan 1570 1265
5 46 432 nan nan 432 nan 610 610
6 47 972 nan nan 972 nan 610 610
7 48 540 nan nan 540 nan 610 610
8 49 1104 nan nan 1104 1104 610 610
9 50 0 nan nan nan 0 0 0
10 51 255 255 nan nan nan 1088 608
11 52 1584 1584 1584 nan nan 960 1265
12 1 1296 nan 1296 1296 nan 1570 1265
\end{filecontents}
%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
use comma,%additional code
1000 sep={\,},%additional code
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=6cm,
height=6cm,
enlargelimits=false,
xticklabel={%
\pgfmathtruncatemacro\datarow{round(\tick)}%
\pgfplotstablegetelem{\datarow}{Kalenderwochen}\of{dataLevelerConverter2.dat}%
\pgfplotsretval%
},
xlabel=Kalenderwoche,
ylabel=Kundennachfrage,
legend style={at={(0.5,-0.3)},anchor=north,draw=none},
legend columns=3
]
\addplot table [x expr=\coordindex, y=Nachfrage] {dataLevelerConverter2.dat} ;
\addlegendentry{label1}
\addplot table [x expr=\coordindex, y=nachLeveler] {dataLevelerConverter2.dat} ;
\addlegendentry{label2}
\addplot table [x expr=\coordindex, y=nachConverter] {dataLevelerConverter2.dat} ;
\addlegendentry{label3}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
@Luigi 和 @Jake 是对的。
xticklabels 是数字
我想使用Kalenderwochen
中的dataLevelerConverter2.dat
列xticklabels
。由于此列包含数字,因此我可以写
\addplot table [x=Kalenderwochen, y=Nachfrage]
代替
\addplot table [x=Wert, y=Nachfrage]
并删除
xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen}
xticklabels 不是数字
如果列Kalenderwochen
包含除数字以外的其他信息(例如“第一个数据集”,“第二个数据集”),则必须写入
\addplot table [x=Wert, y=Nachfrage]
xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen}
平均能量损失
\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{filecontents}{Numbers.dat}
Wert Kalenderwochen Nachfrage Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1 42 nan nan nan nan nan 128 608
2 43 255 255 nan nan nan 1088 608
3 44 1584 1584 1584 nan nan 960 1265
4 45 1296 nan 1296 1296 nan 1570 1265
5 46 432 nan nan 432 nan 610 610
6 47 972 nan nan 972 nan 610 610
7 48 540 nan nan 540 nan 610 610
8 49 1104 nan nan 1104 1104 610 610
9 50 0 nan nan nan 0 0 0
\end{filecontents}
\begin{filecontents}{Strings.dat}
Wert Kalenderwochen Nachfrage Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1 first nan nan nan nan nan 128 608
2 second 255 255 nan nan nan 1088 608
3 third 1584 1584 1584 nan nan 960 1265
4 fourth 1296 nan 1296 1296 nan 1570 1265
5 fifth 432 nan nan 432 nan 610 610
6 sixth 972 nan nan 972 nan 610 610
7 seventh 540 nan nan 540 nan 610 610
8 eighth 1104 nan nan 1104 1104 610 610
9 ninth 0 nan nan nan 0 0 0
\end{filecontents}
%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
use comma,%additional code
1000 sep={\,},%additional code
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=6cm,
xlabel=Kalenderwoche,
ylabel=Kundennachfrage,
legend style={at={(0.5,-0.3)},anchor=north,draw=none},
legend columns=3
]
\addplot table [x=Kalenderwochen, y=Nachfrage] {Numbers.dat} ;
\addlegendentry{label1}
\addplot table [x=Kalenderwochen, y=nachLeveler] {Numbers.dat} ;
\addlegendentry{label2}
\addplot table [x=Kalenderwochen, y=nachConverter] {Numbers.dat} ;
\addlegendentry{label3}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=6cm,
xticklabels from table = {Strings.dat}{Kalenderwochen},
xtick = {1,...,9},
xticklabel style={rotate=90},
xlabel=Kalenderwoche,
xlabel style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
ylabel=Kundennachfrage,
legend style={at={(0.5,-0.3)},anchor=north,draw=none},
legend columns=3
]
\addplot table [x=Wert, y=Nachfrage] {Strings.dat} ;
\addlegendentry{label1}
\addplot table [x=Wert, y=nachLeveler] {Strings.dat} ;
\addlegendentry{label2}
\addplot table [x=Wert, y=nachConverter] {Strings.dat} ;
\addlegendentry{label3}
\end{axis}
\end{tikzpicture}
\end{document}