我花了整个下午的时间来绘制一个函数,正如你在这张图上看到的那样:
我以为这不会太难。但我错了。我已经向谷歌寻求帮助,我只知道,我必须使用tikz
。现在我得到的答案真是太可耻了:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\draw[->] (-0.2,0) -- (5.5,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,5.5) node[right] {$u_1$};
\end{tikzpicture}
\end{document}
对此我深感抱歉,但我不知道。有人能帮我吗?任何回复我都非常感谢!!
PS:函数由 2*x-1 和 -2*x+1 描述
答案1
正如 Ignasi 所建议的,解决方案是pgfplots
...它并不像纯 TikZ 解决方案那么简单,但是它基于我对类似您的问题的回答并保留了绘画风格(如果这很重要):
\documentclass[border=3mm,multi]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=8cm, compat=1.13}
\begin{document}
\begin{tikzpicture}[
every pin edge/.style={<-,draw=black}
]
\begin{axis}[
axis y line=left, % <-- left y axis
ylabel style={at={(0,1)},rotate=-90,anchor=south},
ylabel=$u_1$,
ytick={0, 1},
yticklabels={$-1$, $1$},
%
axis x line=bottom, % <--
xlabel style={at={(1,0)},anchor=west},
xlabel=$x$,
xtick={0, 1},
xticklabels={$0$, $1$},
%
domain=0:1,
xmin=0,xmax=1.1,ymin=0,ymax=1.1,
clip=false
]
\addplot[draw=none] {x}; % <-- dummy line,
% instead it can be drawn some function
% (this is not clear from question)
%
\draw[very thick, draw=red, fill=red!30]
(0,0) -- (0.5,0.5) -- coordinate[pin=above right:{$u(x,R)$}] (ur) (1,0);
\draw[very thick]
(0,1) -- (0.5,0.5) -- coordinate[pin=below right:{$u(x,L)$}] (ul) (1,1);
%
\draw[dashed] (0,1) -| (1,0);
\draw[dashed,draw=red]
(0,.50) node[left] {$V=0$} -| (0.5,0) node[below] {$\frac{1}{2}$};
\end{axis}
\end{tikzpicture}
\end{document}
生成的图像:
编辑:
除了在 中绘制线条外pgflots
,还可以使用addplot
宏来执行此操作:
\begin{axis}[ .... ]
\addplot[very thick, draw=red, fill=red!30]
coordinates {(0,0) (0.5,0.5) (1,0)};
\addplot[very thick, draw=black]
coordinates {(0,1) (0.5,0.5) (1,1)};
\node[pin=below right:{$u(x,L)$}] at (0.75,0.75) {};
\node[pin=above right:{$u(x,R)$}] at (0.75,0.25) {};
%
\draw[dashed] (0,1) -| (1,0);
\draw[dashed,draw=red]
(0,.50) node[left] {$V=0$} -| (0.5,0) node[below] {$\frac{1}{2}$};
\end{axes}
如果文档中有更多pgfplots
图片,您可以定义常用预设,然后在特定图片中调用这些预设。这样,图片代码就会变得更加简洁(请参阅我关于绘图集)
答案2
TiKZ
含有 的纯净物(不含)的溶液pgfplots
:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{amsmath}
\usepackage{tikz}
%\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[->] (0,-1) -- (1.5,-1) node[right] {$x$};
\draw[->] (0,-1) -- (0,1.5) node[above] {$u_1$};
\draw[dashed] (0,-1) rectangle (1,1);
\draw (0,1) node[left] {1} -- coordinate (aux) coordinate[near end](aux2) (1,-1) node[below] {1};
\draw (0,-1) node[left] {-1} node[below]{0} --coordinate[pos=0.7] (aux3) (1,1);
\filldraw[thick, red, opacity=.5] (0,-1)--(aux)--(1,-1)--cycle;
\draw[red, thick,dashed] (0,0) node[left] {$V=0$} -| (0.5,-1) node[below]{$\frac{1}{2}$};
\draw[<-] (aux2) --++(15:1cm) node[right]{$u(x,R)$};
\draw[<-] (aux3) --++(-15:1cm) node[right]{$u(x,L)$};
\end{tikzpicture}
\end{document}