从笛卡尔图到极坐标直方图

从笛卡尔图到极坐标直方图

本文介绍了使用 Mathematica 从笛卡尔图到极坐标直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

dalist={{21, 22}, {26, 13}, {32, 17}, {31, 11}, {30, 9},
        {25, 12}, {12, 16}, {18, 20}, {13, 23}, {19, 21},
        {14, 16}, {14, 22}, {18,22}, {10, 22}, {17, 23}}


ScreenCenter = {20, 15}

FrameXYs = {{4.32, 3.23}, {35.68, 26.75}}

Graphics[{EdgeForm[Thick], White, Rectangle @@ FrameXYs,
          Black, Point@dalist, Red, Disk[ScreenCenter, .5]}]

我想做的是计算每个点在坐标系中的角度,例如:

What I would like to do is to compute, for each point, its angle in a coordinate system such as :

上面是 Deisred 输出,这些是给定特定角度箱"的点的频率计数.一旦我知道如何计算角度,我就应该能够做到这一点.

Above is the Deisred output, those are frequency count of point given a particular "Angle Bin".Once I know how to compute the angle i should be able to do that.

推荐答案

Mathematica 有一个特殊的绘图函数用于此目的:ListPolarPlot.您需要将 x,y 对转换为 theta, r 对,例如如下所示:

Mathematica has a special plot function for this purpose: ListPolarPlot. You need to convert your x,y pairs to theta, r pairs, for instance as follows:

ListPolarPlot[{ArcTan[##], EuclideanDistance[##]} & @@@ (#-ScreenCenter & /@ dalist),
          PolarAxes -> True,
          PolarGridLines -> Automatic,
          Joined -> False,
          PolarTicks -> {"Degrees", Automatic},
          BaseStyle -> {FontFamily -> "Arial", FontWeight -> Bold,FontSize -> 12},
          PlotStyle -> {Red, PointSize -> 0.02}
]

更新

根据每条评论的要求,可以按如下方式制作极坐标直方图:

As requested per comment, polar histograms can be made as follows:

maxScale = 100;
angleDivisions = 20;
dAng = (2 [Pi])/angleDivisions;

一些测试数据:

(counts = Table[RandomInteger[{0, 100}], {ang, angleDivisions}]) // BarChart
ListPolarPlot[{{0, maxScale}},
    PolarAxes -> True, PolarGridLines -> Automatic,
    PolarTicks -> {"Degrees", Automatic},
    BaseStyle -> {FontFamily -> "Arial", FontWeight -> Bold, FontSize -> 12},
    PlotStyle -> {None},
    Epilog -> {Opacity[0.7], Blue,
               Table[
                 Polygon@
                  {
                   {0, 0},
                   counts[[ang + 1]] {Cos[ang dAng - dAng/2],Sin[ang dAng- dAng/2]},
                   counts[[ang + 1]] {Cos[ang dAng + dAng/2],Sin[ang dAng+ dAng/2]}
                  },
                 {ang, 0, angleDivisions - 1}
               ]}
]

使用 Disk 扇区代替 Polygons 的小视觉改进:

A small visual improvement using Disk sectors instead of Polygons:

ListPolarPlot[{{0, maxScale}},
    PolarAxes -> True, PolarGridLines -> Automatic,
    PolarTicks -> {"Degrees", Automatic},
    BaseStyle -> {FontFamily -> "Arial", FontWeight -> Bold,
    FontSize -> 12}, PlotStyle -> {None},
    Epilog -> {Opacity[0.7], Blue,
               Table[
                 Disk[{0,0},counts[[ang+1]],{ang dAng-dAng/2,ang dAng+dAng/2}],
                 {ang, 0, angleDivisions - 1}
               ]
              }
]

通过在 Epilog 中添加 EdgeForm[{Black, Thickness[0.005]}] 可以获得更清晰的条​​形"分离.现在标记环的数字后面仍然有不必要的小数点.跟随带有替换 /的情节.样式[num_?MachineNumberQ, List[]] ->Style[num//Round, List[]] 删除了那些.最终结果是:

A clearer separation of the 'bars' is obtained with the addition of EdgeForm[{Black, Thickness[0.005]}] in the Epilog. Now the numbers marking the rings still have the unnecessary decimal point trailing them. Following the plot with the replacement /. Style[num_?MachineNumberQ, List[]] -> Style[num // Round, List[]] removes those. The end result is:

上面的图也可以用 SectorChart 生成,尽管这个图主要是为了显示数据的不同宽度和高度,并且没有针对固定宽度的图进行微调扇区,并且您想突出显示这些方向的方向和数据计数.但它可以通过使用 SectorOrigin 来完成.问题是我认为扇区的中点为其方向编码,因此扇区中间有 0 度 我必须通过 [Pi]/angleDivisions 偏移原点并指定当它们也旋转时手动滴答:

The above plot can also be generated with SectorChart although this plot is primarily intended to show varying width and height of the data, and isn't fine-tuned for plots where you have fixed-width sectors and you want to highlight directions and data counts in those directions. But it can be done by using SectorOrigin. The problem is I take it that the midpoint of a sector codes for its direction so to have 0 deg in the mid of a sector I have to offset the origin by [Pi]/angleDivisions and specify the ticks by hand as they get rotated too:

SectorChart[
   {ConstantArray[1, Length[counts]], counts}[Transpose],
   SectorOrigin -> {-[Pi]/angleDivisions, "Counterclockwise"},
   PolarAxes -> True, PolarGridLines -> Automatic,
   PolarTicks ->
    {
     Table[{i [Degree] + [Pi]/angleDivisions, i [Degree]}, {i, 0, 345, 15}],
     Automatic
    },
   ChartStyle -> {Directive[EdgeForm[{Black, Thickness[0.005]}], Blue]},
   BaseStyle -> {FontFamily -> "Arial", FontWeight -> Bold,
   FontSize -> 12}
 ]

情节几乎相同,但更具交互性(工具提示等).

The plot is almost the same, but it is more interactive (tooltips and so).

这篇关于使用 Mathematica 从笛卡尔图到极坐标直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:23