我想使用 MWE 中包含的表格数据创建线图。问题是,使用此数据,我希望绘图看起来像下图:
即,一个图具有不同的颜色区域。
如果这不起作用,那么制作情节的替代选择如下图所示:
但在本例中,0 是变化的颜色线,而不是中位数。关键是在一个图中将正值设为红色,将负值设为蓝色。但同时添加颜色条来指示异常(而不是图例)。
以下是我对该图的 MWE:
\documentclass[]{article}
\usepackage{filecontents,pgfplots}
\begin{filecontents}{data.dat}
year DJF JFM FMA MAM AMJ MJJ JJA JAS ASO SON OND NDJ
1950 -1.4 -1.2 -1.1 -1.2 -1.1 -0.9 -0.6 -0.6 -0.5 -0.6 -0.7 -0.8
1951 -0.8 -0.6 -0.2 0.2 0.2 0.4 0.5 0.7 0.8 0.9 0.7 0.6
1952 0.5 0.4 0.4 0.4 0.4 0.2 0 0.1 0.2 0.2 0.2 0.3
1953 0.5 0.6 0.7 0.7 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.7
1954 0.7 0.4 0 -0.4 -0.5 -0.5 -0.5 -0.7 -0.7 -0.6 -0.5 -0.5
1955 -0.6 -0.6 -0.7 -0.7 -0.7 -0.6 -0.6 -0.6 -1.0 -1.4 -1.6 -1.4
1956 -0.9 -0.6 -0.6 -0.5 -0.5 -0.4 -0.5 -0.5 -0.4 -0.4 -0.5 -0.4
1957 -0.3 0 0.3 0.6 0.7 0.9 1.0 1.2 1.1 1.2 1.3 1.6
1958 1.7 1.5 1.2 0.8 0.7 0.6 0.5 0.4 0.4 0.5 0.6 0.6
1959 0.6 0.5 0.4 0.2 0.1 -0.2 -0.3 -0.3 -0.1 -0.1 -0.1 -0.1
1960 -0.1 -0.2 -0.1 0 -0.1 -0.2 0 0.1 0.2 0.1 0 0
1961 0 0 -0.1 0 0.1 0.2 0.1 -0.1 -0.3 -0.3 -0.2 -0.2
1962 -0.2 -0.2 -0.2 -0.3 -0.3 -0.2 -0.1 -0.2 -0.2 -0.3 -0.3 -0.4
1963 -0.4 -0.2 0.1 0.2 0.2 0.4 0.7 1.0 1.1 1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0.7 -0.6 -0.5 -0.6 -0.7 -0.8 -0.8
2001 -0.7 -0.6 -0.5 -0.3 -0.2 -0.1 0 -0.1 -0.1 -0.2 -0.3 -0.3
2002 -0.2 -0.1 0.1 0.2 0.4 0.7 0.8 0.9 1.0 1.2 1.3 1.1
2003 0.9 0.6 0.4 0 -0.2 -0.1 0.1 0.2 0.3 0.4 0.4 0.4
2004 0.3 0.2 0.1 0.1 0.2 0.3 0.5 0.7 0.7 0.7 0.7 0.7
2005 0.6 0.6 0.5 0.5 0.4 0.2 0.1 0 0 -0.1 -0.4 -0.7
2006 -0.7 -0.6 -0.4 -0.2 0.0 0.1 0.2 0.3 0.5 0.8 0.9 1.0
2007 0.7 0.3 0 -0.1 -0.2 -0.2 -0.3 -0.6 -0.8 -1.1 -1.2 -1.3
2008 -1.4 -1.3 -1.1 -0.9 -0.7 -0.5 -0.3 -0.2 -0.2 -0.3 -0.5 -0.7
2009 -0.8 -0.7 -0.4 -0.1 0.2 0.4 0.5 0.6 0.7 1.0 1.2 1.3
2010 1.3 1.1 0.8 0.5 0 -0.4 -0.8 -1.1 -1.3 -1.4 -1.3 -1.4
2011 -1.3 -1.1 -0.8 -0.6 -0.3 -0.2 -0.3 -0.5 -0.7 -0.9 -0.9 -0.8
2012 -0.7 -0.6 -0.5 -0.4 -0.3 -0.1 0.1 0.3 0.4 0.4 0.2 -0.2
2013 -0.4 -0.5 -0.3 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.3
2014 -0.5 -0.6 -0.4 -0.2 0 0 0 0 0.2 0.4 0.6 0.6
2015 0.5 0.4 0.5 0.7 0.9 1.0 1.2 1.5 1.8 2.1 2.2 2.3
\end{filecontents}
\begin{document}
\pgfplotstableread{data.dat}{\data}
\begin{tikzpicture}
\begin{axis}[minor tick num=1,
xlabel=Degrees]
\addplot [black] table [x={year}, y={DJF}] {\data};
\addplot [black] table [x={year}, y={JFM}] {\data};
\addplot [black] table [x={year}, y={FMA}] {\data};
\addplot [black] table [x={year}, y={MAM}] {\data};
\addplot [black] table [x={year}, y={AMJ}] {\data};
\addplot [black] table [x={year}, y={MJJ}] {\data};
\addplot [black] table [x={year}, y={JJA}] {\data};
\addplot [black] table [x={year}, y={JAS}] {\data};
\addplot [black] table [x={year}, y={ASO}] {\data};
\addplot [black] table [x={year}, y={SON}] {\data};
\addplot [black] table [x={year}, y={OND}] {\data};
\addplot [black] table [x={year}, y={NDJ}] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
获得上述任一数字对我来说都行得通(如果能同时获得两者就更好了)。谢谢。
答案1
不幸的是,您不能简单地将fillbetween
PGFPlots 库用于第一个示例,因为在您提供的数据中有一些值等于 0,这也是您想要将数据“拆分”为“上部”和“下部”的值,这两个部分应该分开处理。但这会使库混乱intersection
。为了避免这种情况,您要么必须移动水平线(数据应该拆分的位置),要么必须做一些“手动”工作才能获得所需的结果。
以下代码生成两个示例。第一种是更“自动化”的方法,它使用小技巧将水平线移到零以下一点以避免上述问题,但当然,当您仔细观察时,您会发现线条发生了移动。这里我展示了两者,即如何用不同的颜色绘制两个部分以及如何填充相应部分的区域。两者都结合使用了postaction
和特征。decoration
soft clip
第二种方法可以得到所需的解决方案,但需要更多“手动”工作。我使用库intersection segments
的功能fillbetween
分别手动设置绘图的“下”和“上”路径,与数据分割的水平线进行比较。
有关更多详细信息,请查看代码中的注释。
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.fillbetween,
}
\pgfplotsset{
compat=1.11,
%
% define a style which can be used for both plots
my axis style/.style={
xmin=1950,
xmax=2015,
enlarge x limits={abs=5},
ymin=-2.5,
ymax=2.5,
minor tick num=1,
ylabel=$\Delta T$ $/$ K,
% remove the `1000 sep'
xticklabel style={
/pgf/number format/1000 sep={},
},
line join=bevel,
},
}
% missing value for the year 1978
% added a zero in the last column (NDJ)
\begin{filecontents*}{data.dat}
year DJF JFM FMA MAM AMJ MJJ JJA JAS ASO SON OND NDJ
1950 -1.4 -1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4 0.2 0.1 0 0 -0.1 -0.4 -0.7
2006 -0.7 -0.6 -0.4 -0.2 0.0 0.1 0.2 0.3 0.5 0.8 0.9 1.0
2007 0.7 0.3 0 -0.1 -0.2 -0.2 -0.3 -0.6 -0.8 -1.1 -1.2 -1.3
2008 -1.4 -1.3 -1.1 -0.9 -0.7 -0.5 -0.3 -0.2 -0.2 -0.3 -0.5 -0.7
2009 -0.8 -0.7 -0.4 -0.1 0.2 0.4 0.5 0.6 0.7 1.0 1.2 1.3
2010 1.3 1.1 0.8 0.5 0 -0.4 -0.8 -1.1 -1.3 -1.4 -1.3 -1.4
2011 -1.3 -1.1 -0.8 -0.6 -0.3 -0.2 -0.3 -0.5 -0.7 -0.9 -0.9 -0.8
2012 -0.7 -0.6 -0.5 -0.4 -0.3 -0.1 0.1 0.3 0.4 0.4 0.2 -0.2
2013 -0.4 -0.5 -0.3 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.3
2014 -0.5 -0.6 -0.4 -0.2 0 0 0 0 0.2 0.4 0.6 0.6
2015 0.5 0.4 0.5 0.7 0.9 1.0 1.2 1.5 1.8 2.1 2.2 2.3
\end{filecontents*}
\begin{document}
\pgfplotstableread{data.dat}{\data}
% first, more automated approach
% which gives almost the desired result
\begin{tikzpicture}
\begin{axis}[
my axis style,
]
% define a y value where to clip
% (this is needed because at exactly 0 you will get an
% undesired result; give it a try to see what is happening)
\pgfmathsetmacro{\yclip}{-0.03}
% define a horizontal line where the values should be split
% into an upper and a lower part
\path [
draw=black,
name path=split path,
]
(\pgfkeysvalueof{/pgfplots/xmin},\yclip)
-- (\pgfkeysvalueof{/pgfplots/xmax},\yclip);
% draw the plot in blue
\addplot [
blue,
name path=curve,
% using `postaction' and `decorate' we draw the plot in red
% but clip it only to the "upper" part of using `soft clip'
postaction={
decorate,
red,
thin,
},
decoration={
soft clip,
soft clip path={
(\pgfkeysvalueof{/pgfplots/xmin},\yclip)
rectangle
(\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax})
},
},
] table [x={year},y={DJF}] {\data};
% with the clipped `curve' path we can now also fill the area
% (to do the same for the lower part you need to add another
% `\addplot' now clipping the "lower" part and then just add
% another `\addplot fill between')
\addplot [red!25] fill between [of=split path and curve];
\end{axis}
\end{tikzpicture}
% second, more manual approach
% giving the wanted solution
\begin{tikzpicture}
\begin{axis}[
my axis style,
]
% what column should be printed
\newcommand*\ColName{DJF}
% define a horizontal line where the values should be split
% into an upper and a lower part
\path [
% draw=black,
name path=origin,
]
(\pgfkeysvalueof{/pgfplots/xmin},0)
-- (\pgfkeysvalueof{/pgfplots/xmax},0);
% just plot one line
\addplot [
name path=curve,
] table [x={year},y=\ColName] {\data};
% compute + label the upper segment (but do not draw it):
\path [
name path=upper,
% draw=red,
% thick,
intersection segments={
of=origin and curve,
sequence=%
L{1} -- R{2} -- L{3} -- R{4} -- L{5}
-- L{6} -- R{7} -- L{8} -- R{9} -- L{10}
-- R{11} -- L{12} -- R{13} -- L{14} -- R{15}
-- R{16} -- L{17} -- R{18} -- L{19} -- R{20}
-- L{21} -- R{22} -- L{23} -- R{24} -- L{25}
-- R{26} -- L{27} -- R{28} -- L{29} -- R{30}
-- L{31} -- R{-1}
},
];
% compute + label the lower segment (but do not draw it):
\path [
name path=lower,
% draw=blue,
% thick,
intersection segments={
of=origin and curve,
sequence=%
R{1} -- L{2} -- R{3} -- L{4} -- R{5}
-- R{6} -- L{7} -- R{8} -- L{9} -- R{10}
-- L{11} -- R{12} -- L{13} -- R{14} -- L{15}
-- L{16} -- R{17} -- L{18} -- R{19} -- L{20}
-- R{21} -- L{22} -- R{23} -- L{24} -- R{25}
-- L{26} -- R{27} -- L{28} -- R{29} -- L{30}
-- R{31} -- L{-1}
},
];
% store the first and last value of the `\data' table
\pgfplotstablegetelem{0}{year}\of{\data}
\pgfmathsetmacro{\FirstX}{\pgfplotsretval}
\pgfplotstablegetrowsof{\data}
\pgfmathsetmacro{\LastX}{\pgfplotsretval-1}
\pgfplotstablegetelem{\LastX}{year}\of{\data}
\pgfmathsetmacro{\LastX}{\pgfplotsretval}
% now plot the filled areas between the "origin" path and the
% computed "upper" and "lower" parts
\addplot [red!25] fill between [
of=origin and upper,
% use another clip here to have a vertical start and end
% of the filled area
% (comment the next lines to see the difference)
soft clip={
domain=\FirstX:\LastX
},
];
\addplot [blue!25] fill between [
of=origin and lower,
soft clip={
domain=\FirstX:\LastX
},
];
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这更像是一个长评论而不是答案本身。
首先,您的 MWE 不起作用,因为缺少 1978 年的测量 NDJ,并且表格标题(第一列)显示为Year
(大写Y
),而在 axis 环境中您调用year
。如果您重新排列数据并在异常的正值和负值之间包含零,这将很容易做到。例如,在 1992 年您有0.2 0 -0.1
,这很好,而在 2002 年有-0.1 0.1
,您需要-0.1 0 0.1
特别小心地添加x
相应的坐标。我会在 Matlab 中执行此操作。
也许你可以用一些技巧来避免这种情况,pgfplotstable
但我不知道该怎么做。
将数据重新排列为零后,您可以使用选项y filter/.expression={y>=0 ? nan : y}
(unbounded coords=jump
前言\pgfplotsset{compat=1.14}
中要求)。这意味着,检查y
坐标,如果为负数,则用替换值nan
,否则绘制点。这些是命令的选项addplot
。