【发布时间】:2021-04-29 10:22:47
【问题描述】:
我正在尝试将采样策略的每日周期绘制为一种玫瑰图/极坐标图。
我想显示我们的每个实验样本,其中距中心的距离是一天,从 0 开始的角度代表收集该样本的时间。理想情况下,我希望能够通过不同的变量为点着色。
理想的情节应该如下所示:
模拟虚拟数据来解释问题
我有xarray 格式的数据。我们有一个 launchtime 维度,其中包含采集样本的时间,我们希望使用它来绘制一天中的什么时间,然后依次绘制每一天。
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas.tseries.offsets import DateOffset
import matplotlib.dates as mdates
import itertools
value = np.random.normal(size=100)
expected_time = pd.date_range("2000-01-01", freq="180min", periods=100)
# add random offset to simulate being +/- the true expected release time
time_deltas = np.array([DateOffset(minute=max(0, min(int(i), 59))) for i in np.abs(np.random.normal(0, 10, size=100))])
time = [expected_time[i] + time_deltas[i] if (i % 2 == 0) else expected_time[i] - time_deltas[i] for i in range(100)]
df = pd.DataFrame({"launchtime": time, "value": value})
ds = df.set_index("launchtime").to_xarray()
ds = ds.assign_coords(expected_time=("launchtime", expected_time))
到目前为止我的想法
def time_to_angle(dt: pd.Timestamp) -> float:
SEC_IN_DAY = 86_400
start_of_day = pd.to_datetime(f"{dt.day}-{dt.month}-{dt.year}")
delta = (dt - start_of_day)
n_seconds = delta.seconds
# return angle in degrees
return (n_seconds / SEC_IN_DAY) * 360
# angle from 0 degrees
angles = [time_to_angle(pd.to_datetime(dt)) for dt in ds.launchtime.values]
# how far along the radius
days = np.arange(np.unique(ds["launchtime.day"].values).size)
# how to plot in polar coordinates? Do I have to draw an x,y grid and plot as a scatter?
任何关于如何解决这个问题的建议都将非常感激!
【问题讨论】:
标签: python python-3.x pandas matplotlib