如何更改乳胶中的图例栏?

如何更改乳胶中的图例栏?

我正在使用 tikzpicture 在 latex 中绘制图表。但是,图例显示为双矩形。我可以将图例更改为一个矩形吗?谢谢

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{figure} 
    \begin{tikzpicture} 
        \begin{axis}[
            ybar,
            xlabel = X,
            xmin = 0.0,
            xmax = 5,
            ymin = 0.5,
            ymax = 6.0,
            axis x line* = bottom,
            axis y line* = left,
            ylabel= Y,
            ymajorgrids = true,
            xticklabels = \empty,
            extra x ticks = {1,2,3,4, 5},
            extra x tick labels = {1,2,3,4,5},
            ylabel near ticks,
            legend style={draw=darkgray!60!black,fill=white,align=left,legend pos= north west,legend columns=-1}
            ]           
            \addplot [red!20!black,fill=red!80!white] coordinates {
                (1,1)
                (2,2)
                (3,4)
                (4,3)               
            };
            \addlegendentry{A};         
        \end{axis} 
    \end{tikzpicture}
    \caption{Test}  
\end{figure}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

您可以重新定义legend image

    legend image code/.code={
        \draw [/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
        plot coordinates {(0cm,0.8em)};
    },

得出的结果是:

在此处输入图片描述

代码:

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{figure} 
    \begin{tikzpicture} 
        \begin{axis}[
            ybar,
            xlabel = X,
            xmin = 0.0,
            xmax = 5,
            ymin = 0.5,
            ymax = 6.0,
            axis x line* = bottom,
            axis y line* = left,
            ylabel= Y,
            ymajorgrids = true,
            xticklabels = \empty,
            extra x ticks = {1,2,3,4, 5},
            extra x tick labels = {1,2,3,4,5},
            ylabel near ticks,
            legend image code/.code={
                \draw [/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
                plot coordinates {(0cm,0.8em)};
            },
            legend style={draw=darkgray!60!black,fill=white,align=left,legend pos= north west,legend columns=-1}
            ]           
            \addplot [red!20!black,fill=red!80!white] coordinates {
                (1,1)
                (2,2)
                (3,4)
                (4,3)               
            };
            \addlegendentry{A};         
        \end{axis} 
    \end{tikzpicture}
    \caption{Test}  
\end{figure}
\end{document}

相关内容