How can I make a bar chart with custom x axis range which is from 100 to 0 then to 100?

The figure should look like this:

enter image description here

(Figure is in https://doi.org/10.1145/2971648.2971676)

Also, how to make the neutral bar align its middle with 0?

This answer is somehow close, but cannot do the above: Add single bar to a stacked bar plot

  • Welcome to TeX.SX. Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. Apr 11, 2018 at 13:32
  • I have tried to modify the one in my link "Add single bar to a stacked bar plot". That's the MWE.
    – C Luo
    Apr 11, 2018 at 14:36

1 Answer 1


Here I present a solution that should be very near your requirements. For details please have a look at the comments in the code.

% used PGFPlots v1.16
    % load PGFPlots ColorBrewer library for adequate colors/an adequate `cycle list'
        % use this `compat' level or higher to use the advanced positioning of
        % the axis labels
        % add the small font size also to the legend entries
        small/.append style={
            legend style={
        % define a custom style which is used for both `axis' environments
        my axis style/.style={
            % use the (appended) `small' style
            % set axis with and height
            % set axis x limits
            % use stacked xbars ...
            xbar stacked,
            % ... where the negative values are plotted separately
            stack negative=separate,    % <-- default
            % align xticks outside ...
            xtick align=outside,
            % ... and only on the lower axis line
            xtick pos=lower,
            % don't show yticks
            % at x = 0 stuff should be handled differently
            extra x ticks={0},
            extra x tick style={
                grid style={
            % rotate the ylabel (back)
            ylabel style={
            % draw the gridlines on top of the bars
            axis on top,
            % define the style of the legend
            legend style={
                legend columns=3,
            legend cell align=left,
            % this loads a proper colormap which contains the colors we want to use
            % Now we create the cycle list on our own.
            % Because bars are drawn from 0 on, we need to change the order of
            % colors of the above loaded `cycle list'; first the 3 negative bars
            % and then the 3 positive bars
            cycle list={
                {index of colormap={2},fill=.,draw=.},
                {index of colormap={1},fill=.,draw=.},
                {index of colormap={0},fill=.,draw=.},
                {index of colormap={2},fill=.,draw=.},
                {index of colormap={3},fill=.,draw=.},
                {index of colormap={4},fill=.,draw=.},
            % since we have only one bar per `axis' environment and no y value
            % we can state an arbitrary value here
            table/y expr=0,
        % state the values for the different categories
        %   index 0: strongly disagree
        %   index 1: disagree
        %   index 2: (negative) neutral
        %   index 3: (positive) neutral
        %   index 4: agree
        %   index 5: strongly agree
            0 0 -5 3 56 37
            0 -10 -10 8 45 30
        % load the created style
        my axis style,
        % misuse the `ylabel'
        % don't show `xticklabels' on the upper plot
        % list the legend entries
        legend entries={
            strongly disagree,
            neutral,,               % <-- show neutral only once
            strongly agree%
        % give the upper `axis' a (node) name
        % to get the right order of legend entries with the right corresponding
        % colors, we need to provide proper `\addlegendimage' commands
        \pgfplotsinvokeforeach {0,1,2,0,3,4} {
            \addlegendimage{index of colormap={#1},fill=.,draw=.}

        % now we add the bars in a proper order
        \foreach \i in {2,1,0,3,4,5} {
            \addplot table [x index=\i] {\TableOne};
        my axis style,
        % position the "lower" `axis' environment properly, relative to the
        % "upper" `axis' environment ...
        at={(upper.below south)},
        % ... and state a proper `anchor' for the "lower" `axis' environment
        % only show positive values as `xticklabels'
            \pgfmathparse{ifthenelse(\tick < 0, -\tick, \tick)}
        xlabel style={
        \foreach \i in {2,1,0,3,4,5} {
            \addplot table [x index=\i] {\TableTwo};

image showing the result of above code

  • Very good answer. I upvote you.
    – Sebastiano
    Apr 11, 2018 at 22:25
  • Amazing. Thanks! Any idea to remove the minus sign on the left side of x axis?
    – C Luo
    Apr 12, 2018 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .