我需要你的帮助。我遇到了一个问题,困扰了我好几天,但还是找不到解决办法。
我想将三张图表并排放置。
所有图表都应具有相同的 x 轴标签、y 轴标签和图例。也就是说,不是每个图表都单独使用一个,而是使用一个全局轴标签。
编辑:
- 所有三个 y 轴的比例相同,只需要一个全局图例(和值)。
-x 轴值应该保留,但标签(R)应该是全局的。
-每个图表中可以有多个绘图,因此如果图例在三个图表上齐平会更好。
编辑2:
有一个小瑕疵。是否可以将图例(大量文本)与左右两侧的图表完全齐平?(请参阅我的示例代码!)
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[name=leftplot, width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
% xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\begin{axis}[name=middleplot, at ={($(leftplot.east)+(1cm,0)$)}, anchor=west,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{11 1111 111 }
\addlegendentry{22 2222 222};
\addlegendentry{33 3333 333};
\addlegendentry{44 4444 444};
\addlegendentry{55 5555 555};
\addlegendentry{66 6666 666};
\end{axis}
\begin{axis}[name=rightplot, at = {($(middleplot.east)+(1cm,0)$)}, anchor=west,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
% xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\caption{Example}
\end{figure}
\end{document}
问候 Parsec
2:代码示例
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\begin{document}
\begin{figure}
\begin{center}
\begin{minipage}{\linewidth}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)},anchor=south, legend columns=3,column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{1}
\addlegendentry{2};
\addlegendentry{3};
\addlegendentry{4};
\addlegendentry{5};
\addlegendentry{6};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{center}
\caption{Example}
\end{figure}
\end{document}
答案1
只是对 OP 代码的第一次测试。每个情节都是独立的tikzpicture
,并且根据其对齐baseline
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
% xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{1}
\addlegendentry{2};
\addlegendentry{3};
\addlegendentry{4};
\addlegendentry{5};
\addlegendentry{6};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
% xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\caption{Example}
\end{figure}
\end{document}
第二种选择:所有地块都具有相同的 inte tikzpicture
,并根据其“节点”锚点对齐,name
每个地块都有一个固定的选项。此解决方案显示在groupplots
库部分中。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[name=leftplot, width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
% xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\begin{axis}[name=middleplot, at ={($(leftplot.east)+(1cm,0)$)}, anchor=west,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{1}
\addlegendentry{2};
\addlegendentry{3};
\addlegendentry{4};
\addlegendentry{5};
\addlegendentry{6};
\end{axis}
\begin{axis}[name=rightplot, at = {($(middleplot.east)+(1cm,0)$)}, anchor=west,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
% xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\caption{Example}
\end{figure}
\end{document}
第三种选择:使用groupplots
库
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{groupplot}[%
group style={%
group size=3 by 1,
},
width=0.3\textwidth,height=0.2\textheight,
grid=major, cycle list name=color, ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south, legend columns=3, column sep=6pt}
]
\nextgroupplot[ylabel={R}]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\nextgroupplot[xlabel={x}]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{1};
\addlegendentry{2};
\addlegendentry{3};
\addlegendentry{4};
\addlegendentry{5};
\addlegendentry{6};
\nextgroupplot
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
更新:与图例边距对齐
为了将第一和第三个图与长图例边框对齐,最简单的方法是先绘制中心图,然后使用outer
锚点(图例定义它们)来固定横向图。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{axis}[name=middleplot,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addlegendentry{11 1111 111 }
\addlegendentry{22 2222 222};
\addlegendentry{33 3333 333};
\addlegendentry{44 4444 444};
\addlegendentry{55 5555 555};
\addlegendentry{66 6666 666};
\end{axis}
\begin{axis}[name=leftplot, at ={(middleplot.outer west|-middleplot.center)}, anchor=west,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
ylabel={R},
% xlabel={x},
ylabel near ticks,
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\begin{axis}[name=rightplot, at = {(middleplot.center-|middleplot.outer east)}, anchor=east,
width=0.3\textwidth,height=0.2\textheight,
grid=major,
cycle list name=color,
% ylabel={R},
% xlabel={x},
ylabel near ticks,
legend style={at={(0.5,1.05)}, anchor=south,
legend columns=3, column sep=6pt}
]
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{axis}
\end{tikzpicture}
\caption{Example}
\end{figure}
\end{document}