如何在绘图和颜色栏中进行注释

如何在绘图和颜色栏中进行注释

好吧,我写了这个 MWE

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep=comma, header = true]{
    label, angle, tempo, coef
    Cx-1, 45, 1.633333333,118.5483871
    Cx-2, 90, 1.916666667,110.9433962
    Cx-3, 45, 2.833333333,91.87500001
    Cx-4,-90, 2.950000000,89.90825688
    Cx-5, 90, 3.066666667,88.02395209
    Cx-6, 180, 3.183333333,86.21700880
}\censo
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        colorbar horizontal,
        point meta=x,
        colorbar style={,
            ylabel= Relative \\ absorption,
            ylabel style ={%
                rotate = -90,
                align=center},
            xtick={90,60,40,20},
            },
        width=16cm,
        height=10cm,  % size of the image
        grid = major,
        grid style = {gray!50},
        %
        axis on top=false,
        %
        xmin = 10,
        xmax = 150,
        ymin = 0,  
        ymax = 25,
        %
        xtick = {0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150},
        ytick = {0,2,4,6,8,10,12,14,16,18,20,22,24},
        %
        xtick pos=left,
        ytick pos=left,
        extra tick style={% changes for all extra ticks
            tick align=outside,
            tick pos=left,
            },
        extra x tick style={% changes for extra x ticks
            major tick length=1.3\baselineskip,
            /pgf/number format/precision=2,
            /pgf/number format/fixed,
            /pgf/number format/fixed zerofill,
            /pgf/number format/1000 sep={.},
            /pgf/number format/set decimal separator={,}%
            },
        extra y tick style={% changes for extra y ticks
                major tick length=1.5em,
                tick label style={rotate=90}
            },
        axis background/.style = {fill=white},
        ylabel = {%
            \begin{minipage}{6cm}
                \centering
                Infiltration time\\%
                (for 1 cm lowering)
            \end{minipage}},
        xlabel = {Infiltration coefficient($l/m^{2}\cdot\,dia$)},
        tick align = outside,
%        nodes near coords= {\pgfplotspointmeta},
%       nodes near coords align={vertical},
%       scatter/@pre marker code/.code={
%           \pgfplotstablegetelem{\coordindex}{[index]1}\of\censo%#1=row, #2=column
%           \edef\firstanchor{\pgfplotsretval}% 
%       }
        ]
        \addplot[mesh, domain=0:150, ultra thick] {490/x-2.5};
        \addplot[] graphics[xmin=60,ymin=8,xmax=150,ymax=25] {grafico_infiltracao.pdf}; %image in the right top corner      
        \addplot[
            scatter,
            only marks, 
            mark size = 2.5,
            every node near coord/.append               
                style={anchor/.expanded=\firstanchor},          
%           nodes near coords,
%           point meta = explicit symbolic,
%           every node near coord/.style = {
%               anchor = center,
%               pin = {\angle:\pgfplotspointmeta}
%               }
            ]
            table [x = coef,
                y = tempo,
%               meta = label
                ] {\censo};
        \end{axis}
    \end{tikzpicture}
\end{document}

如何调整上面的代码以获得如下结果:

在此处输入图片描述

答案1

几个月后……

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{ifthen}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep=comma, header = true]{
label,angle,rel_pos,leng,min,seg
Cx-01,90,south,1,1,36
Cx-02,90,south,1,3,17
Cx-27,270,north,1,3,22
Cx-29,45,south west,1.5,4,18
Cx-48,-90,north,1,4,19
Cx-05,45,south west,1,5,17
Cx-22,-90,north,1,5,18
Cx-44,90,south,1,5,31
Cx-19,225,north east,1,5,37
Cx-03,90,south,5,6,21
Cx-36,180,east,3.5,6,27
Cx-06,90,south,1,8,35
Cx-09,90,south,1,10,18
}\censo

\pgfplotstablecreatecol[
create col/expr={490/((\thisrow{min} + \thisrow{seg}/60) + 2.5)}
]{xvalue}{\censo}

\pgfplotstablecreatecol[
create col/expr={\thisrow{min} + \thisrow{seg}/60}
]{yvalue}{\censo}

\begin{document}
    \pgfplotsset{compat=newest,
colormap={mycolormap}{
    color(10)=(violet) color(19.999)=(violet) color(20)=(teal)
color(39.999)=(teal) color(40)=(olive)
color(59.999)=(olive) color(60)=(orange)
color(89.99)=(orange) color(90)=(red)
color(150)=(red)
}
}   

