在乳胶中缩放/聚焦图表的某些部分

在乳胶中缩放/聚焦图表的某些部分

我正在使用 pgfplot 在 latex 中绘制图形。现在我面临的问题是:

\begin{tikzpicture}[scale=1]
\begin{axis}[ybar,grid=major,bar width=2pt,width=8cm, height=8cm,xmin=1,xmax=540,ymin=1,ymax=240]
\addplot
[draw=black,fill=green]
 table [x=size, y=cluster, col sep=comma] {sim-0.4-0.6.csv};
\end{axis}
\end{tikzpicture}

它生成以下图表:

在此处输入图片描述

由于 x 轴上的值主要在 0-50 之间,因此我想将其放大。之后,值会达到 500 左右。因此,50 - 450 之间没有图点。我该怎么做?

答案1

我找到了一种解决方法这里并对其进行了一些修改,以便它看起来更符合您的要求。

这当然不是完美的,因为你必须从数据中减去一个偏移量,并且要对其进行大量调整。由于我以前需要这样的东西,所以我仍然认为这样做是可以的,因为该x discontinuity功能不允许你在任意位置插入断点,而只能在另一个轴旁边插入。

编辑:再调整一下,extra x tick labels并添加负间距以减小白框的大小。现在结果看起来好多了,但实现方法却不是这样。

\documentclass{standalone}
\usepackage{tikz,pgfplots,color,amsmath}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[ybar,
    xtick distance =10,
    xmin=0,xmax=110,width=10cm,
    xticklabels={,0,10,20,30,40,50,,500,510,520,530,540,550},
    extra x ticks={60},
    extra x tick style={tick label style={yshift=1.6em}},
    extra x tick labels={\colorbox{white}{$ \mkern-14mu||\mkern-8mu$}}
    ]
\addplot+[draw=black,fill=green] table { 
    x y
    10 11
    20 15
    50 20
};
\addplot+[draw=black,fill=green] table[x expr=\thisrow{x}-430,] {
    x y
    530 25
    520 15
    500 10
    };
    \end{axis}
    \end{tikzpicture}
\end{document}

截屏

答案2

除了模拟不连续的轴之外,您还可以绘制仅显示图的一部分(“放大”)的插图。

有关解决方案如何运作的更多详细信息,请查看代码中的注释。

(请注意,我创建了一个虚拟函数来显示一些数据,因为在写这个答案时 CSV 文件不可用。)

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % enlare the axis limits
        % (as it was requested in your other question
        %  <http://tex.stackexchange.com/q/358199>)
        enlargelimits=true,
        grid=major,
        % I use this so it easier to set the two plots in a good ratio
        % (because then the labels don't count to the `width' and `height')
        scale only axis,
        % now list the options that are in common for both plots
        table/col sep=comma,
        %
        % these options are only for the dummy data, since the CSV file was not
        % available when writing this answer
        domain=0:450,
        samples=51,
    }
    % declare some layers and their order
    % these are needed if you want to have a background (color) behind the whole
    % inset plot (including the `ticklabels' etc.) and not only the background
    % of the "box" of the inset plot.
    % You want to do at least one of these to avoid that also in the inset plot
    % the grid lines of the "main" plot are shown/visible
    \pgfdeclarelayer{background}
    \pgfdeclarelayer{foreground}
    \pgfsetlayers{background,main,foreground}
\begin{document}
\begin{tikzpicture}
    % on the "lowest" layer, draw the normal plot
    \begin{pgfonlayer}{background}
    \begin{axis}[
        width=6cm,
        height=8cm,
        xmin=0,
        xmax=450,
        ymin=1,
        ymax=240,
%        % if you really want to "cut off" some data, you can do this with this
%        % key
%        restrict x to domain=1:1000,
    ]
%        \addplot [mark=x] table [x=size, y=cluster] {sim-0.3-0.6.csv};
        % just a dummy function to produce a plot
        \addplot {250*exp(-x/10)};

        % store a coordinate where the inset should be plotted at
        \coordinate (inset) at (axis description cs:0.95,0.95);
    \end{axis}
    \end{pgfonlayer}

    % the inset plot should be plotted on the "top" most layer
    \begin{pgfonlayer}{foreground}
    % this is the inset plot ...
    \begin{axis}[
        % ... which should be plotted at the stored coordinate ...
        at={(inset)},
        % ... with this `anchor'
        anchor=north east,
        % use this predefined style (it is predefined by PGFPlots itself)
        small,
        %
        % now state the options which should be used for the inset plot
        width=3cm,
        height=4cm,
        xmin=1,
        xmax=50,
        ymin=1,
        ymax=240,
        % use this key to fill the background of the axis only
        axis background/.style={
            fill=white,
        },
        % name this axis so it can later be used to fill the "background" of the
        % whole plot including the labels
        name=insetAxis,
    ]
%        \addplot [mark=x] table [x=size, y=cluster] {sim-0.3-0.6.csv};
        % again the dummy plot
        \addplot {250*exp(-x/10)};
    \end{axis}
    \end{pgfonlayer}

    % on the "medium" layer we draw the background of the whole inset plot
    % including the labels
    \begin{pgfonlayer}{main}
        % (I fill it with a gray here, so one can see the difference to the
        %  `axis background' result in the inset plot itself)
        \fill [black!10] ([shift={(-2pt,-2pt)}] insetAxis.outer south west)
            rectangle    ([shift={(+5pt,+5pt)}] insetAxis.outer north east);
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容