我创建了一个堆叠面积图,我想添加网格线。但是,当我这样做时,网格线位于堆叠面积图的顶部。我怎样才能反过来做(这样图就位于网格线的顶部)?
这是一个最小的工作示例(我知道这不是最有效的代码,但我还需要学习很多东西):
\documentclass[trans]{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgfcalendar}
\usetikzlibrary{dateplot}
\usetikzlibrary{patterns}
\usepackage{eurosym}
\usetheme{EastLansing}
\setbeamercovered{invisible}
\usefonttheme{serif}
\setbeamercolor{block title example}{bg=pink,fg=blue}
\setbeamercolor{block body example}{bg=white,fg=magenta}
\setbeamertemplate{blocks}[rounded][shadow=true]
\begin{document}
\begin{frame}
\frametitle{Liabilities of the Eurosystem}
\begin{center}
\pgfplotsset{scaled y ticks=false}
\begin{tikzpicture}[scale=0.9]
\begin{axis}[
every tick label/.append style={
font=\scriptsize},
every major grid/.append style={
dashed},
width=12cm,
height=6cm,
scale only axis,
legend style={
at={(0.05,0.95)},
anchor={north west},
line width=0.4pt},
stack plots=y,
area style,
ymajorgrids=true,
date coordinates in=x,
date ZERO=2006-01-04,
xmin=2006-01-04,
xmax=2024-02-28,
ylabel={\scriptsize \EUR billion},
ymin=0,
xticklabels={2006,2008,2010,2012,2014,2016,2018,2020,2022,2024},
xtick={2006-01-04,2008-01-02,2010-01-06,2012-01-04,2014-01-01,2016-01-06,2018-01-03,2020-01-01,2022-01-05,2024-01-03},
ymax=10000,
ytick={2000,4000,6000,8000,10000},
xticklabel={\year}
]
\addplot [draw=cyan,fill=cyan] table[x=Date,y=OL] {ES_liabilities.txt} \closedcycle;
\addplot [draw=teal,fill=teal] table[x=Date,y=BC] {ES_liabilities.txt} \closedcycle;
\addplot [draw=orange,fill=orange] table[x=Date,y=CA] {ES_liabilities.txt} \closedcycle;
\addplot [draw=purple,fill=purple] table[x=Date,y=DF] {ES_liabilities.txt} \closedcycle;
\addplot [stack plots=false,line legend,draw=black,line width=3pt] table[x=Date,y=TL] {ES_liabilities.txt};
\legend{{\tiny Other liabilities}, {\tiny Banknotes in circulation}, {\tiny Current accounts}, {\tiny Deposit facility},
{\tiny Total liabilities}};
\end{axis}
\end{tikzpicture}
\end{center}
{\scriptsize \textit{Source: ECB.}}
\transwipe
\end{frame}
\end{document}
还有一件事。总负债是一条线,但在图例中它很粗,我无法让它变细。有办法吗?
答案1
area style
设置axis on top
。- 您可以通过将其设置为 来撤消该操作false
。或者您可以根据需要使用area cycle list
和。area legend
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every tick label/.append style={font=\scriptsize},
every major grid/.append style={dashed},
width=12cm, height=6cm, scale only axis,
stack plots=y,
area style, axis on top=false,
ymajorgrids=true,
date coordinates in=x,
xmin=2006-01-04, xmax=2024-02-28,
ymin=0, ymax=10000,
xtick={2006-01-04,2008-01-02,2010-01-06,2012-01-04,2014-01-01,2016-01-06,2018-01-03,2020-01-01,2022-01-05,2024-01-03},
xticklabels={2006,2008,2010,2012,2014,2016,2018,2020,2022,2024},
ytick={2000,4000,6000,8000,10000},
legend style={font=\tiny},
]
\addplot[cyan, fill=cyan] coordinates {(2006-01-04,1) (2024-01-04,3000)} \closedcycle;
\addplot[teal, fill=teal] coordinates {(2006-01-04,1) (2024-01-04,2000)} \closedcycle;
\addplot[orange, fill=orange] coordinates {(2006-01-04,1) (2024-01-04,2000)} \closedcycle;
\addplot[purple, fill=purple] coordinates {(2006-01-04,1) (2024-01-04,2000)} \closedcycle;
\legend{{Other liabilities}, {Banknotes in circulation}, {Current accounts}, {Deposit facility}, {Total liabilities}};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
对于此演示我:
- 使用类
standalone
,这在开发过程中通常更好 - 模仿你的堆叠数据
- 选择一些选项和命令
- 忽略了所有其他选项,例如与日期等相关的选项
- 显示简化数据
这里可能不需要很多软件包。例如,pgfplotstable
当您想要将数据文件(=表格)转换为 Latex 中有用的表格时,Eg 很有用;但对于绘制数据,您不需要它。
从你的问题中看不清楚你是如何尝试网格的。无论如何,所展示的显然是正确的。
建议:
- 从这个简单的演示开始(即将代码复制到新文件中)
- 逐步过渡到你所展示的内容,例如
- 从 1,2,3 移动到您的日期序列(即修改我的表,添加包直到它编译)
- 用你的数据文件替换我的表格
- 添加更多数据文件
- ETC。
这种方法很像在陡峭的地形或顶层建筑物上徒步旅行:安全第一,从安全的位置到安全的位置(即在接近目标时从编译到编译)。
\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
area style,
grid=major,
grid style={
color=red, dashed},
title=Demonstrating relevant commands,
]
\addplot table{
1 10
2 100
3 45
} \closedcycle;
\addplot table{
1 27
2 35
3 50
} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}