以matplotlib绘制多个值的范围图

2024-05-04 08:40:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试确定生成一组lineplots显示为范围的最有效方法。我希望能生产出这样的产品:

enter image description here

我会尽量解释的。对不起,如果我错过了任何信息。我设想x-axistimestamps的范围hours(上午8点到上午9点到上午10点等等)。总范围在8:00:00到27:00:00之间。y-axis是发生在任何时间点的值的countplot中的范围表示出现的maxminaverage值。你知道吗

示例df如下所示:

import pandas as pd
import matplotlib.pyplot as plt

d = ({
    'Time1' : ['8:00:00','9:30:00','9:40:00','10:25:00','12:30:00','1:31:00','1:35:00','2:45:00','4:50:00'],                 
    'Occurring1' : ['1','2','3','4','5','5','6','6','7'],           
    'Time2' : ['8:10:00','9:34:00','9:48:00','10:40:00','1:30:00','2:31:00','3:35:00','3:45:00','4:55:00'],                 
    'Occurring2' : ['1','2','2','3','4','5','5','6','7'], 
    'Time3' : ['9:00:00','9:34:00','9:58:00','10:45:00','10:50:00','12:31:00','1:35:00','2:15:00','3:55:00'],                 
    'Occurring3' : ['1','2','3','4','4','5','6','7','8'],                     
     })

df = pd.DataFrame(data = d)

所以这个df表示3组不同的data。发生的次数、值以及条目的偶数可能会有所不同。你知道吗

下面是一个初步的例子。尽管我不确定是否需要重新考虑我的方法。滚动方程在这里行得通吗?评估maxminavgdf(8:00:00-9:00:00)中每小时出现的值数的东西。你知道吗

以下是完整的初步尝试:

import pandas as pd
import matplotlib.pyplot as plt

d = ({
    'Time1' : ['8:00:00','9:30:00','9:40:00','10:25:00','12:30:00','1:31:00','1:35:00','2:45:00','4:50:00'],                 
    'Occurring1' : ['1','2','3','4','5','5','6','6','7'],           
    'Time2' : ['8:10:00','9:34:00','9:48:00','10:40:00','1:30:00','2:31:00','3:35:00','3:45:00','4:55:00'],                 
    'Occurring2' : ['1','2','2','3','4','5','5','6','7'], 
    'Time3' : ['9:00:00','9:34:00','9:58:00','10:45:00','10:50:00','12:31:00','1:35:00','2:15:00','3:55:00'],                 
    'Occurring3' : ['1','2','3','4','4','5','6','7','8'],                     
     })

df = pd.DataFrame(data = d)

fig, ax = plt.subplots(figsize = (10,6))

ax.plot(df['Time1'], df['Occurring1'])
ax.plot(df['Time2'], df['Occurring2'])
ax.plot(df['Time3'], df['Occurring3'])

plt.show()

Tags: 方法importdfdataplotaspltax
1条回答
网友
1楼 · 发布于 2024-05-04 08:40:01

为了得到想要的结果,你需要跳过几圈。首先,您需要创建一个规则的时间网格,在该网格上插值y数据(引用)。然后,可以得到插值数据的最小值、最大值和平均值。下面的代码演示了如何执行此操作:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata

# Example data
d = ({
    'Time1' : ['8:00:00','9:30:00','9:40:00','10:25:00','12:30:00','1:31:00','1:35:00','2:45:00','4:50:00'],
    'Occurring1' : ['1','2','3','4','5','5','6','6','7'],
    'Time2' : ['8:10:00','9:34:00','9:48:00','10:40:00','1:30:00','2:31:00','3:35:00','3:45:00','4:55:00'],
    'Occurring2' : ['1','2','2','3','4','5','5','6','7'],
    'Time3' : ['9:00:00','9:34:00','9:58:00','10:45:00','10:50:00','12:31:00','1:35:00','2:15:00','3:55:00'],
    'Occurring3' : ['1','2','3','4','4','5','6','7','8'],
})

# Create dataframe, explicitly define dtypes
df = pd.DataFrame(data=d)
df = df.astype({
    "Time1": np.datetime64,
    "Occurring1": np.int,
    "Time2": np.datetime64,
    "Occurring2": np.int,
    "Time3": np.datetime64,
    "Occurring3": np.int,
})

# Create 1D vectors of time data
all_times = df[["Time1", "Time2", "Time3"]].values

# Representation of 1 minute in time
t_min = np.timedelta64(int(60*1e9), "ns")
# Create a regular time grid with 10 minute spacing
time_grid = np.arange(all_times.min(), all_times.max(), 10*t_min, dtype="datetime64")

# Storage buffer for interpolated occurring data
occurrences_grid = np.zeros((3, len(time_grid)))

# Loop over all occurrence data and interpolate to regular grid
for i in range(3):
    occurrences_grid[i] = griddata(
        points=df["Time%i" % (i+1)].values.astype("float"),
        values=df["Occurring%i" % (i+1)],
        xi=time_grid.astype("float"),
        method="linear"
    )

# Get min, max, and mean values of interpolated data
occ_min = np.min(occurrences_grid, axis=0)
occ_max = np.max(occurrences_grid, axis=0)
occ_mean = np.mean(occurrences_grid, axis=0)

# Plot interpolated data
plt.fill_between(time_grid, occ_min, occ_max, color="slategray")
plt.plot(time_grid, occ_mean, c="white")
plt.xticks(rotation=60)
plt.tight_layout()
plt.show()

结果(x标签格式不正确):

enter image description here

相关问题 更多 >