我如何用 tikzpicture 绘制分段函数(如下所示)?我可以绘制任何关于 x 的函数,但我不知道变量 y 是什么?提前谢谢
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{color}
\usepackage{xcolor}
\begin{document}
\begin{cases}
1 & \mbox{,if } y<\frac{1}{4} \\
$[0,1]$ & \mbox{,if } y=\frac{1}{4} \\
0 & \mbox{,if}\, \, y>\frac{1}{4}.
\end{cases} %it is the function that i want to draw.
\\ \\
\begin{tikzpicture}[scale=1.0]
\begin{axis} [xtick={0,0.1,...,1.1},ytick={0,0.1,...,1.1},xlabel=$x$, ylabel=$y$,ymin=-0.1,ymax=1.1,xmax=1.1,xmin=-0.001,axis on top=true,axis x line=middle,
axis y line=middle, width=10cm, grid=major,legend pos=outer north east
]
\addplot [yellow, line width=3,variable=\y,smooth,domain=1/2:1] {1};
\addplot [yellow, line width=3,variable=\x,smooth,domain=0:1] {1};
\addplot [yellow, line width=3,variable=\y,smooth,domain=0:1/2] {1};
\end{axis}
\end{tikzpicture}
\end{document}
这是我目前所做的
答案1
像这样?
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
Tick/.style = {fill=white, inner sep=1pt, font=\scriptsize}
]
% axes
\draw[draw=gray,thin,-Straight Barb] (-4,0) -- (4,0) node[below left] {$x$};
\draw[draw=gray,thin,-Straight Barb] (0,-1) -- (0,2) node[below left] {$y$};
% function
\draw[draw=red,thick] ( -3,1) -- (0.5,1)
(0.5,0) -- (3,0);
\draw[red,densely dashed,thin] (0.5,1) -- + (0,-1);
% ticks
\foreach \i in {-1,-0.5,0, 0.25, 1}
\draw[thin] (2*\i,0) -- + (0,-0.1) node[Tick, below] {$\i$};
\node[Tick, left] at (-1mm,1) {1};
\end{tikzpicture}
\end{document}
附录: 看起来函数是 x = f(y) ...
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
Tick/.style = {fill=white, inner sep=1pt, font=\scriptsize}
]
% axes
\draw[draw=gray,thin,-Straight Barb] (-1,0) -- (3,0) node[below left] {$x$};
\draw[draw=gray,thin,-Straight Barb] (0,-1) -- (0,2) node[below left] {$y$};
% function
\draw[draw=red,thick] (0,1.5) -- + (0,-1)
(2,0.5) -- + (0,-1);
\draw[red,densely dashed,thin] (0,0.5) -- + (2,0);
% ticks
\node[Tick,left] at (-0.1, 0.5) {$\frac{1}{4}$};
\foreach \i in {0,1}
\node[Tick,below] at (2*\i,-0.1) {\i};
\end{tikzpicture}
\end{document}