2

With the amazing help of this community, I draw the picture below some months ago. Back then, I was very happy with it; but as time goes by, I want my figures to look better and better. As you can see in the picture, some lines exceed the margin of the axes; and I of course want to avoid that. I have seen in this community that the answer is to use the \clip option of TikZ. I have checked the TIkZ Documentation and I have been playing with that option for a while now, but with no success at all. Does anybody know how to force those lines to stick inside the axes defined by the plot? The picture is below:

The MWE that generates such a picture is the following:

\documentclass[]{standalone}
% PACKAGES LOADING
\usepackage[dvipsnames]{xcolor} % To get many colours.
\usepackage{amsmath, mathtools, amsthm, mathrsfs, amssymb} % MATH Packages
\usepackage{tikz} % To plot almost everything.
% TIKZ SETTING
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}

\begin{document}
\begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0:1}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1},}, my axis/.style={latex-latex}, scale=1.5]
\draw[thick, smooth,color=Red,] (0,1) -- (1,1);
\draw[thick, smooth,color=Green,] (0,1.5) -- (1,1.5);
\draw[my plot, color=Blue] (0,0) plot (\x,{(3*\x)});
\coordinate (start plot) at (0,{(0)});
\coordinate (end plot) at (1.5,{(4)});
\draw[my plot, color=Orange, shift={(0,3)},] (0,0) plot (\x,{(-3*\x)});
\draw[my axis] ([shift={(-0cm,0cm)}]start plot |- end plot) node[above] {$H_1$} |- node[coordinate](origin){} ([shift={(0cm,-0cm)}]start plot -| end plot) node[right] {$q$};
\def\x{0.5}\def\y{4}\def\p{0.55}
\coordinate (a) at (0.0,{(0.0)});
\coordinate (b) at (1,{(3)});
\coordinate (c) at (0,{(1)});
\coordinate (d) at (0,{(1.5)});
\coordinate (e) at (1,{(1)});
\coordinate (f) at (1,{(1.5)});
\coordinate (g) at (1,{(0.25)});
\coordinate (h) at (1,{(2.75)});
\draw[my grid] (a) |- node[below,font=\scriptsize]{$0$} (origin) |- node[left,font=\scriptsize]{$0$} cycle;
\draw[my grid] (b) |- node[below,font=\scriptsize]{$1$} (origin) |- node[left,font=\scriptsize]{$3$} cycle;
\draw[my grid] (c) |- node[left,font=\scriptsize]{$1$} cycle;
\draw[my grid] (d) |- node[left,font=\scriptsize]{$1.5$} cycle;
\draw[my grid] (e) |- node[right,font=\scriptsize]{$\textcolor{Red}{H_1(B,q)=1}$} cycle;
\draw[my grid] (f) |- node[right,font=\scriptsize]{$\textcolor{Green}{H_1(\sigma_1,q)=1.5}$} cycle;
\draw[my grid] (g) |- node[right,font=\scriptsize]{$\textcolor{Orange}{H_1(M,q)=3(1-q)}$} cycle;
\draw[my grid] (h) |- node[right,font=\scriptsize]{$H_1(T,q)=3q$} cycle;
\clip(0,0) rectangle (1.5,4);
\end{tikzpicture}
\end{document}

And the picture is:

enter image description here

Then, my question is: how do I force the lines to stay inside the plot?

BONUS: Does anybody know what is generating the abnormalities of the vertical grid (gray dotted line)?

PS: I am aware that the code of the figure can probably be improved; I just used other figures I had to create this one because I am new to TikZ. Also, it may look like a duplicate, but I don't think it is because I found no way of properly using the \clip option to get what I desire.

EDIT: Since I have several to many figures with the same issue; I'd really appreciate general solutions; though any help will of course be greatly appreciated.

Thank you all very much for your time!

  • 1
    pgfplots would be much better at this – percusse Apr 19 '17 at 18:58
  • @percusse Thank you for your comment. Would you mind elaborating a little bit further your point, please? Why do you think so? – Héctor Apr 19 '17 at 19:00
  • 1
    axis preperation and plotting are all natively done via pgfplots. You only concentrate on the labels and nodes the rest is done automatically by the axis mechanism which you can skip and greatly reduce your code – percusse Apr 19 '17 at 19:07
  • Thank you; good to know (for me and for others that may see this question in the future). I'll keep it mind. – Héctor Apr 19 '17 at 19:36
3

You have to clip before drawing and define the clipping inside a scope unless you want to apply to the rest of the figure. In this case, move your functions inside a scope like:

\begin{scope}
\clip (0,0) rectangle (1,3);
\draw[my plot, color=Orange, shift={(0,3)},] (0,0) plot (\x,{(-3*\x)});
\draw[my plot, color=Blue] (0,0) plot (\x,{(3*\x)});
\end{scope}

enter image description here

  • Thank you: just perfect. Any clue on the BONUS question, though? – Héctor Apr 19 '17 at 17:12
  • 1
    @Hector probably because you \draw[my grid] too many times. In fact I don't very much understand why do you place the label with such a complicated syntax, (but this is another question). Try to replace all \draw[my grid] with \path and add a command like \draw[my grid] (0,3)-|(1,0); and look if it's better. – Ignasi Apr 19 '17 at 18:54
  • Thank you very much again. What you proposed did the trick and the grid now looks fine. Also, regarding the complicated syntax... You are totally right. It's unnecessary; but it's a relatively old plot and I haven't updated the syntax according to my current command of LaTeX (I must admit, though, that I myself have lately drawn similar plots to this one and I have used a way more simple syntax to place the labels). Thank you for pointing it out. I'll try to reduce and simplify the syntax. – Héctor Apr 19 '17 at 19:41

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.