2

I am trying to center the graph plot's left side and making it as long as to reach the right margin of the page but failing to do so. I want the labels on the left to be aligned to the right and the whole right part of the page to contain the graph's plot.

Current code:

% !TeX program = xelatex
% !TeX encoding = utf8

\documentclass[a4paper,12pt]{report}
\usepackage[left=2.75cm,right=2.75cm,top=2.5cm,bottom=2.5cm,heightrounded,marginparwidth=2.5cm,marginparsep=0.25cm]{geometry}

\usepackage{polyglossia}
\usepackage{fontspec}
\PassOptionsToPackage{dvips}{graphicx}
\PassOptionsToPackage{dvipsnames,table}{xcolor}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pgfplots}

\begin{document}
        \pgfplotstableread{
            1   8
            2   8
            3   12
            4   32
            5   40
        }\dataset
    \begin{center}
        \begin{tikzpicture}[every node/.style={outer sep=0}]
            \begin{axis}[
                xbar,
                bar width=0.3cm,
                width=.5\textwidth,
                height=6cm,
                xmin=0,
                xmax=99,
                xtick pos = bottom,
                ytick=data,
                yticklabels = {
                    \strut Lorem ipsum dolor sit amet,
                    \strut Consectetur adipiscing elit,
                    \strut Quisque tincidunt,
                    \strut Lectus eu lacus gravida posuere,
                    \strut Sed eget dignissim ligula
                },
                ytick pos = left,
                yticklabel style={
                    text width=.5\textwidth,
                    align=right
                },
                major y tick style = {
                    opacity = 0
                },
                minor y tick num = 1,
                minor tick length = 2ex,
                every node near coord/.append style = {
                    anchor=west,
                    %scale=0.7,
                },
                enlarge y limits = {abs=1},
                nodes near coords={\pgfmathprintnumber\pgfplotspointmeta~\%},
                xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}~\%},
            ]
                \addplot[draw=black,fill=red!60] table[x index=1,y index=0] \dataset;
            \end{axis}
        \end{tikzpicture}

        .
    \end{center}
\end{document}

Result where you can see that the graph is not centered unlike the dot: enter image description here

I am trying for the graph to look like this:

enter image description here

  • @Bernard Apart from the typo (a square bracket gets closed by a curly bracket) I do not see why this should work. Did you try it? – Schrödinger's cat Nov 12 '19 at 23:55
  • @Schrödinger'scat: you're right. I guess I had another situation in mind. It might work only if the labels on the left were as wide as the graph itself. – Bernard Nov 13 '19 at 9:32
1

Welcome! This is rather straightforward to do with TikZ methods only:

  1. Add overlay to yticklabel style to disregard them from the bounding box.
  2. Add

    \path ([xshift=-\textwidth/2]current axis.west)
        ([xshift=\textwidth/2]current axis.west);
    

after \end{axis}.

\documentclass[a4paper,12pt]{report}
\usepackage[left=2.75cm,right=2.75cm,top=2.5cm,bottom=2.5cm,heightrounded,marginparwidth=2.5cm,marginparsep=0.25cm,
showframe]{geometry}

\usepackage{polyglossia}
\usepackage{fontspec}
\PassOptionsToPackage{dvips}{graphicx}
\PassOptionsToPackage{dvipsnames,table}{xcolor}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pgfplots}

