2

I have a file with histogram which I want to draw. Here's my code:

\documentclass{report}

\usepackage{pgfplots} % for histograms

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ height = 0.4\paperheight
                , width = 0.7\paperwidth
                ]

        \addplot [ybar interval, color = black, fill = blue, line width = 0.5mm] 
            table {hist1.data};
    \end{axis}
\end{tikzpicture}

\end{document}

Can I set x and y ticks to values in file? Like that (dots are not required): example

File "hist1.data":

 1 0.06
 3 0.23
 5 0.35
 7 0.2
 9 0.1
 11 0.04
 13 0.01
 15 0
 17 0
 19 0.01
 21 0

I'm sorry if it is duplicated question, but I can't find the answer.

1 Answer 1

2

If you set ytick=data,xtick=data, the ticks will be placed at the y- and x-values used in the first \addplot. And with extra y ticks={0,0.05,...,0.35} you get the default ticks as well. (Of course with different data, you have to change the values. The two numbers before ... is the first two values in the list, and the step is the difference between them. The list stops at the number after ....)

The filecontents stuff is only used to make the example self-contained, you don't need it for your own use, as you already have the hist1.data file.

The yticklabel style is used to get 0.06 instead of 6\cdot10^{-2} as the ticklabel.

output of code

\documentclass{report}
\usepackage{pgfplots} % for histograms
\usepackage{filecontents}
\begin{filecontents*}{hist1.data}
 1 0.06
 3 0.23
 5 0.35
 7 0.2
 9 0.1
 11 0.04
 13 0.01
 15 0
 17 0
 19 0.01
 21 0
\end{filecontents*}
\begin{document}

\begin{tikzpicture} 
\begin{axis}[
  height = 0.4\paperheight,
  width = 0.7\paperwidth,
  extra y ticks={0,0.05,...,0.35},
  ytick=data,xtick=data,
  yticklabel style={
   /pgf/number format/fixed,
   /pgf/number format/precision=2,
  }
]

 \addplot [ybar interval, color = black, fill = blue, line width = 0.5mm] 
            table {hist1.data};
\end{axis}
\end{tikzpicture}
\end{document}
2
  • And if you don't mind, can I ask one more question? Is it possible to save old ticks and draw "ytick=data" at the same time? In any case thank you very much! Commented Nov 21, 2017 at 20:11
  • 1
    @ВячеславРадько See updated answer. Commented Nov 21, 2017 at 21:30

You must log in to answer this question.

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