使用自定义图例样式进行处理

使用自定义图例样式进行处理

我怎样才能使自定义图例框的大小适应其内容?

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usepackage{amsmath}
\usepackage{float}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
    \newenvironment{customlegend}[1][]{%
        \begingroup
        % inits/clears the lists (which might be populated from previous
        % axes):
        \csname pgfplots@init@cleared@structures\endcsname
        \pgfplotsset{#1}%
    }{%
        % draws the legend:
        \csname pgfplots@createlegend\endcsname
        \endgroup
    }
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

    %%--------------------------------
    \begin{axis}[
    width=5cm, height=5cm,
    xmin=0, xmax=1,
    ymin=0, ymax=1.3, 
    xtick=\empty, ytick=\empty,
    axis lines=middle,
    x label style={at={(1,0)},right},
    xlabel={$X_\mathrm{A}$}, ylabel={$-\dfrac{1}{r_\mathrm{A}}$},
    restrict y to domain=0:1,
    ylabel style={at={(ticklabel cs:0.75)},xshift=-1.25cm},
    clip=false
    ]
    \node[anchor=east] (zero) at (axis cs:0,0) {0};             
    \addplot [name path=V,red, very thick, samples=100, smooth,domain=0:1]
    { (1/3) * ((1)/(1-x)) };
    \path[name path=axis] (axis cs:0,0) -- (axis cs:0.6,0);
    
    \addplot [
        thick,
        color=blue,
        fill=blue, 
        fill opacity=0.1
    ]
    fill between[
        of=V and axis,
        soft clip={domain = 0:0.6},
    ];
    \node[anchor=east] (zero) at (axis cs:0,0) {0};

    \draw[dotted] (axis cs:0.6,0) --++ (0,-0.3) node[anchor=north] (Xaf) {$X_\mathrm{A,finale}$};  
    \end{axis}
    \begin{customlegend}[
        legend cell align=left,
        legend entries={ $\dfrac{t}{c_\mathrm{A,0}}$ },
        legend style={at={(6,3)},font=\footnotesize}]
        \addlegendimage{legend image code/.code={
        \node [color=white, draw, rectangle, minimum size=1.5em, fill=blue, fill opacity=0.1,anchor=15] {};}
        }
    \end{customlegend}
    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

或者有没有更简单的方法来获得同样的传奇?

答案1

我认为,在使用诸如 之类的高级软件包时,创建自定义图例、轴和刻度标签应该是最后的手段pgfplots。手册中有几个记录良好的功能可让您实现所需的结果,包括可以自定义的图例。其他人注意到的一个问题是,在图例单元格中使用显示样式数学时,您可能需要使用 引入一些人为的空白\raisebox{<vertical shift>}{<content>},如 中建议的那样问题。

\documentclass[tikz]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=5cm,
    height=5cm,
    xmin=0, xmax=1,
    ymin=0, ymax=1.3, 
    xlabel={$X_\text{A}$},
    ylabel={$-\frac{1}{r_\text{A}}$},
    xlabel style={at={(rel axis cs:1,0)}, right},
    ylabel style={at={(rel axis cs:0,0.75)}, rotate=-90},
    axis lines=left,
    major y tick style={draw=none},
    xtick=\empty,
    ytick={0},
    clip=false,
    legend pos=outer north east,
    legend cell align=left,
    extra x ticks={0.6},
    extra x tick style={tick align=outside,tick style={black,dotted},tickwidth=0.5cm},
    extra x tick labels={$X_\text{A,finale}$},
]

\addplot [domain=0:0.6, draw opacity=0, fill=blue, fill opacity=0.1, area legend] {1/3*1/(1-x)} \closedcycle;
\addlegendentry{\raisebox{0.5ex}{$\frac{t}{c_\text{A,0}}$}}

\addplot [restrict y to domain=0:1, red, very thick, domain=0:1, forget plot] {1/3*1/(1-x)};

\end{axis}
\end{tikzpicture}
\end{document}

输出

答案2

legend style={nodes={text depth=3mm}}对我有用:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usepackage{amsmath}
\usepackage{float}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
    \newenvironment{customlegend}[1][]{%
        \begingroup
        % inits/clears the lists (which might be populated from previous
        % axes):
        \csname pgfplots@init@cleared@structures\endcsname
        \pgfplotsset{#1}%
    }{%
        % draws the legend:
        \csname pgfplots@createlegend\endcsname
        \endgroup
    }
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

    %%--------------------------------
    \pgfmathsetmacro{\k}{5};
    \pgfmathsetmacro{\A}{1};
    \begin{axis}[
    width=5cm, height=5cm,
    xmin=0, xmax=1,
    ymin=0, ymax=1.3, 
    xtick=\empty, ytick=\empty,
    axis lines=middle,
    x label style={at={(1,0)},right},
    xlabel={$X_\mathrm{A}$}, ylabel={$-\dfrac{1}{r_\mathrm{A}}$},
    restrict y to domain=0:1,
    ylabel style={at={(ticklabel cs:0.75)},xshift=-1.25cm},
    clip=false
    ]
    \node[anchor=east] (zero) at (axis cs:0,0) {0};             
    \addplot [name path=V,red, very thick, samples=100, smooth,domain=0:1]
    { (1/3) * ((1)/(1-x)) };
    \path[name path=axis] (axis cs:0,0) -- (axis cs:0.6,0);
    
    \addplot [
        thick,
        color=blue,
        fill=blue, 
        fill opacity=0.1
    ]
    fill between[
        of=V and axis,
        soft clip={domain = 0:0.6},
    ];
    \node[anchor=east] (zero) at (axis cs:0,0) {0};

    \draw[dotted] (axis cs:0.6,0) --++ (0,-0.3) node[anchor=north] (Xaf) {$X_\mathrm{A,finale}$};  
    \end{axis}
    \begin{customlegend}[
        legend cell align=left,
        legend entries={ $\dfrac{t}{c_\mathrm{A,0}}$ },
        legend style={
            nodes={text depth=3mm}, % ‹— this
            at={(6,3)},
            font=\footnotesize}
        ]
        \addlegendimage{legend image code/.code={
        \node [color=white, draw, rectangle, minimum size=1em, fill=blue, fill opacity=0.1,anchor=east] {};}
        }
    \end{customlegend}
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容