如何向 pgfplots 极坐标图添加颜色和箭头装饰

如何向 pgfplots 极坐标图添加颜色和箭头装饰

我正在尝试向极坐标图添加不同的颜色和箭头:

\documentclass[]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
    \begin{polaraxis}[
        ymin=0, ymax=32,
        ytick=\empty, axis y line=none, 
        xticklabel=$\pgfmathprintnumber{\tick}^\circ$,
        nodes near coords style={font=\footnotesize},
        nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
        every axis plot/.append style={ultra thick},
        visualization depends on={x\as\myx},
        nodes near coords style={anchor=\myx-180},
    ]
    \addplot+ [polar comb, green, line width=3pt, mark=none, ->]
    coordinates {(0,21.6) (180,15.8)};
    \addplot+ [polar comb, red, line width=3pt, mark=none, ->]
    coordinates {(90,11.4) (270,10.6)};
    \addplot+ [mark=none, ->, polar comb, blue, line width=3pt]
    coordinates {(30,5.3) (60,5.5) (120,4.0) (150,6.6) (210,5.2) (240,5.5) (300,3.4) (330,5.2)};
    \end{polaraxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我已经将数据分成 3 个图,以显示 3 种不同的颜色(有没有更好的方法?),但问题是每个图中只有最后一项显示箭头。有没有比将数据分成 12 个图,显示 12 个箭头更好的方法?

答案1

我认为,为了附加箭头,必须想出一个稍微修改过的情节处理程序。

\documentclass[]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.16}

\begin{document}
\makeatletter
\def\pgfplothandlermypolarcomb{\pgfkeys{/pgf/plots/@handler options/.cd, 
start=\relax , 
end macro=\relax , 
point macro=\pgfutil@gobble , 
jump macro=\relax , 
special macro=\pgfutil@gobble ,
point macro=\pgf@plot@mypolarcomb@handler }}
\tikzoption{my polar comb}[]{\let\tikz@plot@handler=\pgfplothandlermypolarcomb}%
\def\pgf@plot@mypolarcomb@handler#1{\pgf@process {#1}%
\pgf@xa=\pgf@x 
\pgf@ya=\pgf@y 
\pgfsetarrowsend{>}%
\pgfpathmoveto{\pgfpointorigin}%
\pgfpathlineto{\pgfqpoint {\pgf@xa}{\pgf@ya}}%
\pgfusepath{stroke}%
}
\makeatother
\begin{tikzpicture}
    \begin{polaraxis}[>=stealth,
        ymin=0, ymax=32,
        ytick=\empty, axis y line=none, 
        xticklabel=$\pgfmathprintnumber{\tick}^\circ$,
        nodes near coords style={font=\footnotesize},
        nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
        every axis plot/.append style={ultra thick},
        visualization depends on={x\as\myx},
        nodes near coords style={anchor=\myx-180},
    ]
    \addplot+ [my polar comb, green, line width=3pt, mark=none, ->]
    coordinates {(0,21.6) (180,15.8)};
    \addplot+ [my polar comb, red, line width=3pt, mark=none, ->]
    coordinates {(90,11.4) (270,10.6)};
    \addplot+ [mark=none, ->,my polar comb, blue, line width=3pt]
    coordinates {(30,5.3) (60,5.5) (120,4.0) (150,6.6) (210,5.2) (240,5.5) (300,3.4) (330,5.2)};
    \end{polaraxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:感谢与我分享颜色处方!您可以使用散点图实现您想要的效果。不需要自创的绘图处理程序,但它仍然有点儿像黑客。

\documentclass[]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
    \begin{polaraxis}[>=stealth,
        ymin=0, ymax=32,
        ytick=\empty, axis y line=none, 
        xticklabel=$\pgfmathprintnumber{\tick}^\circ$,
        every axis plot/.append style={ultra thick},
        visualization depends on={x\as\myx},
        visualization depends on={y\as\myy},
        scatter/@pre marker code/.append code={
        \pgftransformreset
        \pgfmathsetmacro{\mycolor}{(abs(sin(\myx))<0.01 ? "green" :
        (abs(cos(\myx))<0.01 ? "red" : "blue"))}
        %\typeout{\myx,\myy,\mycolor}
        \draw[->,color=\mycolor] (axis cs:0,0) -- 
        (axis cs:\myx,\myy) node[anchor=\myx-180]{\pgfmathprintnumber{\myy}\%};
        },
    ]
    \addplot+ [scatter,draw=none]
    coordinates {(0,21.6) (180,15.8)
    (90,11.4) (270,10.6)
    (30,5.3) (60,5.5) (120,4.0) (150,6.6) (210,5.2) (240,5.5) (300,3.4) (330,5.2)};
    \end{polaraxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容