我正在制作一个包含三个子图的图形。然后我想在图形的左上角创建一个图例。目前,我在第三个子图中创建了图例,但最好将其放在图形本身的左上角。
感谢您提供的任何帮助:)
\begin{figure}[tbh]{
\subfigure[]{
\begin{tikzpicture}
\pgfplotsset{width = .3\textwidth, compat = newest}
\begin{axis}[...]
\addplot [...] file{...};
\addplot [...] file{...};
\end{axis}
\end{tikzpicture}}
\subfigure[]{
\begin{tikzpicture}
\pgfplotsset{width = .3\textwidth, compat = newest}
\begin{axis}[...]
\addplot [...] file{...};
\addplot [...] file{...};
\end{axis}
\end{tikzpicture}}
\subfigure[]{
\begin{tikzpicture}
\pgfplotsset{width = .3\textwidth, compat = newest}
\begin{axis}[...]
\addplot [...] file{...}; \addlengentry{a}
\addplot [...] file{...}; \addlengentry{b}
\end{axis}
\end{tikzpicture}}
\caption{...}
\label{...}
}
\end{figure}$
答案1
正如安德鲁在他的对你的问题发表评论一种解决方案是使用groupplots
库pgfplots
,我也在我的解决方案中介绍了它。但这种方法也可以适用于彼此相邻的三个法线轴环境。
此外,您只需在第一个图中添加一些虚拟线,这样您想要在图例中显示的所有条目都具有相同的样式,然后将相对于图的图例放在其顶部。
有关更多详细信息,请查看代码中的注释。
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{
groupplots,
}
\pgfplotsset{
compat=1.13,
% define a style and put stuff in there which all plots have in common
my style/.style={
% reduce the `width' and `height' of the plots
width=4cm,height=4cm,
% set the labels for the axis
xlabel=$x$,
ylabel=$y$,
},
% define another style which contains the stuff of the legend
my legend style/.style={
legend entries={
blue plot,
red plot,
green plot
},
legend style={
at={([yshift=2pt]0,1)},
anchor=south west,
},
legend columns=4,
},
% define a `cycle list` which will be used in the plots
cycle multiindex* list={
blue!75!black,
red!75!black,
green!75!black
\nextlist
mark=*,
mark=square*,
mark=triangle*
\nextlist
},
}
\begin{document}
\begin{tikzpicture}
% if the plots are very similar, use the `groupplots' axis environment
\begin{groupplot}[
group style={
% place the three plots next to each other
group size=3 by 1,
% only set the tick and axis labels at the plots on the left
% and on the bottom
x descriptions at=edge bottom,
y descriptions at=edge left,
},
% load the style created in the preamble
my style,
%
% define the axis limits for the plots ...
xmin=0,xmax=2,
ymin=0,ymax=2,
% ... and enlarge them a bit (use the default value)
enlargelimits=true,
]
\nextgroupplot[
my legend style,
]
\addplot coordinates {(0,0) (1,1) (2,2)};
% to one of the plots you have to add some dummy plots
% so they are collected/stored in the legend
\addplot coordinates {(-1,-1)};
\addplot coordinates {(-1,-1)};
\nextgroupplot[
% start with the second item of the `cycle list' here, so
% shift it one element
cycle list shift=1,
]
\addplot coordinates {(0,2) (1,1) (2,0)};
\nextgroupplot[
% start with the third item of the `cycle list' here, so
% shift it two elements
cycle list shift=2,
]
\addplot coordinates {(0,2) (1,1) (2,1)};
\end{groupplot}
\end{tikzpicture}
\end{document}
答案2
- 简单来说,将
\caption{}
(和\label{}
)放在figure
环境内部而不是内部,subfigure
以将其与而
figure
不是相应的关联起来subfigure
。(并使用
\usepackage{subcaption}
) - 放在-s
\caption{}
之前
subfigure
以将其置于顶部。
\captionsetup{justification=raggedright,singlelinecheck=false}
在之前使用\caption{}
使其左对齐。(\usepackage{caption}
)
我的 MWE:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{caption} %% to play with captions
\usepackage{subcaption} %% for \begin{subfigure}
%\usepackage{tikz} %% for tikz
\usepackage{pgfplots} %% for \pgfplotsset
\begin{document}
\begin{figure}[tbh] %% there is no {...
\captionsetup{justification=raggedright,singlelinecheck=false}
\caption{...} %% inside the figure not the subfigure
\begin{subfigure}[b]{0.3\textwidth} %% not \subfigure[]{...
\includegraphics[width=0.15\textwidth]{solidtex.png}
\end{subfigure} %% not ...}
\begin{subfigure}[b]{0.3\textwidth} %% not \subfigure[]{...
\includegraphics[width=0.15\textwidth]{solidtex.png}
\end{subfigure} %% not ...}
\begin{subfigure}[b]{0.3\textwidth} %% not \subfigure[]{...
\includegraphics[width=0.15\textwidth]{solidtex.png}
\end{subfigure} %% not ...}
\label{...}
\end{figure}
\end{document}