具有两个纵坐标轴的绘图的 X 轴不连续性 - 不在 pgfplots 手册中

具有两个纵坐标轴的绘图的 X 轴不连续性 - 不在 pgfplots 手册中

尽管在 pgfplots 手册中多个纵坐标和轴的不连续性相邻出现,但在同一图中实现两者却很困难。

具体来说,我有以下带有两个 y 轴的 pgfplots 图(参见下面的 MWE)

带有 2 个 y 轴的绘图

我希望 x 轴不连续,将 x 轴分成 2 个区间:第一个区间从 (-4,-3) 开始,第二个区间从 (0,4) 开始。我希望按照此问题的答案的样式保留图形的边框邮政,其 y 轴上存在不连续性。我尝试更新它,但没有成功,我不确定如何将答案调整为具有双 y 轴的图。

任何帮助都将受到赞赏。

平均能量损失

\documentclass[tikz]{standalone}
\usepackage{pgf,pgfplots}
\pgfplotsset{compat=newest}
\begin{document} 

\begin{tikzpicture}
\pgfplotsset{set layers}
\begin{axis}[scale only axis,
xlabel={$x$},
ylabel={Ordinate 1},
axis y line*=left,      
xmin=-4.0, xmax=4.0
]                   
\addplot[red] {x^2}; \label{plot_one}
\end{axis}

\begin{axis}[scale only axis,
ylabel={Ordinate 2},
xmin=-4.0, xmax=4.0, 
axis y line*=right,
axis x line=none,   
]                       
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addplot[blue] {x}; \label{plot_two}
\addplot[green] {3*x};  \label{plot_three}
\addlegendentry{plot 2}
\addlegendentry{plot 3} 
\end{axis}


\end{tikzpicture}
\end{document}

答案1

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture}
\pgfplotsset{set layers}
\begin{axis}[
title={A confusing and unnecessary plot},
scale only axis,
xlabel={$x$},
ylabel={Ordinate 1},
axis y line*=left,      
xmin=-4, xmax=2,
xticklabels={-4,-4,-3,0,1,2,3,4},
x axis line style= { draw=none}
]                   
\addplot[
  red, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {x^2}; \label{plot_one}
  \path[-] (rel axis cs:0,0)     coordinate(botstart)
          --(rel axis cs:0.2,0)coordinate(interruptbotA)
         (rel axis cs:0.3,0)  coordinate(interruptbotB)
         --(rel axis cs:1,0)   coordinate(botstop);
         \path[-] (rel axis cs:0,1)     coordinate(topstart)
         --(rel axis cs:0.7,1) coordinate(interrupttopA)
         (rel axis cs:0.87,1)  coordinate(interrupttopB)
         --(rel axis cs:1,1)   coordinate(topstop);
\end{axis}
\draw(botstart)-- (interruptbotA) decorate[decoration=zigzag]{--(interruptbotB)} -- (botstop);
\draw(topstart) -- (topstop);
\begin{axis}[scale only axis,
ylabel={Ordinate 2},
xmin=-4.0, xmax=2, 
axis y line*=right,
axis x line=none,   
]                       
\addlegendimage{/pgfplots/refstyle=plot_one}
\addlegendentry{plot 1}
\addplot[blue, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {x}; \label{plot_two}
\addplot[green, samples=101,
  unbounded coords=jump,
  x filter/.expression={x<-3? x: (x>0? x-2: nan)},
  ] {3*x};  \label{plot_three}
\addlegendentry{plot 2}
\addlegendentry{plot 3} 
\end{axis}
\end{tikzpicture}
\end{document}

破碎的情节

从这里取的不连续轴:https://tex.stackexchange.com/a/386663/8650

答案2

这是我所能做到的。我尝试使用,axis x discontinuity但没有办法将其放置在正确的位置。它想移动到 0 的右侧,或者xmax如果为负数则移动到左侧。

我还遇到了一些与轴环境和移动相关的错误。如果你将轴放入范围并移动范围,则唯一移动的就是图例。如果你移动轴,边界框不会移动。

\documentclass{standalone}
\usepackage{pgf,pgfplots}
\pgfplotsset{compat=newest}
\begin{document} 

\begin{tikzpicture}
\pgfplotsset{set layers}% ???
\begin{axis}[scale only axis, width=1cm, height=4cm,
xlabel=\vphantom{$x$},
ylabel={Ordinate 1},
axis y line*=left,      
xmin=-4.0, xmax=-3.0,
ymin=0.0, ymax=16.0,
xtick={-4,-3}
]                   
\addplot[red] {x^2}; \label{plot_one}
\coordinate (SE) at (rel axis cs: 1,0);
\coordinate (NE) at (rel axis cs: 1,1);
\end{axis}

\begin{axis}[scale only axis, width=1cm, height=4cm,
xmin=-4.0, xmax=-3.0, 
axis y line=none,
axis x line=none,
ymin=-12, ymax=12.0,
]                       
\addplot[blue] {x};
\addplot[green] {3*x};
\end{axis}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{axis}[xshift=2cm,
scale only axis, width=4cm, height=4cm,
xlabel={$x$},
axis y line=none,      
xmin=0.0, xmax=4.0,
ymin=0.0, ymax=16.0,
name=right
]                   
\addplot[red] {x^2}; \label{plot_one}
\coordinate (SW) at (rel axis cs: 0,0);
\coordinate (NW) at (rel axis cs: 0,1);
\end{axis}
\begin{axis}[xshift=2cm,
scale only axis, width=4cm, height=4cm,
ylabel={Ordinate 2},
xmin=0.0, xmax=4.0, 
axis y line*=right,
axis x line=none,
ymin=-12, ymax=12.0,
]                       
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addplot[blue] {x}; \label{plot_two}
\addplot[green] {3*x};  \label{plot_three}
\addlegendentry{plot 2}
\addlegendentry{plot 3} 
\end{axis}

\path (7cm,0);% kludge to fix bounding box
\draw[red] (SE) -- (SW);
\draw[red] (NE) -- (NW);

\end{tikzpicture}
\end{document}

相关内容