我有两张 tikzpicture,每张都有一个条形图。我希望这两张 tikzpicture 共享一个共同的图例。清除第一张 tikz 中的图例并将第二张 tikz 的图例移动到两个 tikz 区域中间的明显方法不起作用。
图例位置似乎缩小了一张 tikzpicture。如何才能让一个图例位于两张 tikzpicture 中间,而不产生这种奇怪的缩小效果?
以下是 MWE:
\documentclass{standalone}
\usepackage{times}
\usepackage{url}
\usepackage{latexsym}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots, pgfplotstable}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{nicefrac}
\pgfplotsset{grid style={dashed,gray}}
\usepackage{colortbl}
\usepackage{listings}
\usepgfplotslibrary{statistics}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage[justification=centering]{caption}
\begin{document}
%\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotsset{grid style={dashed,gray}}
\begin{axis}[
ybar,
% xlabel=Approachs,
ylabel={Accuracy (\%)},
% legend style={font=\tiny, at={(0.05,0.97)},anchor=north west,legend columns=1},
legend style={font=\small, at={(-0.1,0.-0.2)},anchor=north west,legend columns=2},
symbolic x coords={A, B},
ymax=100,
ymin=30.0,
xtick=data,
enlarge x limits=+0.5,
ymajorgrids=true,
bar width=0.3cm,
xmajorgrids=true ,
ytick={30,40,...,100},
]
\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
(A, 83.4)
(B, 89.0)
};
\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\legend{}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfplotsset{grid style={dashed,gray}}
\begin{axis}[
ybar,
% xlabel=Approachs,
ylabel={Accuracy (\%)},
legend style={font=\small, at={(-0.6,0.-0.2)},anchor=north west,legend columns=3},
symbolic x coords={A, B},
ymax=100,
ymin=30.0,
xtick=data,
enlarge x limits=+0.5,
ymajorgrids=true,
bar width=0.3cm,
xmajorgrids=true ,
ytick={30,40,...,100},
]
\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
(A, 83.4)
(B, 89.0)
};
\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
没有涉及缩放,您看到的效果并不奇怪。 Atikzpicture
最终只是一个大框,放置在类似于字母的基线上X
。 第一个的底部tikzpicture
是轴上刻度标签的底部,但第二个的底部是图例的底部。
但无论如何,只需将两个axis
环境放在同一个位置tikzpicture
,例如:
\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}
%\begin{figure}
%\centering
\begin{tikzpicture}
\pgfplotsset{grid style={dashed,gray}}
\begin{axis}[
name=ax1,
ybar,
% xlabel=Approachs,
ylabel={Accuracy (\%)},
% legend style={font=\tiny, at={(0.05,0.97)},anchor=north west,legend columns=1},
legend style={font=\small, at={(-0.1,0.-0.2)},anchor=north west,legend columns=2},
symbolic x coords={A, B},
ymax=100,
ymin=30.0,
xtick=data,
enlarge x limits=+0.5,
ymajorgrids=true,
bar width=0.3cm,
xmajorgrids=true ,
ytick={30,40,...,100},
]
\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
(A, 83.4)
(B, 89.0)
};
\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\legend{}
\end{axis}
\begin{axis}[
at={(ax1.south east)},
xshift=2cm,
ybar,
% xlabel=Approachs,
ylabel={Accuracy (\%)},
legend style={font=\small, at={(0,-0.1)},anchor=north east,legend columns=3},
symbolic x coords={A, B},
ymax=100,
ymin=30.0,
xtick=data,
enlarge x limits=+0.5,
ymajorgrids=true,
bar width=0.3cm,
xmajorgrids=true ,
ytick={30,40,...,100},
]
\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
(A, 83.4)
(B, 89.0)
};
\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
(A, 85.2)
(B, 90.6)
};
\end{axis}
\end{tikzpicture}
\end{document}