\begin{document}
        \pgfplotstableread{
            1   8
            2   8
            3   12
            4   32
            5   40
        }\dataset
    \begin{center}
        \begin{tikzpicture}%[every node/.style={outer sep=0}]
            \begin{axis}[
                xbar,
                bar width=0.3cm,
                width=.5\textwidth,
                height=6cm,
                xmin=0,
                xmax=99,
                xtick pos = bottom,
                ytick=data,
                yticklabels = {%
                    \strut Lorem ipsum dolor sit amet,%
                    \strut Consectetur adipiscing elit,%
                    \strut Quisque tincidunt,%
                    \strut Lectus eu lacus gravida posuere,%
                    \strut Sed eget dignissim ligula%
                },
                ytick pos = left,
                yticklabel style={overlay,
                    text width=.5\textwidth,
                    align=right
                },
                major y tick style = {
                    opacity = 0
                },
                minor y tick num = 1,
                minor tick length = 2ex,
                every node near coord/.append style = {
                    anchor=west,
                    %scale=0.7,
                },
                enlarge y limits = {abs=1},
                nodes near coords={\pgfmathprintnumber\pgfplotspointmeta~\%},
                xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}~\%},
            ]
                \addplot[draw=black,fill=red!60] table[x index=1,y index=0] \dataset;
            \end{axis}
            \path ([xshift=-\textwidth/2]current axis.west)
            ([xshift=\textwidth/2]current axis.west);
        \end{tikzpicture}
    \end{center}
    \begin{tikzpicture}[overlay,remember picture]
    \draw[red] (current page.north) -- (current page.south);
    \end{tikzpicture}
\end{document}

enter image description here

The frame (showframe) and the red line (from the overlay picture at the end) are only to guide the eye.

Of course, you can shift things around by adding or subtracting some length to or from the shifts, e.g.

\documentclass[a4paper,12pt]{report}
\usepackage[left=2.75cm,right=2.75cm,top=2.5cm,bottom=2.5cm,heightrounded,marginparwidth=2.5cm,marginparsep=0.25cm,
showframe]{geometry}

\usepackage{polyglossia}
\usepackage{fontspec}
\PassOptionsToPackage{dvips}{graphicx}
\PassOptionsToPackage{dvipsnames,table}{xcolor}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pgfplots}

\begin{document}
        \pgfplotstableread{
            1   8
            2   8
            3   12
            4   32
            5   40
        }\dataset
    \begin{center}
        \begin{tikzpicture}%[every node/.style={outer sep=0}]
            \begin{axis}[
                xbar,
                bar width=0.3cm,
                width=.5\textwidth,
                height=6cm,
                xmin=0,
                xmax=99,
                xtick pos = bottom,
                ytick=data,
                yticklabels = {%
                    \strut Lorem ipsum dolor sit amet,%
                    \strut Consectetur adipiscing elit,%
                    \strut Quisque tincidunt,%
                    \strut Lectus eu lacus gravida posuere,%
                    \strut Sed eget dignissim ligula%
                },
                ytick pos = left,
                yticklabel style={overlay,
                    text width=.5\textwidth,
                    align=right
                },
                major y tick style = {
                    opacity = 0
                },
                minor y tick num = 1,
                minor tick length = 2ex,
                every node near coord/.append style = {
                    anchor=west,
                    %scale=0.7,
                },
                enlarge y limits = {abs=1},
                nodes near coords={\pgfmathprintnumber\pgfplotspointmeta~\%},
                xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}~\%},
            ]
                \addplot[draw=black,fill=red!60] table[x index=1,y index=0] \dataset;
            \end{axis}
            \path ([xshift=-\textwidth/2-1ex]current axis.west)
            ([xshift=\textwidth/2-1ex]current axis.west);
        \end{tikzpicture}
    \end{center}
    \begin{tikzpicture}[overlay,remember picture]
    \draw[red] (current page.north) -- (current page.south);
    \end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||
  • Thank you, this works really well. I added some measurements to shift the plot a little to the right (so the blank space between ylabel and the graph's left side is centered) and make the plot span to the right margin of the page (see here: i.imgur.com/c8Cs1XB.png ). Could you please update your answer how to achieve this the proper way? – AMDcze Nov 13 '19 at 23:20
  • @AMDcze That's very easy, you only need to modify the shifts. I added an example. – Schrödinger's cat Nov 14 '19 at 0:00
  • And to make it span to the right margin of the page? I used width=.6\textwidth-0.345em, but that is not really the proper way. =) – AMDcze Nov 14 '19 at 7:39

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.