TransWikia.com

ybar interval width ignored

TeX - LaTeX Asked on January 3, 2022

Following this solution for custom xtick, I set ybar interval in the plot rather than in the axis, but the relative width option (0.7) seems to be ignored.

  1. How can I set the width? (It may be best not to, but I want to experiment.)
  2. Why do I keep getting an "overfull hbox" warning.
documentclass{article}
usepackage{pgfplots}
usepgfplotslibrary{dateplot}
pgfplotsset{compat=1.17}

begin{filecontents}{data.txt}
    month   count
    1998-01-01  487
    1998-02-01  421
    1998-03-01  459
    1998-04-01  509
    1998-05-01  496
    1998-06-01  479
    1998-07-01  396
    1998-08-01  451
    1998-09-01  441
    1998-10-01  574
    1998-11-01  532
    1998-12-01  472
    1999-01-01  413
    1999-02-01  426
    1999-03-01  467
    1999-04-01  483
    1999-05-01  383
    1999-06-01  368
    1999-07-01  410
    1999-08-01  410
    1999-09-01  393
    1999-10-01  437
    1999-11-01  445
    1999-12-01  401
    2000-01-01  428
    2000-02-01  404
    2000-03-01  478
    2000-04-01  489
    2000-05-01  393
    2000-06-01  391
    2000-07-01  414
    2000-08-01  420
    2000-09-01  392
    2000-10-01  460
    2000-11-01  466
    2000-12-01  405
    2001-01-01  0
end{filecontents}

begin{document}

begin{tikzpicture}
begin{axis}[
        width=5in,
        height=3in,
        date coordinates in=x,
        xtick={1998-01-01, 1999-01-01, 2000-01-01},
        ybar,
        xticklabel=year,
        ymin=0,
        date ZERO=1998-01-01,
]
addplot+ [
        ybar interval=0.7,
] table [x=month] {data.txt};
end{axis}
end{tikzpicture}

end{document}

One Answer

When you do addplot [ybar interval].. you get /tikz/ybar interval, which does not have an option for the relative width. But when you do begin{axis}[ybar interval] you get /pgfplots/ybar interval which does have the relative width option. When added to the axis options, that also affects the ticks, which is the reason for using addplot [ybar interval] in the linked post I think.

You can do addplot +[/pgfplots/ybar interval=0.7] table [x=month] {data.txt}; which lets you use the relative width setting, without modifying the ticks. Whether there are any drawbacks to this I don't know.

Regarding the width, my comment on your question was a bit inaccurate. Actually the plot is narrower than textwidth, but the tikzpicture is indented with the standard paragraph indentation, which moves it right by 15pt. Adding noindent before begin{tikzpicture} will remove the indentation, but perhaps more appropriate to use a center environment?

In the example below I added the showframe package to indicate the borders of the text block, you'll want to remove that. I also added the enlarge x limits setting, to reduce the whitespace inside the axis a bit.

enter image description here

documentclass{article}
usepackage{pgfplots,showframe}
usepgfplotslibrary{dateplot}
pgfplotsset{compat=1.17}

begin{filecontents}{data.txt}
    month   count
    1998-01-01  487
    1998-02-01  421
    1998-03-01  459
    1998-04-01  509
    1998-05-01  496
    1998-06-01  479
    1998-07-01  396
    1998-08-01  451
    1998-09-01  441
    1998-10-01  574
    1998-11-01  532
    1998-12-01  472
    1999-01-01  413
    1999-02-01  426
    1999-03-01  467
    1999-04-01  483
    1999-05-01  383
    1999-06-01  368
    1999-07-01  410
    1999-08-01  410
    1999-09-01  393
    1999-10-01  437
    1999-11-01  445
    1999-12-01  401
    2000-01-01  428
    2000-02-01  404
    2000-03-01  478
    2000-04-01  489
    2000-05-01  393
    2000-06-01  391
    2000-07-01  414
    2000-08-01  420
    2000-09-01  392
    2000-10-01  460
    2000-11-01  466
    2000-12-01  405
    2001-01-01  0
end{filecontents}

begin{document}

begin{center}
begin{tikzpicture}
begin{axis}[
        width=5in,
        height=3in,
        date coordinates in=x,
        xtick={1998-01-01, 1999-01-01, 2000-01-01},
        ybar,
        xticklabel=year,
        ymin=0,
        date ZERO=1998-01-01,
        enlarge x limits=0.03
]
addplot +[/pgfplots/ybar interval=0.7] table [x=month] {data.txt};
end{axis}
end{tikzpicture}
end{center}
end{document}

Answered by Torbjørn T. on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP