如何使用 PGFplots/tikz 制作风玫瑰?

如何使用 PGFplots/tikz 制作风玫瑰?

有人能告诉我怎么做吗?我在网上和 stackexchange 网站上都找不到类似的东西。我发现的最相似的情节是这样的:

绘制类似雷达的图表需要哪个 LaTeX 包?

我要做的是这样的:

风玫瑰种类

这是一些数据表

在此处输入图片描述

答案1

对于数据分箱(即计算直方图值),我建议使用外部工具,例如 Python 库 Pandas。例如,如果你有一个data.dat类似于从新西兰气候数据库...

Station,Date(NZST),Dir(DegT),Speed(m/s),Dir StdDev,Spd StdDev,Period(Hrs),Freq
3925,20150801:0000,0,0.0,23.0,0.1,1,H
3925,20150801:0100,156,0.2,35.0,0.3,1,H
3925,20150801:0200,97,0.3,16.0,0.4,1,H
3925,20150801:0300,139,0.3,15.0,0.4,1,H
3925,20150801:0400,315,0.4,51.0,0.5,1,H
...

...您可以使用以下 Python 脚本计算直方图:

import pandas as pd
data = pd.read_csv('data.dat', sep=',')
data['Dir(Rounded)'] = (data['Dir(DegT)']/(360/16)).round().mod(16)*360/16
frequencies = pd.crosstab(data['Dir(Rounded)'], pd.cut(data['Speed(m/s)'], bins)) / data['Dir(Rounded)'].size
frequencies.to_csv('frequencies.csv', sep='\t')

frequencies.csv然后看起来像这样:

Dir(Rounded)    (0, 0.5]    (0.5, 2]    (2, 4]  (4, 6]  (6, 8]  (8, 10]
0.0 0.0228187919463 0.0496644295302 0.0134228187919 0.0 0.0 0.0
45.0    0.0174496644295 0.0510067114094 0.0604026845638 0.0308724832215 0.0 0.0
90.0    0.0362416107383 0.0751677852349 0.00268456375839    0.0 0.0 0.0
135.0   0.0389261744966 0.153020134228  0.0510067114094 0.0 0.0 0.0
180.0   0.0201342281879 0.0348993288591 0.0161073825503 0.0 0.0 0.0
225.0   0.0161073825503 0.0295302013423 0.00402684563758    0.0 0.0 0.0
270.0   0.0375838926174 0.0402684563758 0.00402684563758    0.0 0.0 0.0
315.0   0.0644295302013 0.102013422819  0.00134228187919    0.0 0.0 0.0

然后可以使用 PGFPlots 绘制该数据文件:

\begin{tikzpicture}
\begin{polaraxis}[
    xtick={0,45,...,315},
    xticklabels={E,NE,N,NW,W,SW,S,SE},
    ytick=\empty,
    legend entries={0 to 0.5, 0.5 to 2, 2 to 4, 4 to 6},
    cycle list={cyan!20, cyan!50, cyan, cyan!50!black, cyan!20!black},
    legend pos=outer north east
]
\pgfplotsinvokeforeach{1,...,6}{
    \addplot +[polar bar=17, stack plots=y]
        table [x expr=-\thisrowno{0}+90, y index=#1] {frequencies.csv};
    }
\end{polaraxis}
\end{tikzpicture}

样式polar bar需要在文档的序言中定义。以下是完整的示例.tex文件:

\documentclass[]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.12}


