tikz、pgfplots 和 polaraxis

tikz、pgfplots 和 polaraxis

我找到了一个不错的极坐标图模板1我做了如下调整:

\documentclass[12pt]{standalone}
\usepackage[x11names]{xcolor} 
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\huge
\begin{tikzpicture}
    \edef\val{0}
    \foreach \a in {0, 1,...,359}
    \draw[Azure4] (\a:9.7) -- (\a:10);
    \foreach \a in {0, 5,...,359}
    \draw[Azure4] (\a:9.5) -- (\a:10);          
    \foreach \r in {5,6,...,9}
    \draw[black!45] (0,0) circle (\r);    
    \draw[black, ultra thick] (0,0) circle (10);    
    \foreach \r in {5, 6,...,10}
      {
      \draw (\r,0) node[inner sep=1pt,below=3pt,rectangle,fill=white] {$\val$};
      \pgfmathparse{int(\val+1)}
      \xdef\val{\pgfmathresult}
      }
    \draw (7.5,0) node[inner sep=1pt,above=3pt,rectangle,fill=white] {wear (mm)};
    \foreach \a in {0, 90,...,359}
    \draw[very thick] (\a:5) -- (\a:10);
    \draw (0: 10.5) node {$0$};
    \draw (90: 10.5) node {$\pi/2$};
    \draw (180: 10.5) node {$\pi$};
    \draw (270: 10.5) node {$3\pi/2$};
    \draw[fill=red] (0,0) circle(1.1mm);

\end{tikzpicture}
\end{document}

我想从这个空白图中添加一些从外部文本文件读取的数据,比如说data.txt

到目前为止,我发现包含外部数据的唯一方法与命令有关\addplot,因此我正在考虑使用环境polaraxis

该文件data.txt包含两列,例如:

t   u
0   1
20  2
40  3
60  4
100 5
130 4
160 3
180 2
220 2
240 3
260 3
290 3
300 1
320 1
360 1
360 10
360 1

首先,为了读取数据我使用:

\usepgfplotslibrary{polar}
\pgfplotstableread{data.txt}\data

然后在 tikzpicture 中创建我的极轴环境:

\begin{polaraxis}[%
xmin=0, xmax=360,
xtick={},
xticklabels={},
xticklabels={},
xlabel={},
ylabel={},
yticklabels={},
axis lines = none,
filter discard warning=false,
axis on top]
\addplot [color=blue,each nth point=1,very thick] table [x=t,y=u] {\data};
\end{polaraxis}

我最终得到了这个图表:

在此处输入图片描述

我的问题是:(1)我怎样才能使 和 我的 tikzpicture 具有相同的来源polaraxis,以及(2)我可以使用\draw命令直接读取我的数据而不使用polaraxis\addplot吗?

1 http://www.texample.net/tikz/examples/polar-coordinates-template/

答案1

它们都应该在相同的 tikzpicture环境。

\documentclass[12pt]{standalone}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{polar}
\pgfplotstableread{
t   u
0   1
20  2
40  3
60  4
100 5
130 4
160 3
180 2
220 2
240 3
260 3
290 3
300 1
320 1
360 1
360 10
360 1
}{\data}

\tikzset{mypic/.pic={%
    \begin{scope}[x=1cm,y=1cm]
    \edef\val{#1}
    \foreach \a in {0, 1,...,359}
    \draw[Azure4] (\a:9.7) -- (\a:10);
    \foreach \a in {0, 5,...,359}
    \draw[Azure4] (\a:9.5) -- (\a:10);
    \foreach \r in {5,6,...,9}{
    \draw[black!45] (0,0) circle (\r);
    }
    \draw[black, ultra thick] (0,0) circle (10);
    \foreach \r in {5, 6,...,10}
      {
      \draw (\r,0) node[inner sep=1pt,below=3pt,rectangle,fill=white] {$\val$};
      \pgfmathparse{int(\val+1)}
      \xdef\val{\pgfmathresult}
      }
    \draw (7.5,0) node[inner sep=1pt,above=3pt,rectangle,fill=white] {wear (mm)};
    \foreach \a in {0, 90,...,359}{
    \draw[very thick] (\a:5) -- (\a:10);
    }
    \draw (0: 10.5) node {$0$};
    \draw (90: 10.5) node {$\pi/2$};
    \draw (180: 10.5) node {$\pi$};
    \draw (270: 10.5) node {$3\pi/2$};
    \draw[fill=red] (0,0) circle(1.1mm);
     \end{scope}
  }
}


\begin{document}
\huge

\begin{tikzpicture}

    \begin{polaraxis}[%
xmin=0, xmax=360,clip=false,
xtick={},
xticklabels={},
xticklabels={},
xlabel={},
ylabel={},
yticklabels={},
axis lines = none,
filter discard warning=false,
axis on top]
\addplot [color=blue,each nth point=1,very thick] table [x=t,y=u] {\data};
\pic {mypic={0}};

\end{polaraxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容