1

I would like to be able to define the bar width of a bar chart based on the x-axis scale. For example, if x is between 0 and 4, I would like to set the bar width as 1 (one-quarter of the x-axis).

2 Answers 2

1

Since your question isn't 100% sure (at least to me) I hope that you are searching for something like the following.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat` level or higher so also absolute `bar width` values are allowed
    % (otherwise values without units will be interpreted in `pt`s.)
    \pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}[
    /pgf/declare function={
        xmin = 0;
        xmax = 4;
        % change the `3.5` to your needs
        BarWidth = ifthenelse(xmax-xmin > 3.5, 1, 0.5);
    },
]
    \begin{axis}[
        xmin=xmin,
        xmax=xmax,
        ybar,
        enlarge x limits={abs=0.5},
        bar width=BarWidth,
    ]
        \addplot table {
            x   y
            0   0.5
            1   1
            2   2
            3   3
            4   4
            5   5
        };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • Thank you Stefan, you got exactly what I wanted. I would like to ask you another thing. Is it possible to define a horizontal bar shift based on the x-axis length. In practice I have a stacked grouped plot and what I do is changing the position of the column by this coomand \addplot[ybar, bar shift=-0.420cm]. I would like to define the shift not in cm but based on the x-axis units. Do you know a way? Thank you lot
    – Fly
    Mar 15, 2021 at 7:22
  • 1
    You are welcome. I am sorry, but also this question is not 100% clear to me. But of course there is no problem in asking a follow-up question. Please be specific when asking your question and optimally add a sketch to your image where you indicate what result you expect. Mar 15, 2021 at 17:08
0

Here A follow-up question. I would like also to define the horizontal shift of the bar based on the x-axis length and not in centimeters so that I can perfectly overlap the edges of the column

\documentclass[10pt]{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tikzscale}


\pgfplotsset{compat=1.15}

\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] coordinates{(0.75,0) (1.5,0) (2.25,0)};
}
\makeatother


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    /pgf/declare function={
    xmin = 0.25;
    xmax = 2.75;
    BarWidth = ifthenelse(xmax-xmin > 2.5, 0.25, 0.25);},
    ybar stacked,
    legend pos=north west,
    legend style={row sep=4pt},
    xtick pos=left,
    ytick pos=left,
    ybar=1pt,
    bar width=BarWidth,
    scale only axis=true, 
    xticklabels={AA,BB,CC},
    xtick={0.75,1.5,2.25},   
    xticklabel style={text height=2ex},
    xmin=0.25, xmax = 2.75,
    ymin=0]
    
    \addplot[ybar,fill=yellow, bar shift=-0.420cm] coordinates {
        (0.75,1)
        (1.5,1)
        (2.25,1)}; 
    \addplot[ybar,fill=red, bar shift=-0.420cm] coordinates {
        (0.75,2)
        (1.5,2)
        (2.25,1)};
    \addplot[ybar,fill=blue, bar shift=-0.420cm] coordinates {
        (0.75,3)
        (1.5,3)
        (2.25,3)};    
        
    \resetstackedplots      
   
    \addplot[ybar,fill=yellow, bar shift=+.420cm] coordinates {
        (0.75,3)
        (1.5,3)
        (2.25,3)}; 
    \addplot[ybar,fill=red, bar shift=+.420cm] coordinates {
        (0.75,3)
        (1.5,3)
        (2.25,3)};
    \addplot[ybar,fill=blue, bar shift=+.420cm] coordinates {
        (0.75,1)
        (1.5,1)
        (2.25,1)};       

\end{axis}
\end{tikzpicture}


\end{document}

enter image description here

You must log in to answer this question.

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