\begin{document}
\makeatletter
\pgfplotsset{
    polar bar/.style={
        scatter,
        draw=none,
        mark=none,
        visualization depends on=rawy\as\rawy,
        area legend,
        legend image code/.code={%
            \fill[##1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
        },
        /pgfplots/scatter/@post marker code/.add code={}{
            \pgfmathveclen{\pgf@x}{\pgf@y}
            \edef\radius{\pgfmathresult}
            \fill[]
                (\pgfkeysvalueof{/data point/x},-\pgfkeysvalueof{/data point/y})
                ++({\pgfkeysvalueof{/data point/x}-#1/2},\pgfkeysvalueof{/data point/y})
                arc [start angle=\pgfkeysvalueof{/data point/x}-#1/2,
                    delta angle=#1,
                    radius={\radius pt}
                ]
                -- +({\pgfkeysvalueof{/data point/x}+#1/2},-\rawy)
                arc [start angle=\pgfkeysvalueof{/data point/x}+#1/2,
                    delta angle=-#1,
                    radius={
                        (\pgfkeysvalueof{/data point/y} - \rawy) / \pgfkeysvalueof{/data point/y} * \radius pt
                    }
                ]
                --cycle;
        }
    },
    polar bar/.default=30
}

\begin{tikzpicture}
\begin{polaraxis}[
    xtick={0,45,...,315},
    xticklabels={E,NE,N,NW,W,SW,S,SE},
    ytick=\empty,
    legend entries={0 to 0.5, 0.5 to 2, 2 to 4, 4 to 6},
    cycle list={cyan!20, cyan!50, cyan, cyan!50!black, cyan!20!black},
    legend pos=outer north east
]
\pgfplotsinvokeforeach{1,...,6}{
    \addplot +[polar bar=17, stack plots=y]
        table [x expr=-\thisrowno{0}+90, y index=#1] {frequencies.csv};
    }
\end{polaraxis}
\end{tikzpicture}

\end{document}

答案2

相当简短的代码pst-plot

\documentclass[x11names, border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pst-plot}
\usepackage{auto-pst-pdf}
\usepackage{array, siunitx} 

\begin{document}

\sffamily\setlength\tabcolsep{2pt}
\begin{pspicture}(-7,-7)(6,8)
\psset{xAxis=false, dy=1, Dy =4, subticks=4, ticksize=-4pt 0, mathLabel=false, labelFontSize=\footnotesize}
\psaxes{-}(-6.2,0)(-6.2,5)
\psaxes[Dy=-4, ylabelPos=l]{-}(-6.2,0)(-6.2,-5.035)
\rput{90}(-7,0){Frequency (per cent)}
%%%%%
\rput(6,5){\footnotesize\begin{tabular}{@{}l<{\rule[-0.2mm]{6mm}{3mm}}l@{}}\multicolumn{2}{c}{Wind speed}\\\multicolumn{2}{c}{(m/s)}\\[0.5ex]%
\color{red} & > 10\\ \color{Tan1} & 8 to10 \\ \color{Yellow1} & 6 to 8 \\ \color{Green4!80!} & 4 to 6 \\ \color{RoyalBlue3} & 2 to 4 \\ \color{Cyan1} & 0.5 to 2 \\ \color{Gold4} & < 0.5
\end{tabular}}
%%%%%
\rput(5.5,-5){\footnotesize\begin{tabular}{@{}ll@{}}
Mean speed &2.59\,m/s\\ Peak frequency: &18.64\,\%\\ Peak direction & NNW\\ Percent calm: &8.14\,\% \\
\multicolumn{2}{@{}l@{}}{Calm defined as < 0.5\,m/s}
\end{tabular}}
    \psset{axesstyle=polar, xsubticks=4,xsubtickcolor=LightSteelBlue4!60!, ysubticks=2, ysubtickwidth=0.4pt, ysubtickcolor =black, Dy=45, labels=none}
    \psaxes(5,360)
    \psset{unit=1.06cm}
    \rput(5,0){E}\rput{-67.5}(5;22.5){ENE}\rput{-45}(5.;45){NE}\rput{-22.5}(5;67.5){NNE}
    \rput(5;90){N}\rput{22.5}(5;112.5){NNW}\rput{45}(5;135){NW}\rput{67.5}(5;157.5){WNW}
    \rput(5;180){W}\rput{-67.5}(5;202.5){WSW}\rput{-45}(5.;225){SW}\rput{-22.5}(5;247.5){SSW}
    \rput(5;270){S}\rput{22.5}(5;292.5){SSE}\rput{45}(5;315){SE}\rput{67.5}(5;337.5){ESE}
    %%%%%%%%%%%%%%%
    \psset{unit=1cm}%l
    \pswedge*[linecolor=red]{4.7}{105}{120}
    \pswedge*[linecolor=Tan1]{4.65}{105}{120}
    \pswedge*[linecolor=Yellow1]{4.4}{105}{120}
    \pswedge*[linecolor=Green4!80!]{3.75}{105}{120}
    \pswedge*[linecolor=RoyalBlue3]{2.45}{105}{120}
    \pswedge*[linecolor=Cyan1]{0.85}{105}{120}
\end{pspicture}

\end{document} 

在此处输入图片描述

答案3

考虑到使用 pgfplots 生成刻度标签所需的工作量,不妨使用普通的 tikz。

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}

\begin{document}
\begin{tikzpicture}
\begin{polaraxis}[ymax=20,yticklabel=\empty,xticklabel=\empty,
xtick={0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5}]
\coordinate (origin) at (axis cs:0,0);
\coordinate (E) at (axis cs:0,21.5);
\coordinate (ENE) at (axis cs:22.5,21.5);
\coordinate (NE) at (axis cs:45,21.5);
\coordinate (NNE) at (axis cs:67.5,21.5);
\coordinate (N) at (axis cs:90,21.5);
\coordinate (NNW) at (axis cs:112.5,21.5);
\coordinate (NW) at (axis cs:135,21.5);
\coordinate (WNW) at (axis cs:157.5,21.5);
\coordinate (W) at (axis cs:180,21.5);
\coordinate (WSW) at (axis cs:202.5,21.5);
\coordinate (SW) at (axis cs:225,21.5);
\coordinate (SSW) at (axis cs:247.5,21.5);
\coordinate (S) at (axis cs:270,21.5);
\coordinate (SSE) at (axis cs:292.5,21.5);
\coordinate (SE) at (axis cs:315,21.5);
\coordinate (ESE) at (axis cs:337.5,21.5);
\end{polaraxis}
\node at(E) {\scriptsize\textsf{E}};
\node[rotate=-67.5] at(ENE) {\scriptsize\textsf{ENE}};
\node[rotate=-45] at(NE) {\scriptsize\textsf{NE}};
\node[rotate=-22.5] at(NNE) {\scriptsize\textsf{NNE}};
\node at(N) {\scriptsize\textsf{N}};
\node[rotate=22.5] at(NNW) {\scriptsize\textsf{NNW}};
\node[rotate=45] at(NW) {\scriptsize\textsf{NW}};
\node[rotate=67.5] at(WNW) {\scriptsize\textsf{WNW}};
\node at(W) {\scriptsize\textsf{W}};
\node[rotate=-67.5] at(WSW) {\scriptsize\textsf{WSW}};
\node[rotate=-45] at(SW) {\scriptsize\textsf{SW}};
\node[rotate=-22.5] at(SSW) {\scriptsize\textsf{SSW}};
\node at(S) {\scriptsize\textsf{S}};
\node[rotate=22.5] at(SSE) {\scriptsize\textsf{SSE}};
\node[rotate=45] at(SE) {\scriptsize\textsf{SE}};
\node[rotate=67.5] at(ESE) {\scriptsize\textsf{ESE}};
\end{tikzpicture}
\end{document}

罗盘

答案4

\coordinate (origin) at (axis cs:0,0);
\coordinate (E) at (axis cs:0,21.5);
\coordinate (ENE) at (axis cs:22.5,21.5);
\coordinate (NE) at (axis cs:45,21.5);
\coordinate (NNE) at (axis cs:67.5,21.5);
\coordinate (N) at (axis cs:90,21.5);
\coordinate (NNW) at (axis cs:112.5,21.5);
\coordinate (NW) at (axis cs:135,21.5);
\coordinate (WNW) at (axis cs:157.5,21.5);
\coordinate (W) at (axis cs:180,21.5);
\coordinate (WSW) at (axis cs:202.5,21.5);
\coordinate (SW) at (axis cs:225,21.5);
\coordinate (SSW) at (axis cs:247.5,21.5);
\coordinate (S) at (axis cs:270,21.5);
\coordinate (SSE) at (axis cs:292.5,21.5);
\coordinate (SE) at (axis cs:315,21.5);
\coordinate (ESE) at (axis cs:337.5,21.5);
\end{polaraxis}
\node at(E) {\scriptsize\textsf{E}};
\node[rotate=-67.5] at(ENE) {\scriptsize\textsf{ENE}};
\node[rotate=-45] at(NE) {\scriptsize\textsf{NE}};
\node[rotate=-22.5] at(NNE) {\scriptsize\textsf{NNE}};
\node at(N) {\scriptsize\textsf{N}};
\node[rotate=22.5] at(NNW) {\scriptsize\textsf{NNW}};
\node[rotate=45] at(NW) {\scriptsize\textsf{NW}};
\node[rotate=67.5] at(WNW) {\scriptsize\textsf{WNW}};
\node at(W) {\scriptsize\textsf{W}};
\node[rotate=-67.5] at(WSW) {\scriptsize\textsf{WSW}};
\node[rotate=-45] at(SW) {\scriptsize\textsf{SW}};
\node[rotate=-22.5] at(SSW) {\scriptsize\textsf{SSW}};
\node at(S) {\scriptsize\textsf{S}};
\node[rotate=22.5] at(SSE) {\scriptsize\textsf{SSE}};
\node[rotate=45] at(SE) {\scriptsize\textsf{SE}};
\node[rotate=67.5] at(ESE) {\scriptsize\textsf{ESE}};
\end{tikzpicture}
\end{document}

相关内容