使用 ListLinePlot 的赛车圈速图

发布于 2024-11-01 20:22:09 字数 730 浏览 3 评论 0 原文

我正在尝试获取赛车运动单圈位置表并绘制类似于此的单圈图表 http://www.fia.com/en-GB/sport/championships/f1/2010/bahrain/Pages/lap_chart.aspx

每行对应一圈,第一圈位于第一行。每行的车号按照通过起跑线/终点线的顺序列出 表格可能如下所示(4 辆车比赛,6 圈:

1 3 2 4
1 3 2 4
1 3 4 2
3 1 4 2
3 1 4 2
3 4 1 2

在上面的例子中,第一圈后的顺序是1、3、2、4,到6圈比赛结束时,3号车获胜,4号车第二,依此类推。

很容易错误地绘制此图,我这样做了:

ListLinePlot[Table[Position[data,x],{x,4}]]

这确实会产生一个圈数图,但它的第一个位置是底部和顶部的第四个位置,我真正需要的是 y 轴运行 4-3-2-1,所以第一个位置在顶部。

如何反转 y 轴,使其从 1(顶部)运行到 n(底部)?

I'm trying to take a table of motorsport lap positions and plot a lap chart similar to this http://www.fia.com/en-GB/sport/championships/f1/2010/bahrain/Pages/lap_chart.aspx.

Each row corresponds to a lap, with the first lap in the first row. The car numbers are listed across each row in the order they pass the start/finish line
The table may look like this (4-car race, 6 laps:

1 3 2 4
1 3 2 4
1 3 4 2
3 1 4 2
3 1 4 2
3 4 1 2

In the above example, the order was 1,3,2,4 after the first lap, and by the end of the 6-lap race, car 3 won, car 4 was in second, and so on.

It's easy to plot this incorrectly, I did this:

ListLinePlot[Table[Position[data,x],{x,4}]]

Mathematica graphics

This does produce a lap chart, but it has 1st position at the bottom and 4th position at the top, and what I really need is the y-axis to run 4-3-2-1 so 1st position is at the top.

How can I reverse the y-axis so it runs from 1(top) to n(bottom)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我们的影子 2024-11-08 20:22:09

只需使用象限 4 即可解决屏幕位置问题。

这也适用于DNF! (未完成的驱动程序)。

第一个位置绘制在 y = -1 处,第二个位置绘制在 y = -2 处,依此类推。
请注意 {{lap_, y_} :>; 中的 y 是如何被 -y 替换的。下面的 {lap - 1, -y}}

lap 减少了 1,因为我包含了起始位置的数据 (lap=zero)。


进行较小的重写,以处理不同数量的驾驶员和圈数,并重新格式化代码以提高易读性。 - Mr.Wizard


data = 
  {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

{p, n} = {Max@data, Length@data};

ticks = {#, #} &@Array[{-#, #} &, p];
ticks[[All, 1, 2]] = {"Pole", "Winner"};

PrependTo[data, Range@p];  (* add starting position *)

ListLinePlot[
 Replace[
   Array[data~Position~# &, p],
   {lap_, y_} :> {lap - 1, -y},
   {2}
 ],
 Frame -> True,
 FrameLabel ->
  {"Laps Completed",
   "Starting Positions",
   "Laps Completed",
   "Final Positions"},
 GridLines -> {Range[0, n + 1], None},
 FrameTicks -> {ticks, {All, All}},
 PlotRange -> {Automatic, {-.7, -.3 - p}},
 PlotStyle -> Thickness[.01]
]

race cars

这是一辆车(开始于杆位)在完成最后两圈之前就退出了。请注意,3 号车自动前进一位。

DNF

Just use Quadrant 4 to settle the position-on-screen problem.

This also works for DNF! (Drivers that did not finish).

First place is plotted at y = -1, second place is plotted at y = -2, etc.
Note how y is replaced by -y in {{lap_, y_} :> {lap - 1, -y}} below.

lap was decremented by 1 because I included data for the starting position (lap=zero).


A minor rewrite, to work with different numbers of drivers and laps, and reformat the code for increased legibility. - Mr.Wizard


data = 
  {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

{p, n} = {Max@data, Length@data};

ticks = {#, #} &@Array[{-#, #} &, p];
ticks[[All, 1, 2]] = {"Pole", "Winner"};

PrependTo[data, Range@p];  (* add starting position *)

ListLinePlot[
 Replace[
   Array[data~Position~# &, p],
   {lap_, y_} :> {lap - 1, -y},
   {2}
 ],
 Frame -> True,
 FrameLabel ->
  {"Laps Completed",
   "Starting Positions",
   "Laps Completed",
   "Final Positions"},
 GridLines -> {Range[0, n + 1], None},
 FrameTicks -> {ticks, {All, All}},
 PlotRange -> {Automatic, {-.7, -.3 - p}},
 PlotStyle -> Thickness[.01]
]

race cars

Here's the case where car #1 (the one that started in the Pole Position) dropped out before completing the final two laps. Notice that car #3 automatically advanced by one position.

DNF

噩梦成真你也成魔 2024-11-08 20:22:09

反转位置的顺序,然后重新标记刻度:

ListLinePlot[
 Table[Position[data, x] /. {xx_, yy_} :> {xx, 5 - yy}, {x, 4}],
 Ticks -> {Automatic, {{1, 4}, {2, 3}, {3, 2}, {4, 1}}}, 
 PlotStyle -> Thickness[.01]]

在此处输入图像描述

Reverse the order of the positions, and then relabel the ticks:

ListLinePlot[
 Table[Position[data, x] /. {xx_, yy_} :> {xx, 5 - yy}, {x, 4}],
 Ticks -> {Automatic, {{1, 4}, {2, 3}, {3, 2}, {4, 1}}}, 
 PlotStyle -> Thickness[.01]]

enter image description here

锦欢 2024-11-08 20:22:09

好的,有人提出了 BarChartScalingFunctions,那么我们开始吧......

BarChart[Ordering /@ data, ChartLayout -> "Overlapped", 
 Joined -> Automatic, BarSpacing -> 0, ChartElementFunction -> ({} &),
  ChartStyle -> 1, ScalingFunctions -> "Reverse", Axes -> False, 
 Frame -> {{True, False}, {True, False}}, PlotRange -> {All, All}, 
 BaseStyle -> Thickness[0.01]]

基于 BarChart 的解决方案

(但是 ListPlot 解决方案可能更简单。可惜它还不支持 ScalingFunctions。)

Ok, someone brought up BarChart and ScalingFunctions, so here we go....

BarChart[Ordering /@ data, ChartLayout -> "Overlapped", 
 Joined -> Automatic, BarSpacing -> 0, ChartElementFunction -> ({} &),
  ChartStyle -> 1, ScalingFunctions -> "Reverse", Axes -> False, 
 Frame -> {{True, False}, {True, False}}, PlotRange -> {All, All}, 
 BaseStyle -> Thickness[0.01]]

BarChart-based solution

(but the ListPlot solution is probably easier. Too bad it doesn't support ScalingFunctions yet.)

沒落の蓅哖 2024-11-08 20:22:09

我将放弃这个“聪明”的实现,因为我喜欢它,但大卫的答案更加稳健。

laps = 
  {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

ListLinePlot[
  -Thread[Ordering /@ laps],
  AxesOrigin -> {1, 0}, PlotStyle -> Thick,
  Ticks -> {All, Array[{-#, #} &, 4]}
]

在此处输入图像描述

I am going to leave up this "clever" implementation because I like it, but David's answer is far more robust.

laps = 
  {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

ListLinePlot[
  -Thread[Ordering /@ laps],
  AxesOrigin -> {1, 0}, PlotStyle -> Thick,
  Ticks -> {All, Array[{-#, #} &, 4]}
]

enter image description here

萤火眠眠 2024-11-08 20:22:09

现在完全显示 y 轴怎么样:

data = {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

ListLinePlot[Table[Position[5 - data, x], {x, 4}], 
 Axes -> {True, False}]

Mathematicagraphics

How about now showing the y-axes at all:

data = {{1, 3, 2, 4},
   {1, 3, 2, 4},
   {1, 3, 4, 2},
   {3, 1, 4, 2},
   {3, 1, 4, 2},
   {3, 4, 1, 2}};

ListLinePlot[Table[Position[5 - data, x], {x, 4}], 
 Axes -> {True, False}]

Mathematica graphics

流年里的时光 2024-11-08 20:22:09

ScalingFunctions 现在似乎可以与 ListLinePlot 一起使用,

data = {{1, 3, 2, 4}, {1, 3, 2, 4}, {1, 3, 4, 2}, {3, 1, 4, 2}, {3, 1,
     4, 2}, {3, 4, 1, 2}};
ListLinePlot[Table[Position[data, x], {x, 4}], 
 ScalingFunctions -> {Identity, "Reverse"}, AxesOrigin -> {1, -5}]

我不知道为什么 AxesOrigin y 坐标需要为负数。

Mathematica 图形

ScalingFunctions now appears to work with ListLinePlot

data = {{1, 3, 2, 4}, {1, 3, 2, 4}, {1, 3, 4, 2}, {3, 1, 4, 2}, {3, 1,
     4, 2}, {3, 4, 1, 2}};
ListLinePlot[Table[Position[data, x], {x, 4}], 
 ScalingFunctions -> {Identity, "Reverse"}, AxesOrigin -> {1, -5}]

I have no idea why the AxesOrigin y-coordinate needs to be negative.

Mathematica graphics

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文