# PSTricks: Wrong axis labels

Consider the following example:

\documentclass{article}

\def\sandsynlighed#1#2{%
\psframe[
linecolor = red,
fillstyle = solid,
fillcolor = blue!50
](#1,0)(!#1 \bredde add #2 4 div)
}

\psset{unit=0.75}

\begin{document}

\def\bredde{1.5 }
\begin{pspicture}(-0.9,-0.5)(14.3,11.3)
% Akserne.
\psaxes[
xlabelOffset = -0.75,
Ox = -1,
dx = 1.5,
xLabels = {0,1,2,3,4,5,6_{+0},6_{+1},7},
Dy = 2,
dy = 0.5
]{->}(0,0)(-0.25,-0.25)(14,10.75)[$X$,0][$100 \cdot P(X)$,90]
% Sandsynlighederne.
\sandsynlighed{0}{18.70}
\end{pspicture}

\end{document}


As can be seen, the labels on the x-axis is all wrong and I have no idea what I have done wrong. I would like to have the labels as specified by xLabels; how do I get this?

You've got conflicting directives going on for the x-axis. Try something like:

\documentclass{article}

\def\sandsynlighed#1#2{%
\psframe[
linecolor=red,
fillstyle=solid,
fillcolor=blue!50
](#1,0)(!#1 \bredde add #2 4 div)
}

\psset{unit=0.75}

\begin{document}

\def\bredde{1.5 }
\begin{pspicture}(-0.9,-0.5)(14.3,11.3)
\psaxes[
xlabelOffset=-0.75,
%%Ox=-1,
%%dx=1.5,
xLabels={0,1,2,3,4,5,6_{+0},6_{+1},7},
Dy=2,
dy=0.5
]{->}(14,10)[$X$,0][$100 \cdot P(X)$,90]
% Sandsynlighederne.
\sandsynlighed{0}{18.70}
\end{pspicture}

\end{document}


UPDATE

Given your comment, here's a fairly different approach. Here I delved into the pst-plot package to look at how labels are placed and copied some of the code. The key here is the \multido macro. Also, since the label positions are enumerated with \nA, but we're stepping by 2, I added another counter \nB to extract the correct label from \psk@xLabels. All this is wrapped up in the macro \myxlabels

To get the desired spacing, I set the xunit to a new value. Perhaps you may not want to do this globally. If so, then just reassign xunit within a group enclosing the pspicture.

Also, you don't need to add the extra space to \bredde as you did. You can use \space in the call to the post-script commands.

Here's a new MWE:

\documentclass{article}

\def\sandsynlighed#1#2{%
\psframe[
linecolor=red,
fillstyle=solid,
fillcolor=blue!50
](#1,0)(!#1 \bredde\space add #2 4 div)
}

\makeatletter
\def\myxlabels#1{\def\psk@xLabels{#1}%%'
\multido{\nA=1+2,\nB=0+1}{10}{\uput{\psxlabelsep}[-90]{\pst@xLabelsRot}(\nA,0)%%'
{\strut\pshlabel{\psPutXLabel{\nB}}}}%%'
}
\makeatother

\psset{unit=0.75,xunit=0.75}

\begin{document}

\def\bredde{2}
\begin{pspicture}[](-0.9,-0.5)(14.3,11.3)
\psaxes[
dx=2,
xLabels={,},
Dy=2,
dy=0.5
]{->}(20,10)[$X$,0][$100 \cdot P(X)$,90]
%% Sandsynlighederne.
\myxlabels{0,1,2,3,4,5,6_{+0},6_{+1},7}

\sandsynlighed{0}{18.70}
\end{pspicture}

\end{document}


• I would like the unit on the x-axis to be 1.5, and the labels and tick marks to be at the horizontal center of the bars. Aug 8 '13 at 19:31
• Super; thank you. If anyone (maybe @herbert ...) has a non-hack solution, please let me know. :) Aug 9 '13 at 14:43

the offset was not working for special label setting. Try pst-plot.tex from http://texnik.dante.de/tex/generic/pst-plot/ Then use something like:

\documentclass{article}
\def\sandsynlighed#1#2{%
\psframe[linecolor=red,fillstyle=solid,
fillcolor=blue!50](#1,0)(!#1 \bredde add #2 4 div)}

\psset{unit=0.75}
\begin{document}

\def\bredde{1.5 }
\begin{pspicture}(-0.9,-0.5)(14.3,11.3)
% Akserne.
\psaxes[
xunit=1.5,
xLabels={,0,1,2,3,4,5,6_{+0},6_{+1},7},
xlabelOffset=-0.75,
Dy=4,dy=1,
]{->}(9,10.75)[$X$,0][$100 \cdot P(X)$,90]
% Sandsynlighederne.
\sandsynlighed{0}{18.70}
\end{pspicture}

\end{document}


• Thank you, Herbert. Does this version of pst-plot.tex get to CTAN soon? If so, I'll just wait. Aug 10 '13 at 16:34
• yes, next week, hopefully ...
– user2478
Aug 10 '13 at 18:57

Here is what I ended up with; besides from incorporating the improvements of the labels in pst-plot, it is also possible to scale both axes (the width of the x-axis by changing the value of \xUnit and the units on the y-axis by changing the value of \yUnit):

\documentclass{article}

\usepackage{pst-plot}
\usepackage{siunitx}
\usepackage{xfp}

\def\displacement{\fpeval{\xUnit/2}}
\def\yunit{\fpeval{\yUnit/4}}
\def\axisHeight{\fpeval{40/\yUnit+0.4}}
\def\width{\fpeval{9*\xUnit+0.8}}

\makeatother
\def\probability#1#2{%
\psframe[
fillstyle = solid,
fillcolor = blue!50,
linecolor = red
](\fpeval{#1*\xUnit},0)(\fpeval{(#1+1)*\xUnit},\fpeval{#2/4})%
}

\begin{document}

\begin{figure}
\def\xUnit{1.1}
\def\yUnit{4}
\centering
\psset{dimen = m}
\begin{pspicture}(-1.3,-0.6)(\width,11.0)
% Axes.
\psaxes[
xunit = \xUnit,
xLabels = {0,1,2,3,4,5,6_{+0},6_{+1},7},
xlabelOffset = -\displacement,
yunit = \yunit,
dy = 1,
Dy = \yUnit
]{->}(0,0)(0,0)(9.3,\axisHeight)[$X$,0][$100 \cdot P(X)$~[\si{\percent}],90]
% Probabilities.
\probability{0}{18.70}
\probability{1}{39.83}
\probability{2}{29.87}
\probability{3}{9.958}
\probability{4}{1.532}
\probability{5}{0.1021}
\probability{6}{0.002432}
\probability{7}{0.0001667}
\probability{8}{0.000001198}
\end{pspicture}
\end{figure}

\end{document}