![在同一张图中绘制多个函数](https://linux22.com/image/471918/%E5%9C%A8%E5%90%8C%E4%B8%80%E5%BC%A0%E5%9B%BE%E4%B8%AD%E7%BB%98%E5%88%B6%E5%A4%9A%E4%B8%AA%E5%87%BD%E6%95%B0.png)
我是乳胶方面的初学者。
我想在 latex 中跟踪 2 个函数。这些函数由以下值数组定义:
X=[-1., -0.35714286 , 0.28571429 , 0.92857143 , 1.57142857 , 2.21428571 , 2.85714286 , 3.5 , 4.14285714 , 4.78571429 , 5.42857143 , 6.07142857 , 6.71428571 , 7.35714286 , 8.]
f_1(X)=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
f_2(X)=[-3.8846153846153846, -3.241758241758242, -2.598901098901099, -1.9560439560439558, -1.3131868131868127, -0.67032967032967, -0.027472527472527056, 0.6153846153846155, 1.2582417582417584, 1.901098901098902, 2.5439560439560447, 3.1868131868131884, 3.82967032967033, 4.472527472527473, 5.115384615384616]
这是我的尝试:
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pythontex}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.75,xscale=0.6,yscale=0.6]
\draw[very thinck,->](-2,0)--(10,0) node [right];
\draw[very thinck,->](0,-20)--(0,100) node [right];
\foreach \x in {-1,0,...,8} {\draw[very thick] (\x,-0.1cm) -- (\x,0.1cm) node[below] {\x};}
\foreach \y in {-10,-8,...,10} {\draw[very thick] (-0.01cm,\y) -- (0.01cm,\y) node[above] {\y};}
\draw[smooth,samples=3000,very thick,red] plot coordinates {(-1.0, 1), (-0.3571428571428571, 1), (0.2857142857142858, 1), (0.9285714285714288, 1), (1.5714285714285716, 1), (2.2142857142857144, 1), (2.8571428571428577, 1), (3.5, 1), (4.142857142857143, 1), (4.7857142857142865, 1), (5.428571428571429, 1), (6.071428571428572, 1), (6.714285714285715, 1), (7.357142857142858, 1), (8.0, 1)};
\draw[smooth,samples=3000,very thick,blue] plot coordinates {(-1.0, -3.8846153846153846), (-0.3571428571428571, -3.241758241758242), (0.2857142857142858, -2.598901098901099), (0.9285714285714288, -1.9560439560439558), (1.5714285714285716, -1.3131868131868127), (2.2142857142857144, -0.67032967032967), (2.8571428571428577, -0.027472527472527056), (3.5, 0.6153846153846155), (4.142857142857143, 1.2582417582417584), (4.7857142857142865, 1.901098901098902), (5.428571428571429, 2.5439560439560447), (6.071428571428572, 3.1868131868131884), (6.714285714285715, 3.82967032967033), (7.357142857142858, 4.472527472527473), (8.0, 5.115384615384616)};
\end{tikzpicture}
\end{center}
\end{document}
但代码无法编译。你能帮助我吗?
答案1
以下是使用原始问题中的数据(我仍然在磁盘上)的替代方法:
\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=comma]{
-1,1,-3.88461538461538,10.8768115942029
-0.35714286,1,-3.24175824175824,6.5664744158533
0.28571429,1,-2.5989010989011,3.08266784974859
0.92857143,1,-1.95604395604396,0.42539189588879
1.57142857,1,-1.31318681318681,-1.40535344572612
2.21428571,1,-0.67032967032967,-2.40956817509613
2.85714286,1,-0.0274725274725271,-2.58725229222123
3.5,1,0.615384615384616,-1.93840579710145
4.14285714,1,1.25824175824176,-0.463028689736763
4.78571429,1,1.9010989010989,1.83887902987282
5.42857143,1,2.54395604395604,4.9673173617273
6.07142857,1,3.18681318681319,8.92228630582669
6.71428571,1,3.82967032967033,13.703785862171
7.35714286,1,4.47252747252747,19.3118160307601
8,1,5.11538461538462,25.7463768115942
}\loadedtable
\begin{tikzpicture}
\begin{axis}
\addplot table[x index=0,y index=1] {\loadedtable};
\addplot table[x index=0,y index=2] {\loadedtable};
\addplot table[x index=0,y index=3] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
让我们从 MWE 开始吧:
\documentclass[12pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6, very thick]
\draw[->] (-1.5,0) -- (10,0) node[above] {$x$};
\draw[->] (0,-9.5) -- (0,10) node[left] {$y$};
\foreach \x in {-1,0,...,9} {
\ifnum\x=0\else
\draw (\x,-0.1cm) -- (\x,0.1cm) node[below] {\x};
\fi
}
\foreach \y in {-9,-8,...,9} {
\ifnum\y=0\else
\draw[very thick] (-0.1cm,\y) -- (0.1cm,\y) node[left] {\y};
\fi
}
\draw[smooth, samples=100, very thick, red] plot coordinates {
(-1.0, 1)
(-0.3571428571428571, 1)
(0.2857142857142858, 1)
(0.9285714285714288, 1)
(1.5714285714285716, 1)
(2.2142857142857144, 1)
(2.8571428571428577, 1)
(3.5, 1)
(4.142857142857143, 1)
(4.7857142857142865, 1)
(5.428571428571429, 1)
(6.071428571428572, 1)
(6.714285714285715, 1)
(7.357142857142858, 1)
(8.0, 1)
};
\draw[smooth, samples=100, blue] plot coordinates {
(-1.0, -3.8846153846153846)
(-0.3571428571428571, -3.241758241758242)
(0.2857142857142858, -2.598901098901099)
(0.9285714285714288, -1.9560439560439558)
(1.5714285714285716, -1.3131868131868127)
(2.2142857142857144, -0.67032967032967)
(2.8571428571428577, -0.027472527472527056)
(3.5, 0.6153846153846155)
(4.142857142857143, 1.2582417582417584)
(4.7857142857142865, 1.901098901098902)
(5.428571428571429, 2.5439560439560447)
(6.071428571428572, 3.1868131868131884)
(6.714285714285715, 3.82967032967033)
(7.357142857142858, 4.472527472527473)
(8.0, 5.115384615384616)
};
\end{tikzpicture}
\end{document}
一些说明:
- 设置
scale=0.6
相当于设置xscale=0.6, yscale=0.6
- 如果您想
very thick
在任何地方使用,请考虑将其设置为 的选项tikzpicture
。当然very thick
,它不是very thinck
。 - 节点需要一些内容,即使内容为空。因此,您需要
{}
在node
或之后放置\node
。 \draw (0,-20) -- (0,100);
会导致线条很长,可能比纸张尺寸还长。另一方面,\draw (-0.01cm,\y) -- (0.01cm,\y)
会导致线条很短,几乎看不见。我想,你可能想在这里更改尺寸。- 使用 时
plot coordinates
,列表中的单个坐标之间不应有逗号。在这种情况下,我想知道使用具有相同的多个坐标绘制一条线是否有意义是价值......但我想,您想用这种方法做一些不同的事情。 - 可能不需要设置
samples=3000
。请先尝试较小的值。
答案3
- 对于你的图表我将使用
pgfplots
包 - 图表的样式可以通过
axis
选项决定, - 我会像@daleif在他的简约回答中那样在表格中收集函数坐标(+1),
- 我只会对函数使用粗线:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotstableread[col sep=comma]{
-1, 1, -3.88461538461538
-0.35714286, 1, -3.24175824175824
0.28571429, 1, -2.59890109890110
0.92857143, 1, -1.95604395604396
1.57142857, 1, -1.31318681318681
2.21428571, 1, -0.67032967032967
2.85714286, 1, -0.02747252747252
3.5, 1, 0.61538461538461
4.14285714, 1, 1.25824175824176
4.78571429, 1, 1.90109890109890
5.42857143, 1, 2.54395604395604
6.07142857, 1, 3.18681318681319
6.71428571, 1, 3.82967032967033
7.35714286, 1, 4.47252747252747
8, 1, 5.11538461538462
}\loadedtable
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-1.5, xmax=8.5,
ymin=-4.5, ymax=5.5,
axis on top,
xtick distance=1,
ytick distance=1,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=15,
every axis plot post/.append style={very thick}
]
\addplot table[x index=0,y index=1] {\loadedtable};
\addplot table[x index=0,y index=2] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
- 由于图形呈现两个线性函数,您可以将它们绘制为第一个和最后一个坐标之间的直线:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotstableread[col sep=comma]{
-1, 1, -3.88461538461538
8, 1, 5.11538461538462
}\loadedtable
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-1.5, xmax=8.5,
ymin=-4.5, ymax=5.5,
axis on top,
xtick distance=1,
ytick distance=1,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=2,
every axis plot post/.append style={very thick}
]
\addplot table[x index=0,y index=1] {\loadedtable};
\addplot table[x index=0,y index=2] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
结果和以前一样。