1

I would want to plot a 2D horizontal histogram, with ybar interval, xlabel and x tick label rotate by 45 degrees. I would to put symbolic y coords and fill all white. How can I represent it?

I've tried this code:

\begin{tikzpicture}[font=\small]
\begin{axis}[
    width=15cm,
    height=15cm,
    xbar interval,
    bar width=5pt,
    xmin=0.05,
    xmax=0.2,
    xlabel = Interest,
    axis x line=bottom,
    axis y line=left,
    enlargelimits=0.15,
    symbolic y coords={sport,tennis,basketball,government,aid,charity,catastrophe,epidemic,terrorism,automobile},
    ytick = data,
    % y tick label style={rotate=45, anchor=east},
 ]
    \addplot[fill=white] coordinates { 
        (0.15285340939543077,sport) 
        (0.09540517832166039,tennis) 
        (0.08404651315963803,basketball)
        (0.08381236920515132,automobile)
        (0.09480098304371283,government) 
        (0.09153312501031691,aid) 
        (0.08189990416034557,charity) 
        (0.092913400554364,catastrophe) 
        (0.08754789307155249,epidemic)
     };
\end{axis}
\end{tikzpicture}

and I got this result: result

Where have I to make any changes?

Regards.

  • welcome to tex.se!. for start see 4.5.4 Bar Plots on , page 81 in pgfplots manual. then provide complete but small document whit your diagram that we can help you. – Zarko Apr 4 '18 at 13:08
  • Thank you @Zarko. I've tried to read that paragraph, and I've inserted the result I can obtain in the first message. It's not a good result! – Federico Cuozzo Apr 4 '18 at 13:22
  • you should provide complete document :-)! beginning with \documentclass{...} and ending with \end{document} in preamble let be only to your diagram relevant packages. – Zarko Apr 4 '18 at 13:28
  • Why xbar interval and not xbar? Is it on purpose that you don't have a data point for terrorism? – Torbjørn T. Apr 4 '18 at 13:30
1

It's a bit hard to say what you should change, because I'm not entirely sure about what you're after. However, it seems to me that xbar interval is the wrong choice here, that xbar is better. I changed some other settings as well, see the comments in the code.

If you do want rotated yticklabels, the code you already had works just fine, though I wouldn't use it.

enter image description here

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
    width=12cm, % changed size a bit
    height=8cm,
    xbar, % not xbar interval
    bar width=12pt,
    xmin=0, % does it make more sense to start at zero?
    xmax=0.17, % reduced this a bit
    xlabel = Interest,
    axis x line=bottom,
    axis y line=left,
    enlarge y limits=0.15, % no need to enlarge x limits
    symbolic y coords={sport,tennis,basketball,government,aid,charity,catastrophe,epidemic,terrorism,automobile},
    ytick = data,
%    y tick label style={rotate=45, anchor=east}, % I wouldn't use this
    xticklabel style={
      /pgf/number format/fixed % no scientific notation for the smallest values
    }
 ]
    \addplot[fill=white] coordinates { 
        (0.15285340939543077,sport) 
        (0.09540517832166039,tennis) 
        (0.08404651315963803,basketball)
        (0.08381236920515132,automobile)
        (0.09480098304371283,government) 
        (0.09153312501031691,aid) 
        (0.08189990416034557,charity) 
        (0.092913400554364,catastrophe) 
        (0.08754789307155249,epidemic)
     };
\end{axis}
\end{tikzpicture}
\end{document}
1

You mix a lot of things up, that is why you get such a strange result. For details on how to make it better, please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[font=\small]
    \begin{axis}[
        width=15cm,
        height=15cm,
        xbar interval,
        bar width=5pt,
        xmin=0.05,
        xmax=0.2,
        xlabel=Interest,
        axis x line=bottom,
        axis y line=left,
        enlargelimits=0.15,
        symbolic y coords={
            sport,
            tennis,
            basketball,
            government,
            aid,
            charity,
            catastrophe,
            epidemic,
            terrorism,
            automobile,
            dummy,          % <-- needed to prevent an error message (see below)
        },
        ytick=data,
        % y tick label style={rotate=45, anchor=east},
        % (just show labels in "fixed" notation)
        xticklabel style={
            /pgf/number format/fixed,
        },
    ]
        \addplot[fill=white] coordinates {
            % resorted entries to match the `symbolic y coords'
            (0.15285340939543077,sport)
            (0.09540517832166039,tennis)
            (0.08404651315963803,basketball)
            (0.09480098304371283,government)
            (0.09153312501031691,aid)
            (0.08189990416034557,charity)
            (0.092913400554364,catastrophe)
            (0.08754789307155249,epidemic)
            (0,terrorism)   % <-- added, so bars all have the same width
            (0.08381236920515132,automobile)
            (0,dummy)       % <-- added, so also the last bar is drawn
         };
    \end{axis}
\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.