4

How do I limit the y values on the following tikz picture, so that the functions are contained in the box? I tried 'clipping' it into a rectangle, but this makes it hard to format it, and to write captions around the figure. Any tips?

enter image description here

\begin{figure}[h]
\centering
\begin{minipage}{0.48\textwidth}
\begin{tikzpicture}[scale=1.3]
\foreach \a/\Col in {0.25/green,3/red, 1/black}
{
\draw[\Col] plot[domain=0:4, range=0:4,variable=\x,samples=90] ({\x},{0.625*(\x^(\a+1))/(0.5*(\x^\a+1))});
}
\foreach \a/\Col\dashing in {3/cyan/dashed,0.25/magenta/dashed, 1/black}
{
\draw[\Col, \dashing] plot[domain=0:4, range=0:4, variable=\x,samples=90] ({\x},{\a*(\x^2)/(0.5*(1+\a*\x)});
}
\draw (0,0) rectangle (4,4);
\draw [dotted] (1,0) node[below]{$1$} -- (1,4);
\draw [dotted] (0,1.1) node[left]{$0.5$} -- (4,1.1);
\node at (3.6,-.2) {$v_i$};
\node at (-.2,3.85) {\small MRS};
\node [text=red, rotate=26] at (2.3,3.25) {\small $\alpha=4$};
\node [text=green, rotate=28] at (2.9,2.1) {\small $\alpha=1/4$};
\node [text=cyan, rotate=13] at (1.55,1.42) {\small $a_i=4$};
\node [text=magenta, rotate=10] at (0.85,0.2) {\small $a_i=4$};
\end{tikzpicture}
\end{minipage}
\end{figure}
3
  • Please take a look at this post. Jul 28, 2018 at 22:46
  • @RuixiZhang They use clip. Are any other methods available?
    – pafnuti
    Jul 28, 2018 at 22:49
  • 1
    Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what packages etc you are using before we can compile your code.
    – user30471
    Jul 28, 2018 at 23:23

3 Answers 3

4

@Zarko has already shown a very nice way to do this with pgfplots. Here is a slightly more manual way to do it with pgfplots which involves plotting each line separately. It's not too tedious in fairness, and allows you easy control of the different colours, line styles and, in particular, the positioning of these custom labels which you want to run parallel to the lines I presume?

enter image description here

\documentclass[margin=0.5cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}    

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel={$v_i$},
    ylabel={MRS},
    every axis x label/.style={at={(current axis.right of origin)},below},
    every axis y label/.style={at={(current axis.north west)},left},
    xtick={1},
    ytick={0.5},
    xtick style={draw=none},
    ytick style={draw=none},
    no marks,
    xmin=0,xmax=4, 
    ymin=0,ymax=4,
    domain=0:4,
    samples=90
    ]

\draw [dotted] (axis cs:1,0) -- (axis cs:1,4);
\draw [dotted] (axis cs:0,0.5) -- (axis cs:4,0.5);

\newcommand{\aVal}{0.25}
\addplot [green] {0.625*(\x^(\aVal+1))/(0.5*(\x^\aVal+1))} node[green,pos=0.6,sloped,anchor=north] {$\alpha = 1/4$};
\renewcommand{\aVal}{3}
\addplot [red] {0.625*(\x^(\aVal+1))/(0.5*(\x^\aVal+1))} node[red,pos=0.6,sloped,anchor=south] {$\alpha = 4$};
\renewcommand{\aVal}{1}
\addplot [black] {0.625*(\x^(\aVal+1))/(0.5*(\x^\aVal+1))};

