我正在使用 tikz-library 矩阵为具有两个 y 轴的图创建图例。
当我将“修剪轴右”添加到代码中时,图例会向右拉伸。
我该如何防止这种情况发生?
\documentclass{scrreprt}
\usepackage{color}
\usepackage{showframe}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[%
scale only axis,
hide x axis,
ylabel={y2},
axis x line*=bottom,
axis y line*=right
]
\addplot {ln(x)};
\label{pgfplots:Plot1}
\addplot {ln(x)};
\label{pgfplots:Plot2}
\end{axis}
\begin{axis}[%
scale only axis,
xlabel={x},
ylabel={y1},
]
\addplot {ln(x)};
\label{pgfplots:Plot3}
\addplot {ln(x)};
\label{pgfplots:Plot4}
\addplot {ln(x)};
\label{pgfplots:Plot5}
\addplot {ln(x)};
\label{pgfplots:Plot6}
\end{axis}
\matrix[
matrix of nodes,
anchor=north west,
fill=white,
draw,
inner sep=0.3em,
column sep = 0.3em,
node font=\scriptsize,
align=left,
column 1/.style={nodes={anchor=center}},
column 2/.style={nodes={anchor=west}},
column 3/.style={nodes={anchor=center}},
column 4/.style={nodes={anchor=west}},
draw
]
at (current axis.north west){
\ref{pgfplots:Plot1}& Text1 & \ref{pgfplots:Plot4}& Text4\\
\ref{pgfplots:Plot2}& Text2 & \ref{pgfplots:Plot5}& Text5\\
\ref{pgfplots:Plot3}& LongerText & \ref{pgfplots:Plot6}& LongerText\\};
\end{tikzpicture}
\caption{caption}
\end{figure}
\end{document}
另一个问题是图例中的彩色线条略高于相应标签的文本高度的中间。如果
column 1/.style={nodes={anchor=center}},
column 2/.style={nodes={anchor=west}},
column 3/.style={nodes={anchor=center}},
column 4/.style={nodes={anchor=west}},
被移除后,它们位于中间,但图标签不再居中。我该如何解决这个问题?
答案1
仅作为一种解决方法:text width
为图例中包含彩色线条的节点设置选项:
column 1/.style={nodes={align=center,text width=.6cm}},
column 2/.style={nodes={anchor=base west}},
column 3/.style={nodes={align=center,text width=.6cm}},
column 4/.style={nodes={anchor=base west}},
代码:
\documentclass{scrreprt}
\usepackage{showframe}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[scale only axis,
hide x axis,
ylabel={y2},
axis x line*=bottom,axis y line*=right
]
\addplot {ln(x)};\label{pgfplots:Plot1}
\addplot {ln(x)};\label{pgfplots:Plot2}
\end{axis}
\begin{axis}[scale only axis,
xlabel={x},ylabel={y1},
]
\addplot {ln(x)};\label{pgfplots:Plot3}
\addplot {ln(x)};\label{pgfplots:Plot4}
\addplot {ln(x)};\label{pgfplots:Plot5}
\addplot {ln(x)};\label{pgfplots:Plot6}
\end{axis}
\matrix[
matrix of nodes,
anchor=north west,
fill=white,draw,
inner sep=0.3em,
%column sep = 0.3em,
node font=\scriptsize,
column 1/.style={nodes={align=center,text width=.6cm}},
column 2/.style={nodes={anchor=base west}},
column 3/.style={nodes={align=center,text width=.6cm}},
column 4/.style={nodes={anchor=base west}},
]
at (current axis.north west){
\ref{pgfplots:Plot1}& Text1 & \ref{pgfplots:Plot4}& Text4\\
\ref{pgfplots:Plot2}& Text2 & \ref{pgfplots:Plot5}& Text5\\
\ref{pgfplots:Plot3}& LongerText & \ref{pgfplots:Plot6}& LongerText\\};
\end{tikzpicture}
\caption{caption}
\end{figure}
\end{document}
以下是另一个可能与图像外化不兼容的建议:删除trim axis left, trim axis right
并插入
\pgfresetboundingbox% resets the bounding box
\useasboundingbox(current axis.below south west)
rectangle (current axis.above north east);% defines a new one
在第二个图之后。我在下图中插入了一个橙色矩形来显示生成的边界框。
代码:
\documentclass{scrreprt}
\usepackage{showframe}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[scale only axis,
hide x axis,
ylabel={y2},
axis x line*=bottom,axis y line*=right
]
\addplot {ln(x)};\label{pgfplots:Plot1}
\addplot {ln(x)};\label{pgfplots:Plot2}
\end{axis}
\begin{axis}[scale only axis,
xlabel={x},ylabel={y1},
]
\addplot {ln(x)};\label{pgfplots:Plot3}
\addplot {ln(x)};\label{pgfplots:Plot4}
\addplot {ln(x)};\label{pgfplots:Plot5}
\addplot {ln(x)};\label{pgfplots:Plot6}
\end{axis}
\matrix[
matrix of nodes,
anchor=north west,
fill=white,draw,
inner sep=0.3em,
%column sep = 0.3em,
node font=\scriptsize,
column 1/.style={nodes={align=center}},
column 2/.style={nodes={anchor=base west}},
column 3/.style={nodes={align=center}},
column 4/.style={nodes={anchor=base west}},
]
at (current axis.north west){
\ref{pgfplots:Plot1}& Text1 & \ref{pgfplots:Plot4}& Text4\\
\ref{pgfplots:Plot2}& Text2 & \ref{pgfplots:Plot5}& Text5\\
\ref{pgfplots:Plot3}& LongerText & \ref{pgfplots:Plot6}& LongerText\\};
\pgfresetboundingbox% resets the bounding box
\useasboundingbox(current axis.below south west)
rectangle (current axis.above north east);% defines a new one
% to show the resulting bounding box
\draw[orange,very thick](current bounding box.south west)
rectangle(current bounding box.north east);
\end{tikzpicture}
\caption{caption}
\end{figure}
\end{document}