\tikzset{
    mystyle/.style={font=\scriptsize},
}

    \begin{tikzpicture}
        \begin{axis}[
            colorbar horizontal,
            point meta=x,
            point meta min=10,
            point meta max=150,
            enlargelimits=false,            
            colorbar style={,
                ylabel= Absorção \\ relativa,
                ylabel style ={%
                    rotate = -90,
                    align=center},
                xtick={90,60,40,20},
                extra x ticks = {10,30,50,75,120},
                    extra x tick labels = {
                    \textcolor{violet}{\bfseries\footnotesize Impermeável},
                    \textcolor{teal}{\bfseries\footnotesize {Semi\\impermeável}},
                    \textcolor{olive}{\bfseries\footnotesize Vagarosa},
                    \textcolor{orange}{\bfseries\footnotesize Média},
                    \textcolor{red}{\bfseries\footnotesize Rápida}
                },
                extra x tick style = {
                    draw=none,
                    text width = 1.7cm,
                    align = center,
                    major tick length=0\baselineskip,
                    yshift=-0.75cm, 
                    anchor=north    
                    }
                },
            width=16cm,
            height=10cm,  % size of the image
            grid = major,
            grid style = {gray!50},
            %
            axis on top=false,
            %
            xmin = 10,
            xmax = 150,
            ymin = 0,  
            ymax = 25,
            %
            xtick = {0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150},
            ytick = {0,2,4,6,8,10,12,14,16,18,20,22,24},
            %
            xtick pos=left,
            ytick pos=left,
            extra tick style={% changes for all extra ticks
                tick align=outside,
                tick pos=left,
            },
            extra x tick style={% changes for extra x ticks
                major tick length=1.3\baselineskip,
                /pgf/number format/precision=2,
                /pgf/number format/fixed,
                /pgf/number format/fixed zerofill,
                /pgf/number format/1000 sep={.},
                /pgf/number format/set decimal separator={,}%
            },
            extra y tick style={% changes for extra y ticks
                major tick length=1.5em,
                tick label style={rotate=90}
            },
            axis background/.style = {fill=white},
            ylabel = {%
                \begin{minipage}{10cm}
                    \centering
Tempo de infiltração\\%
(minutos p/ rebaixamento de 15 para 14 cm)
            \end{minipage}},
            xlabel = {Coeficiente de infiltração $\begin{aligned}\left(\frac{L}{m^{2}\cdot\,dia}\right)\end{aligned}$},
            tick align = outside,
            ]
            % Waterproof segment (violet)
            \addplot[domain=10:20, ultra thick, violet] {490/x-2.5};
            % Semi-waterproof segment (green)
            \addplot[domain=20:40, ultra thick, teal] {490/x-2.5};
            % Slow segment (olive)
            \addplot[domain=40:60, ultra thick, olive] {490/x-2.5};
            % Medium segment (orange)
            \addplot[domain=60:90, ultra thick, orange] {490/x-2.5};
            % Fast segment (red)
            \addplot[domain=90:150, ultra thick, red] {490/x-2.5};
            \addplot[] graphics[xmin=60,ymin=8,xmax=150,ymax=25] {grafico_infiltracao.pdf}; %image in the right top corner
            \addplot+[scatter,
                mark = *,
                only marks,
                scatter/use mapped color={draw=mapped color, fill=mapped color}] 
                table[x=xvalue, y=yvalue] {\censo};
                % Add labels and small dashes
% Add labels and small dashes
\pgfplotstablegetrowsof{\censo}
\pgfmathsetmacro{\totalrows}{\pgfplotsretval-1}

\foreach \row in {0,1,...,\totalrows}{
    \pgfplotstablegetelem{\row}{label}\of{\censo}
    \edef\label{\pgfplotsretval}
    \pgfplotstablegetelem{\row}{xvalue}\of{\censo}
    \edef\xvalue{\pgfplotsretval}
    \pgfplotstablegetelem{\row}{yvalue}\of{\censo}
    \edef\yvalue{\pgfplotsretval}
    \pgfplotstablegetelem{\row}{angle}\of{\censo}
    \edef\angle{\pgfplotsretval}
    \pgfplotstablegetelem{\row}{rel_pos}\of{\censo}
    \edef\relpos{\pgfplotsretval}
    \pgfplotstablegetelem{\row}{leng}\of{\censo}
    \edef\leng{\pgfplotsretval}
    
    \pgfmathsetmacro{\dashlength}{\leng}  % Adjust the dash length here
    \pgfmathsetmacro{\dx}{\dashlength*cos(\angle)}
    \pgfmathsetmacro{\dy}{\dashlength*sin(\angle)}
    \pgfmathsetmacro{\mycolor}{
        ifthenelse(\xvalue < 20, "violet",
        ifthenelse(\xvalue < 40, "teal",
        ifthenelse(\xvalue < 60, "olive",
        ifthenelse(\xvalue < 90, "orange", "red"))))
    }   
    \edef\temp{\noexpand\draw [->, color=\mycolor] (axis cs:\xvalue,\yvalue) -- (axis cs:\xvalue+\dx,\yvalue+\dy);
        \noexpand\node[mystyle, \mycolor, anchor=\relpos] at (axis cs:\xvalue+\dx,\yvalue+\dy) {\label};}
    \temp
}
        \end{axis}
    \end{tikzpicture}
\end{document}

结果是:

在此处输入图片描述

相关内容