\newcommand{\bVal}{3}
\addplot [cyan,dashed] {\bVal*(x^2)/(0.5*(1+\bVal*x)} node[cyan,pos=0.4,sloped,anchor=south] {$a_i = 4$};
\renewcommand{\bVal}{0.25}
\addplot [magenta,dashed] {\bVal*(x^2)/(0.5*(1+\bVal*x)} node[magenta,pos=0.2,sloped,anchor=north] {$a_i = 4$};
\renewcommand{\bVal}{1}
\addplot [black] {\bVal*(x^2)/(0.5*(1+\bVal*x)};

\end{axis}
\end{tikzpicture}
\end{document}
2
  • 1
    you can define own color list and with this make code shorter. see section 4.7.7 Cycle Lists - Options Controlling Line Styles. +1 for nice answer.
    – Zarko
    Jul 29, 2018 at 0:39
  • Thanks for the info, I was trying to figure out how to make \foreach \a/\Col in {0.25/green,3/red, 1/black} work for a while, but got nowhere. Seems it is not possible, even when I tried this tex.stackexchange.com/questions/17638/…
    – Milo
    Jul 29, 2018 at 1:00
3

with pgfplots is simple ... with small redesign of your diagram you can obtain:

enter image description here

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid,
xmax=4, ymax=4,
enlargelimits=false,
legend pos=north west,
legend cell align=left,
domain=0:4,
samples=90,
no marks
            ]
\foreach \a in {0.25,3, 1}
{
\addplot  {0.625*(x^(\a+1))/(0.5*(x^\a+1))};
}
\foreach \b in {3,0.25, 1}
{
\addplot +[dashed] {\b*(x^2)/(0.5*(1+\b*x)};
}
\legend{a=0.25, a=3, a=1,
        b=3, b=0.25, b=1}
\end{axis}
\end{tikzpicture}
\end{document}

addendum: to draw only dashed grid lines as shown on provided picture you can use extra x tick and extra y tick:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid,
xtick=\empty,ytick=\empty,
extra x ticks={1,4}, extra y ticks={1,4},
extra x tick labels={1,$v_i$},
extra y tick labels={0.5, RMS},
extra tick style={dashed},
xmax=4, ymax=4,
enlargelimits=false,
legend pos=north west,
legend cell align=left,
legend style={font=\footnotesize,draw=none},
domain=0:4,
samples=90,
no marks
            ]
\foreach \a in {0.25,3, 1}
{
\addplot  {0.625*(x^(\a+1))/(0.5*(x^\a+1))};
}
\foreach \b in {3,0.25, 1}
{
\addplot +[dashed] {\b*(x^2)/(0.5*(1+\b*x)};
}
\legend{$\alpha=0.25$, $\alpha=3$, $\alpha=1$,
        $a_i=3$, $a_1=0.25$, $a_1=1$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

in both cases i suggest to use legend. In my opinion, the battle clearly indicates the parameters of the curves.

0
2

Without pgfplots but \clip instead and a style for sloped labels, i.e. you don't have to put the rotation angles nor positions by hand.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\begin{document}

\tikzset{annotate/.style args={#1,#2,#3}{postaction={decorate,decoration={markings,%
mark=at position #1-0.1 with {\coordinate (Xpfft1);},
mark=at position #1 with {\coordinate (Xpfft2);
\pgftransformreset
\path let \p1=($(Xpfft2)-(Xpfft1)$) in
\pgfextra{\pgfmathsetmacro{\myangle}{atan2(\y1,\x1)}\xdef\myangle{\myangle}};
\path (Xpfft1) -- (Xpfft2) node[rotate=\myangle,above,#2] {#3};
}}}}}
\begin{figure}[h]
\centering
\begin{minipage}{0.48\textwidth}
\begin{tikzpicture}[scale=1.3]
\draw (0,0) rectangle (4,4);
\clip (-1,-1) rectangle (4,4);
\foreach \a/\Col in {0.25/green,3/red, 1/black}
{
\draw[\Col,annotate={0.7,{text=\Col,font=\small},$\alpha=\a$}] 
plot[domain=0:4, range=0:4,variable=\x,samples=90] ({\x},{0.625*(\x^(\a+1))/(0.5*(\x^\a+1))});
}
\foreach \a/\Col\dashing in {3/cyan/dashed,0.25/magenta/dashed, 1/black}
{
\draw[\Col, \dashing,annotate={0.4,{text=\Col,font=\small},$\alpha=\a$}] plot[domain=0:4, range=0:4, variable=\x,samples=90] ({\x},{\a*(\x^2)/(0.5*(1+\a*\x)});
}
\draw [dotted] (1,0) node[below]{$1$} -- (1,4);
\draw [dotted] (0,1.1) node[left]{$0.5$} -- (4,1.1);
\node at (3.6,-.2) {$v_i$};
\node [font=\small]at (-.2,3.85) {MRS};
\end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

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