如何限制 TiKZ 中函数的范围

如何限制 TiKZ 中函数的范围

如何限制以下 tikz 图片上的 y 值,以便函数包含在框中?我尝试将其“剪切”成矩形,但这使其难以格式化,也难以在图形周围写标题。有什么建议吗?

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}

答案1

@Zarko 已经展示了一种非常好的方法pgfplots。这是一种稍微手动一点的方法,pgfplots需要单独绘制每条线。公平地说,这并不太繁琐,而且可以让您轻松控制不同的颜色、线条样式,尤其是您想要与线条平行的这些自定义标签的位置,我猜?

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

pgfplots简单...通过对图表进行稍微的重新设计,您可以获得:

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}

附录: 要仅绘制虚线网格线(如提供的图片所示),您可以extra x tick使用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

在这两种情况下我都建议使用图例。在我看来,战斗清楚地表明了曲线的参数。

答案3

没有 pgfplots,而是\clip使用倾斜标签的样式,即您不必手动输入旋转角度和位置。

\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

相关内容