tikz:如何用线条对直方图中的区域进行阴影处理?

tikz:如何用线条对直方图中的区域进行阴影处理?

我有以下运行良好的代码。

% Source: http://tex.stackexchange.com/a/65518/5645
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{pgfplots}
\usetikzlibrary{calc,decorations.pathmorphing,patterns}
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,
    auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                      {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}
        }
                    {\pgfqpoint{-\pgfdecorationsegmentaspect
                     \pgfdecoratedinputsegmentremainingdistance}%
                               {\pgfmathresult\pgfdecorationsegmentamplitude}
                    }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}

\begin{document}
\begin{preview}
    \begin{tikzpicture}[decoration=penciline, decorate]
        \begin{axis}[
            ybar interval,
            xticklabel=
            \pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
        ]
            \addplot+[hist={bins=3}]
            table[row sep=\\,y index=0] {
            data\\
            1\\ 2\\ 1\\ 5\\ 4\\ 6\\
            7\\ 6\\ 9\\ 8\\ 5\\ 2\\
            };
        \end{axis}
    \end{tikzpicture}
\end{preview}
\end{document}

我想要做的是让直方图中的条形以 45 度角的线条进行阴影处理(而不是着色)。我该怎么做?

答案1

获得 45 度线等的最简单方法是使用patterns.meta。但这实际上不支持装饰。因此,假设您想要装饰线条,则可以使用path picture。(请注意,如果需要,可以将其概括为允许任意倾斜角度。)

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{pgfplots}
\usetikzlibrary{calc,decorations.pathmorphing,patterns}
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,
    auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                      {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}
        }
                    {\pgfqpoint{-\pgfdecorationsegmentaspect
                     \pgfdecoratedinputsegmentremainingdistance}%
                               {\pgfmathresult\pgfdecorationsegmentamplitude}
                    }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}
\tikzset{manual pattern/.style={manual pattern keys/.cd,#1,
    /tikz/path picture={
    \path
    let \p1=($(path picture bounding box.north east)-(path picture bounding box.south west)$),
    \n1={scalar(int(1+(\x1+\y1)/\pgfkeysvalueof{/tikz/manual pattern keys/distance}))}
    in foreach \z in {0,...,\n1}
    {($(path picture bounding box.south west)+(0,-\x1+\z*\pgfkeysvalueof{/tikz/manual pattern keys/distance})$)
     edge[/tikz/manual pattern keys/lines] ++ (\x1,{\x1})
    };
}},manual pattern keys/.cd,distance/.initial=5pt,
    lines/.style={draw}}
\begin{document}
\begin{preview}
    \begin{tikzpicture}[decoration=penciline, decorate]
        \begin{axis}[
            ybar interval,
            xticklabel=
            \pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
        ]
            \addplot+[hist={bins=3},fill=none,
                postaction={manual
                pattern={lines/.style={decorate,decoration=random steps}}}]
            table[row sep=\\,y index=0] {
            data\\
            1\\ 2\\ 1\\ 5\\ 4\\ 6\\
            7\\ 6\\ 9\\ 8\\ 5\\ 2\\
            };
        \end{axis}
    \end{tikzpicture}
\end{preview}

\begin{preview}
    \begin{tikzpicture}[decoration=penciline, decorate]
        \begin{axis}[
            ybar interval,
            xticklabel=
            \pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
        ]
            \addplot+[hist={bins=3},fill=none,
                postaction={manual
                pattern={lines/.style={decorate}}}]
            table[row sep=\\,y index=0] {
            data\\
            1\\ 2\\ 1\\ 5\\ 4\\ 6\\
            7\\ 6\\ 9\\ 8\\ 5\\ 2\\
            };
        \end{axis}
    \end{tikzpicture}
\end{preview}
\end{document}

在此处输入图片描述

上面的图片只是为了证明它确实有效,第二张图片使用了代码中的笔画线,但是要仔细观察才能发现线条有点不规则。

