2

In this figure two of my x-labels overlap with functions. I would like to either move the labels to the top or perhaps give them a white box? Not sure what looks better.

How can I do that?

 \documentclass[letter,11pt,twoside]{article}
    \usepackage{tikz}
    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \usepackage{stmaryrd}
    \usepackage{array,booktabs}
    \usepackage{placeins}
    \pgfplotsset{
        compat=newest,
        xlabel near ticks,
        ylabel near ticks,
        myplot/.style={smooth,tension=0.5,mark=none,very thick}
    }    
    \begin{document}
\begin{figure}[h]
\begin{tikzpicture}[
labelnode/.style={font=\footnotesize, above},%
  labelline/.style={stealth-stealth,shorten >=0.1pt, shorten <=0.5pt}%
  ]%
\begin{axis}[%
 width=0.95\textwidth,height=0.4\textheight,%
 axis x line=center,%
 xmin=0,%
 xmax=0.8,%
 xtick={0.2,0.22,0.458,0.70},%
 xticklabels={{$c_1$},{$c_2$},{$c_{S_2}$},{$w_{S_2}^*$}},%
 axis y line=left,%
 ymin=-75,%
 ymax=85,%
 ytick={0},%
 yticklabels={{0}},%The ** looks odd there - I know. but otherwise it clashes into c_S_2; I think we can keep it as is. Not an error, but on purpose ;)
 ylabel={$\Delta\left(E\left[\pi\right]\right)$},%
 xlabel={$c_{S_1}$},%
 label style={at={(axis description cs:1,0.46)},anchor=north},
    y label style={at={(axis description cs:-0.06,0.95)},rotate=-90,anchor=south},
]% 
% Retailer
\addplot+[color=black,myplot, thick]%price
 coordinates {%
 (0, 70.)  (0.34, -45.42384424965933)  (0.7000000000000001, 0)
}%
 ;%
  % S1
\addplot+[color=gray,myplot,thick]%
 coordinates {%
  (0, -70.)  (0.31, 25.52000000000001)  (0.45, 0)  
} 
 ;%

\draw[densely dotted] (0.70, -75) -- (0.70,85);%
\draw[densely dotted] (0.22, -75) -- (0.22,85);%
\draw[densely dotted] (0.2, -75) -- (0.2,85);%
\end{axis}%
\end{tikzpicture}%
{Effect of financial distress on expected profits\label{delta}} 
\end{figure}
    \end{document}
Stefan Pinnow
  • 29,535
Paul
  • 551
  • 2
  • 8
  • 1
    To move the tick labels to the top, use axis x line=right. They will still overlap with each other, though. What do you want to do about that? –  Jan 29 '20 at 16:13

1 Answers1

1

Since you didn't say (yet) how to deal with the two overlapping x tick labels c1 and c2, I'll improvise. I've added pins to avoid the overlapping problem, used clip=false to ensure these pins are visible, improved the placement and typesetting of the mathematical expression attached to the y axis:

$\operatorname{\Delta}\bigl(\operatorname{E}[\pi]\bigr)$

(using \operatorname from amsmath to get appropriate spacing and an upright font), removed unused things—including a lot of unnecessary end-of-line percent signs—, placed a legend box ten points left of the vertical line with equation x = wS2*, used every axis plot/.append style={myplot} to avoid repeating the myplot style for each plot and removed the + from \addplot+ since:

  • we don't want any mark to be used for the plots here, and

  • we specify the color of each plot ourselves.

Also, the title wasn't in an appropriate place, this looked very bad. I have added it to the figure in a standard way:

\caption{Effect of financial distress on expected profits}

You'll probably want to add:

\label{whatever-you-want}

immediately after this line (not before!). Finally, I have set compat=1.16 instead of compat=newest in the argument of \pgfplotsset in order to ensure a reproducible behavior.

With the x axis at the bottom

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\pgfplotsset{
  compat=1.16,
  myplot/.style={smooth, tension=0.5, mark=none, thick}
}

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
      width=0.95\textwidth, height=0.4\textheight,
      axis x line=bottom,
      xmin=0,
      xmax=0.8,
      xtick={0.2, 0.22, 0.458, 0.70},
      xticklabels={,, $c_{S_2}$ , $w_{S_2}^*$},
      axis y line=left,
      ymin=-75,
      ymax=85,
      ytick={0},
      yticklabels={0},
      ylabel={$\operatorname{\Delta}\bigl(\operatorname{E}[\pi]\bigr)$},
      label style={at={(axis description cs:1,0.46)},anchor=north},
      y label style={at={(axis description cs:0,0.95)},
                     rotate=-90, anchor=south east},
      legend entries={Price, $S_1$},
      legend style={at={({axis cs:0.70,0} |- 0,0.95)}, anchor=north east,
                    outer xsep=10pt},
      clip=false,
      every axis plot/.append style={myplot},
      ]
      % Retailer
      \addplot[color=blue!40] coordinates { % price
        (0, 70.)  (0.34, -45.42384424965933)  (0.7000000000000001, 0)
      }
      ;
      % S1
      \addplot[color=red!40] coordinates {
        (0, -70.)  (0.31, 25.52000000000001)  (0.45, 0)
      };

      \node [pin=-100:$c_1$] at (0.2,0  |- {axis description cs:0,0}) {};
      \node [pin=-80:$c_2$]  at (0.22,0 |- {axis description cs:0,0}) {};

      \draw[densely dotted] (0.70, -75) -- (0.70,85);
      \draw[densely dotted] (0.22, -75) -- (0.22,85);
      \draw[densely dotted] (0.2, -75) -- (0.2,85);
    \end{axis}
  \end{tikzpicture}%
  \caption{Effect of financial distress on expected profits}
\end{figure}

\end{document}

x axis at bottom

With the x axis at the top

The code is the same except that you have to replace axis x line=bottom with axis x line=top and

\node [pin=-100:$c_1$] at (0.2,0  |- {axis description cs:0,0}) {};
\node [pin=-80:$c_2$]  at (0.22,0 |- {axis description cs:0,0}) {};

with

\node [pin=100:$c_1$] at (0.2,0  |- {axis description cs:0,1}) {};
\node [pin=80:$c_2$]  at (0.22,0 |- {axis description cs:0,1}) {};

x axis on top

frougon
  • 24,283
  • 1
  • 32
  • 55