如何使用 TikZ 制作类似于新 Mathematica 9 图的图

如何使用 TikZ 制作类似于新 Mathematica 9 图的图

新的 Mathematica 9 已经发布!我很想知道如何制作类似于某人的广告,特别是以下内容:

用 x 轴上的线绘制每个点 特定点之后颜色发生变化的图

这是一个简单的入门情节。

%& -shell-escape
\documentclass{article}
\usepackage{tikz}

\begin{filecontents}{data.dat}
3.045784        3.415896
3.405784        4.025693
3.785784        4.522530
4.125784        5.538449
4.485784        6.704992
4.805784        6.978939
5.145784        7.113496
5.425784        8.916397
6.065784        9.487712
6.365784        10.876397
6.685784        10.693497
7.025784        11.364131
7.345784        11.442530
7.665784        12.582530
8.005784        13.125693
8.225784        13.738450
8.585784        14.247891
8.865784        14.982530
\end{filecontents}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[x=1cm,y=0.4cm]

  \def\xmin{3}
  \def\xmax{9.2}
  \def\ymin{2}
  \def\ymax{15.5}

  % grid
  \draw[style=help lines, ystep=2, xstep=1] (\xmin,\ymin) grid
  (\xmax,\ymax);

  % axes
  \draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {$x$};
  \draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {$y$};

  % xticks and yticks
  \foreach \x in {3,4,...,9}
    \node at (\x, \ymin) [below] {\x};
  \foreach \y in {2,4,...,14}
    \node at (\xmin,\y) [left] {\y};

  % plot the data from the file data.dat
  % smooth the curve and mark the data point with a dot
  \draw[color=blue] plot[smooth,mark=*,mark size=1pt] file {data.dat}
   node [right] {data};


\end{tikzpicture}

\end{document}

简单情节示例

答案1

以下是使用 PGFPlots 实现此目的的一种方法:第一个图只是一个ycomb图,第二个图我将使用单独的图来绘制区域(带有尾随\closedcycle以正确填充)和线条。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
3.045784        3.415896
3.405784        4.025693
3.785784        4.522530
4.125784        5.538449
4.485784        6.704992
4.805784        6.978939
5.145784        7.113496
5.425784        8.916397
6.065784        9.487712
6.365784        10.876397
6.685784        10.693497
7.025784        11.364131
7.345784        11.442530
7.665784        12.582530
8.005784        13.125693
8.225784        13.738450
8.585784        14.247891
8.865784        14.982530
\end{filecontents}

\begin{document}
\pgfplotsset{
    compat=1.5,
    width=10cm,
    height=5cm
}


\begin{tikzpicture}
\begin{axis}[
    ymin=0,
    xtick=\empty,
    ytick=\empty,
    axis background/.style={fill=gray!10},
]
\addplot [ycomb, red, very thick, mark=*, mark options={red!60!black}] table {data.dat};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    ymin=0,
    xtick=\empty,
    ytick=\empty,
    axis background/.style={fill=gray!10},
    enlarge x limits=false
]
\addplot [draw=none, fill=orange!40!yellow] table {data.dat} \closedcycle;
\addplot [draw=gray, very thick] table {data.dat};

\addplot [draw=none, fill=red!60] table [
    x expr=\thisrowno{0}+8.865784-3.045784,
    y expr=\thisrowno{1}+14.982530-3.415896
] {data.dat} \closedcycle;
\addplot [draw=red!70!black, very thick] table [
    x expr=\thisrowno{0}+8.865784-3.045784,
    y expr=\thisrowno{1}+14.982530-3.415896
] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}

答案2

啊哈,杰克速度更快了。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=false,
axis background/.style={shade,top color=gray!20,bottom color=white},
]
\addplot[ycomb,mark=*,mark color=black,domain=2:10,samples=70] {rnd+(x)^0.3333};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
ymax=7,
axis background/.style={shade,top color=gray!20,bottom color=white},
]
\addplot[fill=orange,no marks,samples=500,domain=1.5:10] {rnd+ln(x)} \closedcycle;
\addplot[fill=brown,no marks,samples=100,domain=10:12] {0.5*rnd+ln(x)} \closedcycle;
\draw node[append after command={ (a) -| (axis cs:11,2)}] (a) at (axis cs:6,4) {Global Filtering!};
\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

首先,我建议将您的示例移至使用pgfplots,然后解决它。

买这个可以省 1000 美元(代码如下)在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.dat}
3.045784        3.415896
3.405784        4.025693
3.785784        4.522530
4.125784        5.538449
4.485784        6.704992
4.805784        6.978939
5.145784        7.113496
5.425784        8.916397
6.065784        9.487712
6.365784        10.876397
6.685784        10.693497
7.025784        11.364131
7.345784        11.442530
7.665784        12.582530
8.005784        13.125693
8.225784        13.738450
8.585784        14.247891
8.865784        14.982530
\end{filecontents}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[x=1cm,y=0.4cm]
\begin{axis}
\addplot+[ycomb] table {data.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[x=1cm,y=0.4cm]
\begin{axis}
\addplot+[fill=orange, opacity=0.5,mark=none] table {data.dat} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}

相关内容