我想仅生成带有 pgfplots 中模式的循环列表的图例。这是我拥有的代码。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\def\mystrut{\vphantom{hg}}
\pgfplotscreateplotcyclelist{cyc}{
{black,solid, very thick},
{blue,dashed, very thick},
{red,dashdotted, very thick},
{black,dotted, very thick},
{brown,dash pattern=on 5pt off 2pt, very thick},
{teal,dashdotdotted, very thick},
{orange,densely dashed, very thick},
{violet,densely dotted, very thick},
{cyan,densely dashdotted, very thick},
{green!70!black,loosely dashdotdotted, very thick},
{magenta,dash pattern=on 3pt off 2pt on \the\pgflinewidth off 2pt, very thick}
{gray}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend columns=3,
hide axis,
xmin=10,
xmax=50,
ymin=0,
ymax=0.4,
legend style={font=\mystrut,draw=white!15!black,legend cell align=left},
cycle list name = cyc,
legend image post style={sharp plot}
]
\addlegendimage{}
\addlegendentry{A};
\addlegendimage{}
\addlegendentry{B};
\addlegendimage{}
\addlegendentry{C};
\addlegendimage{}
\addlegendentry{D};
\addlegendimage{}
\addlegendentry{E};
\addlegendimage{}
\addlegendentry{F};
\end{axis}
\end{tikzpicture}
\end{document}
结果:
结果中没有循环模式。如何获取自动模式?
答案1
\addlegendimage
需要您手动指定样式,它不会从循环列表中读取,它所做的只是向图例添加一些内容。您可以通过将轴做得非常小(因此图例框变得比轴大)来解决此问题,然后添加六个虚拟图。也许不太优雅,但它确实有效:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\def\mystrut{\vphantom{hg}}
\pgfplotscreateplotcyclelist{cyc}{
{black,solid, very thick},
{blue,dashed, very thick},
{red,dashdotted, very thick},
{black,dotted, very thick},
{brown,dash pattern=on 5pt off 2pt, very thick},
{teal,dashdotdotted, very thick},
{orange,densely dashed, very thick},
{violet,densely dotted, very thick},
{cyan,densely dashdotted, very thick},
{green!70!black,loosely dashdotdotted, very thick},
{magenta,dash pattern=on 3pt off 2pt on \the\pgflinewidth off 2pt, very thick}
{gray}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend columns=3,
scale only axis,width=1mm, % make axis really small (smaller than legend)
hide axis,
legend style={font=\mystrut,draw=white!15!black,legend cell align=left},
cycle list name = cyc,
legend image post style={sharp plot}
]
\pgfplotsinvokeforeach{1,...,6}{\addplot coordinates {(0,0)};} % make six dummy plots
\addlegendentry{A};
\addlegendentry{B};
\addlegendentry{C};
\addlegendentry{D};
\addlegendentry{E};
\addlegendentry{F};
\end{axis}
\end{tikzpicture}
\end{document}