
다음 코드가 있습니다.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[active,float]{preview}
\usetikzlibrary{calc}
\usetikzlibrary{graphs,plotmarks,arrows,automata,positioning,fit,shapes.geometric,backgrounds}
\usepackage{graphicx}
\PreviewEnvironment{tikzpicture}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}[x=0.8cm,y=0.5cm]
%axis
\draw (0,0) -- coordinate (x axis mid) (10,0);
\draw (0,0) -- coordinate (y axis mid) (0,12);
%ticks
\foreach \x in {0,...,10}
\draw (\x,0pt) -- (\x,-3pt) node[anchor=north] {\x};
\foreach \y in {0,...,12}
\draw (0pt,\y) -- (-3pt,\y) node[anchor=east] {\y};
%labels
\node[below=0.8cm,xshift=0.3cm,scale=1.5] at (x axis mid) {$\sum{t_i}\;\;\longrightarrow$};
% plot first concave function
\draw[mark=square*,green,thick,mark options={fill=green}] (0,2) -- (1,3) -- (2,4) -- (3,5) -- (4,5) -- (5,5) -- (6,5) -- (7,5) -- (8,5) -- (9,3) -- (10,1);
\node[mark=square*,green,thick,mark options={fill=green}] at (4,5) {};
% plot second concave function
\draw[mark=square*,red,thick,mark options={fill=red}] (0,1) -- (1,2) -- (2,3) -- (3,4) -- (4,5) -- (5,6) -- (6,6) -- (7,5) -- (8,4) -- (9,3) -- (10,2);
% plot third concave function
\draw[mark=*,blue,thick,mark options={fill=blue}] (0,3) -- (1,5) -- (2,7) -- (3,9) -- (4,10) -- (5,11) -- (6,11) -- (7,10) -- (8,9) -- (9,6) -- (10,3);
%legend
\begin{scope}[shift={(0.5,10)}]
\draw[yshift=3\baselineskip] (0,0) --
plot[mark=square*, mark options={fill=red}] (0.25,0) -- (0.5,0) node[right]{$f(t)$};
\draw[yshift=2\baselineskip] (0,0) --
plot[mark=square*, mark options={fill=green}] (0.25,0) -- (0.5,0) node[right]{$g(t)$};
\draw[yshift=\baselineskip] (0,0) --
plot[mark=square*, mark options={fill=blue}] (0.25,0) -- (0.5,0) node[right]{$f(t) + g(t)$};
\end{scope}
\end{tikzpicture}
\end{document}
다음 그림을 나타냅니다.
다음을 수행하는 데 문제가 있습니다.
2단계로 xy축 양쪽에 숫자를 표시합니다.
어떤 이유로 선분 사이의 노드를 시각화할 수 없습니다.
범례 주위에 직사각형 영역을 추가할 수 없습니다.
답변1
의견과 Harish의 답변에서 언급했듯이 을 사용하면 pgfplots
스타일을 한 번 지정하고 모든 플롯에 대해 바퀴를 다시 만드는 대신 문서에서 다시 사용할 수 있습니다.
나는 모든 플롯에서 사용할 수 있는 cycle list
호출을 만들었습니다( 플롯 스타일을 순환하는 데 사용).mycycle
cycle list name=mycycle
나머지 스타일링은 환경에 대한 옵션에서 수행되지만 axis
이는 다른 플롯에서 재사용할 수 있도록 TikZ 스타일로 쉽게 패키징됩니다.
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\pgfplotscreateplotcyclelist{mycycle}{%
green!70!black,every mark/.append style={fill=green!80},mark=diamond*\\%
red!70!black,every mark/.append style={fill= red!80},mark=square*\\%
blue!70!black,every mark/.append style={fill= blue!80},mark=*\\%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
ymin=0,ymax=13.75,
tick align=inside,
axis line style={-},
cycle list name=mycycle,
legend pos=north west,
legend cell align=left,
legend style={
outer sep=-0.2em,
inner sep=0.2em,
node font=\small,
},
xlabel={$\sum{t_i}\;\;\longrightarrow$},
]
\addplot coordinates {
(0,2) (1,3) (2,4) (3,5) (4,5) (5,5) (6,5) (7,5) (8,5) (9,3) (10,1)
};
\addlegendentry{$f(t)$};
\addplot coordinates {
(0,1) (1,2) (2,3) (3,4) (4,5) (5,6) (6,6) (7,5) (8,4) (9,3) (10,2)
};
\addlegendentry{$g(t)$};
\addplot coordinates {
(0,3) (1,5) (2,7) (3,9) (4,10) (5,11) (6,11) (7,10) (8,9) (9,6) (10,3)
};
\addlegendentry{$f(t)+g(t)$};
\end{axis}
\end{tikzpicture}
\end{document}
답변2
pgfplots
그러한 작업에 사용하지 않겠습니까 ? 어쨌든 범례 주위에 상자를 그리려면 fit
라이브러리를 사용할 수 있습니다. 구문이 plot
다릅니다. 플롯 표시를 얻으려면 적절한 구문의 좌표를 사용해야 합니다. 그리고 변경의 배수로 눈금 레이블을 얻으려면 2
foreach 루프를 {0,2,...,12}
.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{graphs,plotmarks,arrows,automata,positioning,fit,shapes.geometric,backgrounds}
\usetikzlibrary{plotmarks,fit}
\begin{document}
\begin{tikzpicture}[x=0.8cm,y=0.5cm]
%axis
\draw (0,0) -- coordinate (x axis mid) (10,0);
\draw (0,0) -- coordinate (y axis mid) (0,12);
%ticks
\foreach \x in {0,2,...,10}
\draw (\x,0pt) -- (\x,-3pt) node[anchor=north] {\x};
\foreach \y in {0,2,...,12}
\draw (0pt,\y) -- (-3pt,\y) node[anchor=east] {\y};
%labels
\node[below=0.8cm,xshift=0.3cm,scale=1.5] at (x axis mid) {$\sum{t_i}\;\;\longrightarrow$};
% plot first concave function
\draw[green,thick,] plot[mark=square*,mark options={fill=green}] coordinates{(0,2) (1,3) (2,4) (3,5) (4,5) (5,5) (6,5) (7,5) (8,5) (9,3) (10,1)};
\node[mark=square*,green,thick,mark options={fill=green}] at (4,5) {};
% plot second concave function
\draw[red,thick] plot[mark=square*,mark options={fill=red}] coordinates {(0,1) (1,2) (2,3) (3,4) (4,5) (5,6) (6,6) (7,5) (8,4) (9,3) (10,2)};
% plot third concave function
\draw[blue,thick] plot [mark=*,mark options={fill=blue}] coordinates{(0,3) (1,5) (2,7) (3,9) (4,10) (5,11) (6,11) (7,10) (8,9) (9,6) (10,3)};
%legend
\begin{scope}[shift={(0.5,10)}]
\draw[yshift=3\baselineskip] (0,0) node(a){} --
plot[mark=square*, mark options={fill=red}] (0.25,0) -- (0.5,0) node[right]{$f(t)$};
\draw[yshift=2\baselineskip] (0,0) --
plot[mark=square*, mark options={fill=green}] (0.25,0) -- (0.5,0) node[right]{$g(t)$};
\draw[yshift=\baselineskip] (0,0) --
plot[mark=square*, mark options={fill=blue}] (0.25,0) -- (0.5,0) node[right](fg){$f(t) + g(t)$};
\node[draw,fit=(a) (fg)] {};
\end{scope}
\end{tikzpicture}
\end{document}
preview
그런데, 클래스 와 함께 명시적으로 패키지가 필요하지 않습니다 standalone
.