1

I am using pgfplot to draw graphs in latex. Now this is the problem I am facing:

\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}

It generates the following graph:

enter image description here

Since, I have values mainly between 0-50 on x-axis, I want to zoom there. After that, values comes around 500. S0, there are no graph points between 50 - 450. How should I do that?

2
2

I found a workaround approach here and adapted it a little so it would look more like your request.

This is certainly not perfect, since you have to substract an offest from your data and are tweeking it a lot. As I needed something like that before at some point I still think it's okay to do it like this since the x discontinuity feature doesn't allow you to insert the break at an arbitrary position rather than just next to the other axis.

edit: Fiddled some more with the extra x tick labels and added negative spacing to reduce the size of the white box. The result looks better now, the way to do it isn't though.

\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}

screenshot

3

Besides simulating a discontinuous axis you could also draw an inset plot which shows only a ("zoomed") part of the plot.

For more details on how the solution works, please have a look at the comments in the code.

(Please note that I have created a dummy function to show some data, because the CSV file was not available when writing this answer.)

% 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}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.