问题

问题

问题

我想在和x轴上添加轴不连续性y,但这样做(即使只有一个轴)也会将我的整个图向右移动。

平均能量损失

代码

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}

% Colour
\usepackage[cmyk]{xcolor}

% Graphing and math
\usepackage{pgfplots}
\pgfplotsset{%
    compat=1.18,
    axis lines=left,
    axis line style={semithick},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel style={at=(current axis.above origin), anchor=south east, rotate=-90, yshift=-1.5mm},
    ticklabel style={font=\scriptsize},
    clip mode=individual,
    width=6.8cm,
    anchor={outer north west}
}
\usetikzlibrary{decorations.pathreplacing, intersections}
\usepgfplotslibrary{fillbetween}

% Float placement
\usepackage{float}

% Dummy text
\usepackage{lipsum}


\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\lipsum[1]

% Don't worry about the janky looking fill, it's just a modified test version. Although, if there was a way to hide it from clipping through the axis discontinuity lines, that would be greatly appreciated!
\begin{figure}[H]
    \centering
    \begin{tikzpicture}[baseline]
        \begin{axis}[
                        title={Before tax},
                        xlabel={\( Q \)},
                        ylabel={\( P \)},
                        ymin=20, ymax=32,
                        xmin=40, xmax=57,
                        domain={0:\pgfkeysvalueof{/pgfplots/xmax}},
                        declare function={%
                            supply(\P)=2.05*\P;
                        },
                        axis y discontinuity=crunch  % <--- Problematic for the plot
                    ]
            \addplot[blue, thick, name path=supplycurve] ({supply(x)}, x) node[above right, pos=0.475, xshift=-3pt, yshift=4pt] {\( Q_S \)} node[blue!15!white, below right, pos=0.4075, xshift=3pt, yshift=-3pt, opacity=1, font=\tiny] {PS};

            \node[] (equilibriumpoint) at (axis cs:50,25) {};

            \path[name path=separator] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},25) -- (equilibriumpoint);
            \draw[black, dashed, opacity=0.25] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},25) -| (axis cs:50,\pgfkeysvalueof{/pgfplots/ymin});

            \addplot[blue, fill opacity=0.15] fill between[of=separator and supplycurve, soft clip={domain=40:50}];
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

图片)

无轴不连续性

该图没有明确放置轴不连续标记,因此没有问题。

由于轴线不连续

该图明确放置了轴不连续标记,因此其放置很混乱。

答案1

您的图中有几个奇怪的内容。造成问题的主要原因是将其与图中的 y 值domain={0:\pgfkeysvalueof{/pgfplots/xmax}},结合使用。x

当 PGFPlot 改变其计算边界框的方式以包含紧缩因素时,它还包括超出轴的长线性图。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=40, xmax=57,
ymin=20, ymax=32,
axis lines=left,
axis y discontinuity=crunch,
declare function={supply(\P)=2.05*\P;},
]
\addplot[
blue, thick,
domain={\pgfkeysvalueof{/pgfplots/ymin}:\pgfkeysvalueof{/pgfplots/ymax}},
] ({supply(x)}, x);
\end{axis}
\end{tikzpicture}
\end{document}

具有紧密边界框的图形

答案2

回答

在查阅了相关pgfplots文档之后,我发现了似乎可以完成这项工作的tikz风格。trim axis left

/tikz/trim axis left

一种有价值的风格trim left=(current axis.south west)

需要将样式作为参数提供给\begin{tikzpicture}[trim axis left]。它要求图片中至少有一个PGFPLOTS环境。效果是修剪最后一个轴的锚点左侧的所有内容south west(即左轴边界左侧的所有内容)。

   -PGFPLOTS手动的

结果

代码

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}

% Colour
\usepackage[cmyk]{xcolor}

% Graphing and math
\usepackage{pgfplots}
\pgfplotsset{%
    compat=1.18,
    axis lines=left,
    axis line style={semithick},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel style={at=(current axis.above origin), anchor=south east, rotate=-90, yshift=-1.5mm},
    ticklabel style={font=\scriptsize},
    clip mode=individual,
    width=6.8cm,
    anchor={outer north west}
}
\usetikzlibrary{decorations.pathreplacing, intersections}
\usepgfplotslibrary{fillbetween}

% Float placement
\usepackage{float}

% Dummy text
\usepackage{lipsum}


\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\lipsum[1]

% Don't worry about the janky looking fill, it's just a modified test version. Although, if there was a way to hide it from clipping through the axis discontinuity lines, that would be greatly appreciated!
\begin{figure}[H]
    \centering
    \begin{tikzpicture}[baseline, trim axis left]  % <--- This is what did the trick!
        \begin{axis}[
                        title={Before tax},
                        xlabel={\( Q \)},
                        ylabel={\( P \)},
                        ymin=20, ymax=32,
                        xmin=40, xmax=57,
                        domain={0:\pgfkeysvalueof{/pgfplots/xmax}},
                        declare function={%
                            supply(\P)=2.05*\P;
                        },
                        axis y discontinuity=crunch
                    ]
            \addplot[blue, thick, name path=supplycurve] ({supply(x)}, x) node[above right, pos=0.475, xshift=-3pt, yshift=4pt] {\( Q_S \)} node[blue!15!white, below right, pos=0.4075, xshift=3pt, yshift=-3pt, opacity=1, font=\tiny] {PS};

            \node[] (equilibriumpoint) at (axis cs:50,25) {};

            \path[name path=separator] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},25) -- (equilibriumpoint);
            \draw[black, dashed, opacity=0.25] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},25) -| (axis cs:50,\pgfkeysvalueof{/pgfplots/ymin});

            \addplot[blue, fill opacity=0.15] fill between[of=separator and supplycurve, soft clip={domain=40:50}];
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

图像

已修复图片。

相关内容