Örnek veri sağlamadınız. Örtük olarak bir veri çerçevesi tanımlanır
Çokgen |
Objectıdname |
Nesne sınıfı |
zaman |
ÇOKGEN ((58 91, 58 77, 0 73, -0 87, 58 91)) |
0 |
1 |
00:00 |
ÇOKGEN ((51 102, 58 90, 7 62, 0 74, 51 102)) |
0 |
1 |
01:00 |
ÇOKGEN ((40 110, 52 101, 18 54, 6 63, 40 110)) |
0 |
1 |
02:00 |
Bu, her zaman için farklı koordinatlara sahip aynı çokgen olan ek bir sütun ObjectId varsaymıştır
Plotly animasyonlu bir figür oluşturmanın en basit yolu Plotly Express'i kullanmaktır. Bu nedenle, verileri uzun bir veri çerçevesinde x ve y dizileri olacak şekilde yeniden yapılandırın
import io
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
import shapely.wkt
df_p = pd.read_csv(io.StringIO("""Polygon,ObjectId,ObjClass,time
"POLYGON ((58 91, 58 77, 0 73, -0 87, 58 91))",0,1,00:00
"POLYGON ((51 102, 58 90, 7 62, 0 74, 51 102))",0,1,01:00
"POLYGON ((40 110, 52 101, 18 54, 6 63, 40 110))",0,1,02:00
"POLYGON ((28 112, 41 109, 30 52, 17 55, 28 112))",0,1,03:00
"POLYGON ((32 29, 20 33, 38 93, 50 89, 32 29))",1,1,00:00
"POLYGON ((45 31, 34 29, 25 91, 36 93, 45 31))",1,1,01:00
"POLYGON ((57 38, 47 32, 13 84, 23 90, 57 38))",1,1,02:00
"POLYGON ((65 50, 58 39, 5 72, 12 83, 65 50))",1,1,03:00
"POLYGON ((1 90, 7 90, 12 48, 6 48, 1 90))",2,1,00:00
"POLYGON ((-7 85, -2 89, 20 53, 15 49, -7 85))",2,1,01:00
"POLYGON ((-13 78, -10 83, 26 60, 23 55, -13 78))",2,1,02:00
"POLYGON ((-15 69, -14 75, 28 69, 27 63, -15 69))",2,1,03:00
"POLYGON ((59 12, 49 18, 84 92, 94 86, 59 12))",3,1,00:00
"POLYGON ((77 11, 65 11, 66 93, 78 93, 77 11))",3,1,01:00
"POLYGON ((94 17, 83 12, 49 87, 60 92, 94 17))",3,1,02:00
"POLYGON ((107 30, 99 21, 36 74, 44 83, 107 30))",3,1,03:00
"POLYGON ((9 29, 3 37, 13 43, 19 35, 9 29))",4,1,00:00
"POLYGON ((12 28, 4 34, 10 44, 18 38, 12 28))",4,1,01:00
"POLYGON ((15 29, 5 31, 7 43, 17 41, 15 29))",4,1,02:00
"POLYGON ((17 32, 8 29, 5 40, 14 43, 17 32))",4,1,03:00"""))
df_p["Polygon"] = df_p["Polygon"].apply(shapely.wkt.loads)
# explode out ploygons to x & y for plotly express
df_plot = (
df_p["Polygon"]
.apply(
lambda p: [{"x": x, "y": y} for x, y in zip(p.exterior.xy[0], p.exterior.xy[1])]
)
.explode()
.apply(pd.Series)
.join(df_p)
)
px.line(df_plot, x="x", y="y", color="ObjectId", animation_frame="time")
örnek verileri kullanma
- örnek veriler düzgün kullanılmıyor https://shapely.readthedocs.io/en/stable/manual.html#polygons Bu nedenle kullanımı
exterior.xy
örnek kodda alakasız
- örnek veriler, verileri bir köşenin Köşeyle nitelendirildiği bir ObjID için bir değerler dizisi olarak tanımlar
- bir çokgen çizmek için, başlangıçta ve sonunda aynı noktayı içeren bir x/y dizisi gerekir. Bu eklendi
- sipariş önemlidir, bu nedenle Köşeyi şu şekilde tanımlayın https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Categorical.html
- sonra animasyonlu bir şekil oluşturmak için plotly kullanmak çok basit
import pandas as pd
import io
import plotly.express as px
df = pd.read_csv(
io.StringIO(
"""ObjClass Time ObjID Corner Lat Long
0 3.0 5.9 54.0 RR 21.549906 129.418088
563 3.0 5.9 54.0 LR 23.569243 129.523022
1126 3.0 5.9 54.0 LF 23.569243 136.523022
1689 3.0 5.9 54.0 RF 21.549906 136.418088
1 3.0 5.95 54.0 RR 17.946687 114.856340
564 3.0 5.95 54.0 LR 19.726662 114.919184
1127 3.0 5.95 54.0 LF 19.726662 121.919184
1690 3.0 5.95 54.0 RF 17.946687 121.856340
2 3.0 6.0 54.0 RR 17.685873 121.115523
565 3.0 6.0 54.0 LR 19.556310 121.182149
1128 3.0 6.0 54.0 LF 19.556310 128.182149
1691 3.0 6.0 54.0 RF 17.685873 128.115523
3 3.0 6.05 54.0 RR 17.685873 121.115523
566 3.0 6.05 54.0 LR 19.556310 121.182149
1129 3.0 6.05 54.0 LF 19.556310 128.182149
1692 3.0 6.05 54.0 RF 17.685873 128.115523
4 3.0 6.1 54.0 RR 17.375609 127.925362
567 3.0 6.1 54.0 LR 19.322351 127.848100
1130 3.0 6.1 54.0 LF 19.322351 134.848100
1693 3.0 6.1 54.0 RF 17.375609 134.925362"""
),
sep="\s+",
engine="python",
)
# if polygon is a quadrangle, need five points to define it. 4 is just a linestring
# duplicate first corner
df = pd.concat([df, df.loc[df["Corner"].eq("RR")].assign(Corner="RR2")])
# need to sort by Corner, hence make it a categorical
df["Corner"] = pd.Categorical(
df["Corner"], ["RR", "RF", "LF", "LR", "RR2"], ordered=True
)
px.line(
df.sort_values(["ObjID", "Time", "Corner"]),
x="Long",
y="Lat",
color="ObjID",
animation_frame="Time",
).update_layout(
xaxis={"range": [df["Long"].min(), df["Long"].max()]},
yaxis={"range": [df["Lat"].min(), df["Lat"].max()]},
)