1

I'm trying to make a piecewise function graph with pgfplots that looks something like this...

Finite Well

I wish to have the horizontal (x-axis) and vertical (y-axis) labeled in a generic way, with ±a and V_0 on their respective relative positions. I haven't seen any examples in the documentation with arbitrary parameters on the x or y axis; is this possible? Thanks in advance for any savvy advice.

\documentclass[border=3pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\pgfmathdeclarefunction{MyFunction}{1}{%
  \pgfmathparse{%
(and(   0,    #1<-1)*(0)            +%
(and(#1>=-1,  #1< 1)*(-1)   +%
(and(#1>= 2,    0  )*(0)%
}%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
axis lines=middle, 
domain=-2:2,
samples=100,
xmin=-2, xmax=2,
ymin=-2, ymax=2
]

\addplot[blue, ultra thick] {MyFunction(x)};

\end{axis}
\end{tikzpicture}
\end{document}

The output: enter image description here

I wish for the y-value of -1 to read -V_0, and the x-values of +-1 to read +-a.

  • Welcome on TeX.SX. Do you mean something like xticklabels or xticklabel (section 4.15 in the manual (v1.14) on page 334)? – Stefan Pinnow Sep 4 '16 at 4:49
  • Stefan: I do mean something exactly like that, but I am unsure of how to, for lack of a better word, 'tie' the label on the axis to my piecewise function. – logical123 Sep 4 '16 at 5:02
  • Edited with some actual code now. Hope that helps! – logical123 Sep 4 '16 at 6:37
  • Do you want just what you show in your first image, or do you want the axes as well? Is this the only thing in the figure, or are you going to add more plots in the same axis? The reason I'm asking is that if you just want the image in your first image, then it would be better to use pure TikZ, pgfplots is the wrong tool for the job I think. I'll add an answer in a bit to show you what I mean. – Torbjørn T. Sep 4 '16 at 9:21
1

If you want to generate a diagram just like the image you show, then I'd drop using pgfplots altogether, and just pure TikZ. There are many ways of doing it, here is one.

enter image description here

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[yscale=2]
\draw (-2,0) -| coordinate (a) (-1,-1) coordinate (b) -| coordinate (c) (1,0) coordinate (d) -- (2,0);
\node [below left] at (b) {$-a$};
\node [below right] at (c) {$a$};
\path (b) -- node[below] (V0) {$-V_0$} (c);
\path (a) -- node[pos=0.7,left] (i) {I} (b);
\node (iii) at (i-|c) [right] {III};
\node at (i-|V0) {II};
\end{tikzpicture}
\end{document}

For fun, here's a version using just one path:

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[yscale=2,every node/.style={minimum height=3ex}]
\draw (-2,0) -|  node[left,pos=0.85] (i) {I}
 (-1,-1) 
  node[below left] (a1) {$-a\strut$}
 -| 
  node[pos=0.25,below] (V0) {$-V_0\strut$}
  node[below right] (a2) {$a\strut$}
  node[right,pos=0.65] (iii) {III}
 (1,0) -- (2,0)
  node at(i-|V0) {II};
\end{tikzpicture}
\end{document}
  • I don't know how I overlooked this solution back all that time ago, this is much cleaner, and avoiding pgfplots is great. This is now my preferred method, thanks! – logical123 Aug 11 '17 at 16:55
1

So this is what you are looking for?

(Please note that I haven't tried to avoid the overlapping of the -V_0 with the graph because I am not sure if this is the final graph you are working on. I doubt that a bit because then showing much +y values doesn't make sense and also the axis labels don't really make sense when labeling in this way.)

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{calc}
    \pgfmathdeclarefunction{MyFunction}{1}{%
        \pgfmathparse{%
            (and(     0, #1<-1)*(0)  +%
            (and(#1>=-1, #1< 1)*(-1) +%
            (and(#1>= 2,     0)*(0)%
        }%
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % put 'x' and 'y' in math mode
            xlabel={$x$},
            ylabel={$y$},
            %
            axis lines=middle,
            % -----
            % original code
            domain=-2:2,
            samples=100,
%            % alternatively you can use `samples at' and provide only the
%            % samples that are (really) necessary
%            samples at={-2,-1.00001,-0.9999,0.9999,1.00001,2},
            % -----
            xmin=-2, xmax=2,
            ymin=-2, ymax=2,
            %
            % added tick positions + their corresponding labels
            xtick={-1,1},
            xticklabels={$-a$,$+a$},
            ytick={-1,1},
            yticklabels={$-V_{0}$,$+V_{0}$},
            %
            % uncomment following key to see to put axis "on foreground"
%            axis on top,
        ]
            \addplot[blue, ultra thick] {MyFunction(x)};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • This is exactly what I was looking for. I labeled the y-axis potential, and changed the domain and min/max's to change the proportions, and it looks great. Thanks! – logical123 Sep 4 '16 at 18:27
  • A small suggestion: samples at={-2,-1.00001,-0.9999,0.9999,1.00001,2} instead of samples=100. – Torbjørn T. Sep 5 '16 at 22:06
  • @TorbjørnT., you are absolutely right with your suggestion, when the function in the question is the function robringstad needs in his final plot. As I already stated in my answer I am not totally sure about that and so I didn't change stuff like that. Nonetheless I added it commented to my code. – Stefan Pinnow Sep 9 '16 at 17:46

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.