在 PGFplots 中重複使用系列樣式

在 PGFplots 中重複使用系列樣式

我的文件中有很多 PGF 圖,它們始終具有一致的風格,透過共享循環清單進行管理。

現在我想在同一個 PGFplot 中重複使用先前的繪圖樣式。

作為一個例子,在這裡我想說第三個圖應該使用與第一個圖相同的樣式,但不指定該樣式是什麼(因為這將在循環列表的其他地方給出)。

在此輸入影像描述

(抱歉圖像右側的醜陋裁剪:P)

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

%and define new styles like this:
% \pgfplotsset{
%    ...
%}

\begin{document}
\begin{tikzpicture}
   \begin{axis}[ylabel=y,
    xlabel=x,]
        \addplot
        coordinates
        {
            (0,10)
            (10,0)
        };
        \addlegendentry{Descending}

        \addplot
        coordinates
        {
            (0,3)
            (10,5)
        }; 
        \addlegendentry{Ascending}

        \addplot
        coordinates
        {
            (0,7)
            (10,5)
        };
        % Reuse descending style
    \end{axis}     
\end{tikzpicture}
\end{document}

我希望第三個棕色圖與第一個藍色圖具有相同的樣式,但無需手動指定 、 等樣式詳細資訊bluemark=o

或者,如果不可能,我可以載入第三個圖的活動循環清單中的第一個樣式嗎?

答案1

pgfplots帶有\label/\ref機制來標記繪圖並在其他地方引用它們的樣式。雖然這種機制的目的是在文字內部的某處產生圖例,但也可以使用鍵存取標記圖的樣式選項refstyle

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

%and define new styles like this:
% \pgfplotsset{
%    ...
%}

\begin{document}
\begin{tikzpicture}
   \begin{axis}[ylabel=y,
    xlabel=x,]
        \addplot
        coordinates
        {
            (0,10)
            (10,0)
        };
        \addlegendentry{Descending}
        \label{plot:1}

        \addplot
        coordinates
        {
            (0,3)
            (10,5)
        }; 
        \addlegendentry{Ascending}

        \addplot[refstyle={plot:1}]
        coordinates
        {
            (0,7)
            (10,5)
        };
        % Reuse descending style
    \end{axis}     
\end{tikzpicture}
\end{document}

在此輸入影像描述

請注意,這需要對文件進行兩次編譯。有關此\label/\ref機制的詳細信息,請參閱手冊中的“帶有標籤和參考的圖例”部分。

答案2

或者,如果不可能,我可以載入第三個圖的活動循環清單中的第一個樣式嗎?

您可以使用

\pgfplotsset{cycle list shift=-2}

向後或向前移動循環列表計數器,我們可以透過在第三個圖之前添加上述命令來實現我們所需要的。

請注意,這不會累積......因此,如果您希望第四個圖上升,則需要添加:

\pgfplotsset{cycle list shift=-2}

再次。

相關內容