PGF 轴标签背景覆盖了轴刻度

PGF 轴标签背景覆盖了轴刻度

相关问题:轴刻度后的白色背景

我使用了很多带有笛卡尔坐标的工作表。我喜欢使用 PGF,因为它可以为我绘制函数图形。我想在这个 MWE 中更改一些小细节,包括标签背景覆盖刻度线:

当我不在标签上添加 fill=white 时,标签很难阅读,尤其是在打印时。当我添加它们时,它会遮住轴上的刻度标记。对于刻度,我通过减少内部分隔符然后移动标签来处理这个问题。有没有更简洁的方法可以做到这一点,尤其是对于轴标签?


\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=-0.2ex, fill=white, inner sep=0.1}}
\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=-0.2ex, yshift=-0.15ex, fill=white, inner sep=0.1}} %y shift makes negative signs more apparent.
\pgfplotsset{width=8cm,compat=1.9, every minor tick/.append style={very thick, minor tick length=3pt}}
\pgfplotsset{every axis plot/.append style={thick}}

\pgfplotsset{every axis label/.append style={fill=white}}
%HOW DO I MAKE THE x AND y BACKGROUNDS NOT GET IN THE WAY OF THE TICK MARKS?

\usetikzlibrary{calc}


\begin{document}


    \begin{tikzpicture}
\begin{axis}[
    grid=both,
    axis lines = middle,
    minor xtick={-10, -9, ..., 10},
    minor ytick={-10, -9, ..., 10},
    xtick={-10, -8, ..., 10},
    ytick={-10, -8, ..., 10},
    xlabel = $x$,
    ylabel = $y$,
    xmin=-10,
    xmax=10,
    ymin=-10, 
    ymax=10,
    samples=500] %increase samples to smooth plot, decrease to improve compilation time

    \addplot[color=blue, , thick] {x^2};


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

基本笛卡尔图,其中 xlabel 和 ylabel 具有白色背景,覆盖箭头和刻度

答案1

另一种方法是将轴延伸到网格外部并移动其标签。

b

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=-0.2ex, fill=white, inner sep=0.1}}
\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=-0.2ex, yshift=-0.15ex, fill=white, inner sep=0.1}} %y shift makes negative signs more apparent.
\pgfplotsset{width=8cm,compat=1.9, every minor tick/.append style={very thick, minor tick length=3pt}}

\pgfplotsset{every axis plot/.append style={thick}}         
\usetikzlibrary{calc}
    
\begin{document}        
    \begin{tikzpicture}
        \begin{axis}[
            grid=both,
            axis line style={->,shorten >=-3ex, shorten <=-2ex}, % added <<<<<<<<<<
            axis lines = middle,
            minor xtick={-10, -9, ..., 10},
            minor ytick={-10, -9, ..., 10},
            xtick={-10, -8, ..., 10},
            ytick={-10, -8, ..., 10},
            xlabel = $x$,
            xlabel style={xshift=3ex}, % added <<<<<<<<<<
            ylabel = $y$,
            ylabel style={yshift=3ex}, % added <<<<<<<<<<
            xmin=-10,
            xmax=10,
            ymin=-10, 
            ymax=10,
            samples=500] %increase samples to smooth plot, decrease to improve compilation time
            
            \addplot[color=blue, , thick] {x^2};            
            
        \end{axis}
    \end{tikzpicture}
\end{document}

这是一个变体。

d

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=-0.2ex, fill=white, inner sep=0.1}}
\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=-0.2ex, yshift=-0.15ex, fill=white, inner sep=0.1}} %y shift makes negative signs more apparent.
\pgfplotsset{width=8cm,compat=1.9, every minor tick/.append style={very thick, minor tick length=3pt}}

\pgfplotsset{every axis plot/.append style={thick}}         
\usetikzlibrary{calc}
    
\begin{document}        
    \begin{tikzpicture}
        \begin{axis}[
            grid=both,
            axis line style={->,shorten >=-2ex, shorten <=-1.5ex}, % added <<<<<<<<<<
            axis lines = middle,
            minor xtick={-10, -9, ..., 10},
            minor ytick={-10, -9, ..., 10},
            xtick={-10, -8, ..., 10},
            ytick={-10, -8, ..., 10},
            xlabel = $x$,
            ylabel = $y$,
            xlabel style={% added <<<<<<<<<<
                anchor=west,at={(ticklabel* cs:1.0)},
                xshift=1.5ex
            },
            xlabel=$x$,
            ylabel style={% added <<<<<<<<<<
                anchor=south,
                at={(ticklabel* cs:1.0)},
                yshift=1.5ex
            },
            xmin=-10,
            xmax=10,
            ymin=-10, 
            ymax=10,
            samples=500] %increase samples to smooth plot, decrease to improve compilation time
            
            \addplot[color=blue, , thick] {x^2};            
            
        \end{axis}
    \end{tikzpicture}
\end{document}




\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=-0.2ex, fill=white, inner sep=0.1}}
\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=-0.2ex, yshift=-0.15ex, fill=white, inner sep=0.1}} %y shift makes negative signs more apparent.
\pgfplotsset{width=8cm,compat=1.9, every minor tick/.append style={very thick, minor tick length=3pt}}

\pgfplotsset{every axis plot/.append style={thick}}         
\usetikzlibrary{calc}
    
\begin{document}        
    \begin{tikzpicture}
        \begin{axis}[
            grid=both,
            axis line style={->,shorten >=-2ex, shorten <=-1.5ex}, % added <<<<<<<<<<
            axis lines = middle,
            minor xtick={-10, -9, ..., 10},
            minor ytick={-10, -9, ..., 10},
            xtick={-10, -8, ..., 10},
            ytick={-10, -8, ..., 10},
            xlabel = $x$,
            ylabel = $y$,
            xlabel style={% added <<<<<<<<<<
                anchor=west,at={(ticklabel* cs:1.0)},
                xshift=1.5ex
            },
            xlabel=$x$,
            ylabel style={% added <<<<<<<<<<
                anchor=south,
                at={(ticklabel* cs:1.0)},
                yshift=1.5ex
            },
            xmin=-10,
            xmax=10,
            ymin=-10, 
            ymax=10,
            samples=500] %increase samples to smooth plot, decrease to improve compilation time
            
            \addplot[color=blue, , thick] {x^2};            
            
        \end{axis}
    \end{tikzpicture}
\end{document}

你可以通过使网格变浅来完全不使用白色 grid style={draw=gray!20},

埃

相关内容