2

I would like to raise the left Tick over the x-axis so it doesn't clip into the graph. However If I add a second "Extra x ticks" definition the first definition gets overwritten. How can I achieve an individual tickstyle for each tick. This question is related however it doesn't work for three different tickstyles. (See comments in the answer)

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

\begin{document}
\begin{figure}[tb]
\begin{tikzpicture}[baseline] \begin{axis}[
   ylabel style={rotate=-90},
   xtick = 0,
   ytick = 0,
   xtick style={draw=none},
   xmin = -5,
   xmax = 5,
   ylabel={$I$},
   xlabel={$U$},
   extra x ticks       = {-2,2},
   extra x tick labels = {$ -E_{ \text{t}} $, $ E_{ \text{t2}} $},
   extra x tick style  = { grid = major },
axis y line=middle,
axis x line=middle,
]
\addplot[blue,samples=21]  
        coordinates {  
            (-5,-5)
            (-2,-1)
            (2,1)
            (5,5)
            };
\end{axis}%
\end{tikzpicture}%
\end{figure}
\end{document}

enter image description here

1 Answer 1

2

In the answer to the linked question, you will see xtick is used for positive values of x, while extra x ticks is for the negative values of x. You need to do something similar here. (I removed a couple unnecessary packages as well.)

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[tb]
\begin{tikzpicture}[baseline] \begin{axis}[
   ylabel style={rotate=-90},
   xtick       = {2},
   xticklabels = {$ E_{ \mathrm{t2}} $},
   grid = major,
   ytick = 0,
   xmin = -5,
   xmax = 5,
   ylabel={$I$},
   xlabel={$U$},
   extra x ticks       = {-2},
   extra x tick labels = {$ -E_{ \mathrm{t}} $},
   extra x tick style  = { grid = major,xticklabel style={yshift=0.5ex, anchor=south}
   },
axis y line=middle,
axis x line=middle,
]
\addplot[blue,samples=21]  
        coordinates {  
            (-5,-5)
            (-2,-1)
            (2,1)
            (5,5)
            };
\end{axis}%
\end{tikzpicture}%
\end{figure}
\end{document}

enter image description here

Edit:

One way to get a third tick that is different from the other two is to define a second set of axes with the same limits as the first, and then add the ticks to the new axes.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[tb]
\begin{tikzpicture}[baseline] 
\begin{axis}[
   ylabel style={rotate=-90},
   xtick       = {2},
   xticklabels = {$ E_{ \mathrm{t2}} $},
   grid = major,
   ytick = \empty,
   xmin = -5,
   xmax = 5,
   ylabel={$I$},
   xlabel={$U$},
   extra x ticks       = {-2},
   extra x tick labels = {$ -E_{ \mathrm{t}} $},
   extra x tick style  = { grid = major,xticklabel style={yshift=0.5ex, anchor=south}
   },
axis y line=middle,
axis x line=middle,
]
\addplot[blue]
        coordinates {  
            (-5,-5)
            (-2,-1)
            (2,1)
            (5,5)
            };
\end{axis}%
\begin{axis}[
   xmin = -5,
   xmax = 5,
    xtick       = {4},
   ytick = \empty,
    xticklabels = {$ E_{ \mathrm{t4}} $},
    xticklabel style={blue,yshift=0.5ex, rotate=30},
    axis x line=middle,
    axis y line=middle,
]
\addplot[draw=none] {x};
\end{axis}

\end{tikzpicture}%
\end{figure}
\end{document}

enter image description here

5
  • Thanks, this works of course for this example but what If there was a third tick i wanted to specify? Apr 12, 2016 at 15:10
  • @miceterminator Then it should have been included in your question. How (and where) should it appear?
    – erik
    Apr 12, 2016 at 15:13
  • Well It was kind of at the end of the question. But for now this is fine (probably a duplicate) Apr 12, 2016 at 15:16
  • @miceterminator Ah, I see. It's difficult to come up with a complete solution without knowing where you want the third tick and how you want it to look. Perhaps you could edit the question to add more detail.
    – erik
    Apr 12, 2016 at 15:18
  • Thanks for showing the second solution. My main concern (and why the question is probably not a duplicate) was, that I cannot get the major grid lines AND the ticks. Your solution pretty much sums exactly up what I wanted to know. Apr 12, 2016 at 16:17

You must log in to answer this question.

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