如何更改 pgfplots 中一个轴的颜色?

如何更改 pgfplots 中一个轴的颜色?

我知道如何更改 pgfplots 中所有轴的颜色:

\begin{axis}[blue]
\end{axis}

这使得图的所有四个边都变成蓝色。另一个示例如下:PGFPlots:更改轴线的颜色

问题:如何仅更改其中一个轴的颜色,例如左侧 y 轴?

答案1

目前没有使用不同样式绘制单个线条的选项。不过添加起来并不难。如果你将以下代码块复制到序言中,你可以单独设置轴线样式,因此你可以说

\begin{axis}[
    separate axis lines,
    first x axis line style=red,
    second x axis line style={ultra thick, dashed},
    first y axis line style=blue
  ]
  \addplot {rnd};
\end{axis}

要得到


这是代码块:

\pgfplotsset{
    every first x axis line/.style={},
    every first y axis line/.style={},
    every first z axis line/.style={},
    every second x axis line/.style={},
    every second y axis line/.style={},
    every second z axis line/.style={},
    first x axis line style/.style={/pgfplots/every first x axis line/.append style={#1}},
    first y axis line style/.style={/pgfplots/every first y axis line/.append style={#1}},
    first z axis line style/.style={/pgfplots/every first z axis line/.append style={#1}},
    second x axis line style/.style={/pgfplots/every second x axis line/.append style={#1}},
    second y axis line style/.style={/pgfplots/every second y axis line/.append style={#1}},
    second z axis line style/.style={/pgfplots/every second z axis line/.append style={#1}}
}

\makeatletter
\def\pgfplots@drawaxis@outerlines@separate@onorientedsurf#1#2{%
    \if2\csname pgfplots@#1axislinesnum\endcsname
        % centered axis lines handled elsewhere.
    \else
    \scope[/pgfplots/every outer #1 axis line,
        #1discont,decoration={pre length=\csname #1disstart\endcsname, post length=\csname #1disend\endcsname}]
        \pgfplots@ifaxisline@B@onorientedsurf@should@be@drawn{0}{%
            \draw [/pgfplots/every first #1 axis line] decorate {
                \pgfextra
                % exchange roles of A <-> B axes:
                \pgfplotspointonorientedsurfaceabsetupfor{#2}{#1}{\pgfplotspointonorientedsurfaceN}%
                \pgfplots@drawgridlines@onorientedsurf@fromto{\csname pgfplots@#2min\endcsname}%
                \endpgfextra 
                };
        }{}%
        \pgfplots@ifaxisline@B@onorientedsurf@should@be@drawn{1}{%
            \draw [/pgfplots/every second #1 axis line] decorate {
                \pgfextra
                % exchange roles of A <-> B axes:
                \pgfplotspointonorientedsurfaceabsetupfor{#2}{#1}{\pgfplotspointonorientedsurfaceN}%
                \pgfplots@drawgridlines@onorientedsurf@fromto{\csname pgfplots@#2max\endcsname}%
                \endpgfextra 
                };
        }{}%
    \endscope
    \fi
}%
\makeatother

答案2

好吧,我使用下面的技巧来根据需要给线条上色。

到颜色 x 轴。

\addplot+[red, thick, mark=none] coordinates {(0, 0) (x-max, 0)};

到颜色 y 轴。

\addplot+[red, thick, mark=none] coordinates {(0, 0) (0, y-max)};

相关内容