此版本可与较新版本配合使用,pgfplots并允许您设置距离和角度。

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc,decorations.pathmorphing,patterns}
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,
    auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                      {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}
        }
                    {\pgfqpoint{-\pgfdecorationsegmentaspect
                     \pgfdecoratedinputsegmentremainingdistance}%
                               {\pgfmathresult\pgfdecorationsegmentamplitude}
                    }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}
\tikzset{manual pattern/.style={manual pattern keys/.cd,#1,
    /tikz/path picture={
    \pgfmathsetmacro{\mybeta}{Mod(270+\pgfkeysvalueof{/tikz/manual pattern keys/angle},180)-90}
    \pgfmathtruncatemacro{\myt}{\mybeta<-45?0:(\mybeta<0?1:(\mybeta<45?2:3))}
    \def\myxshift{0pt}
    \def\myyshift{0pt}
    \ifcase\myt
     \pgfmathsetmacro{\mydelta}{abs(cos(\mybeta))}
     \pgfmathsetmacro{\myd}{1/abs(sin(\mybeta))}
     \def\mystart{path picture bounding box.north west}
     \def\myxshift{-\y1*\mydelta+\z*\pgfkeysvalueof{/tikz/manual pattern keys/distance}*\myd}
    \or 
     \pgfmathsetmacro{\mydelta}{abs(sin(\mybeta))}
     \pgfmathsetmacro{\myd}{1/abs(cos(\mybeta))}
     \def\mystart{path picture bounding box.north west}
     \def\myyshift{\x1*\mydelta-\z*\pgfkeysvalueof{/tikz/manual pattern keys/distance}*\myd}
    \or 
     \pgfmathsetmacro{\mydelta}{abs(sin(\mybeta))}
     \pgfmathsetmacro{\myd}{1/abs(cos(\mybeta))}
     \def\mystart{path picture bounding box.south west}
     \def\myyshift{-\x1*\mydelta+\z*\pgfkeysvalueof{/tikz/manual pattern keys/distance}*\myd}
    \or 
     \pgfmathsetmacro{\mydelta}{abs(cos(\mybeta))}
     \pgfmathsetmacro{\myd}{1/abs(sin(\mybeta))}
     \def\mystart{path picture bounding box.south west}
     \def\myxshift{-\y1*\mydelta+\z*\pgfkeysvalueof{/tikz/manual pattern keys/distance}*\myd}
    \fi
    \path
    let \p1=($(path picture bounding box.north east)-(path picture bounding box.south west)$),
    \n1={scalar(int(1+(\x1+\y1)/\pgfkeysvalueof{/tikz/manual pattern keys/distance}))},
    \n2={max(\x1,\y1)*sqrt(2)}
    in foreach \z in {0,...,\n1}
    {([xshift=\myxshift,yshift=\myyshift]\mystart)
     edge[/tikz/manual pattern keys/lines] ++ (\mybeta:\n2)
    };
}},manual pattern keys/.cd,distance/.initial=5pt,angle/.initial=45,
    lines/.style={draw}}
\begin{document}

\begin{preview}
    \begin{tikzpicture}[decoration=penciline, decorate]
        \begin{axis}[
            ybar interval,
            xticklabel=
            \pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
        ]
            \addplot+[hist={bins=3},fill=none,
                postaction={manual pattern={angle=30,lines/.style={decorate}}}]
            table[row sep=\\,y index=0] {
            data\\
            1\\ 2\\ 1\\ 5\\ 4\\ 6\\
            7\\ 6\\ 9\\ 8\\ 5\\ 2\\
            };
        \end{axis}
    \end{tikzpicture}
\end{preview}

\begin{preview}
    \begin{tikzpicture}[decoration=penciline, decorate]
        \begin{axis}[
            ybar interval,
            xticklabel=
            \pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
        ]
            \addplot+[hist={bins=3},fill=none,
                postaction={manual pattern={angle=-30,distance=3pt,
                    lines/.style={decorate}}}]
            table[row sep=\\,y index=0] {
            data\\
            1\\ 2\\ 1\\ 5\\ 4\\ 6\\
            7\\ 6\\ 9\\ 8\\ 5\\ 2\\
            };
        \end{axis}
    \end{tikzpicture}
\end{preview}

\end{document}

在此处输入图片描述

相关内容