python - 如何使用 pandas 和 matplotlib 生成离散数据以传递到等高线图?

标签 python pandas matplotlib graph

我有两组连续数据,我想将它们传递到等高线图中。 x 轴是时间,y 轴是质量,z 轴是频率(如数据点出现的次数)。然而,大多数数据点并不相同,而是非常相似。因此,我怀疑对 x 轴和 y 轴进行离散化是最简单的。

这是我目前拥有的数据:

输入

import pandas as pd
df = pd.read_excel('data.xlsx')
df['Dates'].head(5)
df['Mass'].head(5)

输出

13   2003-05-09
14   2003-09-09
15   2010-01-18
16   2010-11-21
17   2012-06-29
Name: Date, dtype: datetime64[ns]

13    2500.0
14    3500.0
15    4000.0
16    4500.0
17    5000.0
Name: Mass, dtype: float64

我想转换数据,以便将一年内的数据点分组(例如:2003 年采集的所有数据点),并将不同质量水平内的数据点分组(例如:3000-4000 之间的所有数据点)公斤)。接下来,代码将计算每个 block 内有多少个数据点,并将其作为 z 轴传递。

理想情况下,我还希望能够调整切片的级别。例如:每 100 公斤而不是 1000 公斤对点进行分组,或者传递不均匀分布的自定义级别列表。我该如何去做呢?

最佳答案

我认为您正在寻找的函数是 pd.cut

import pandas as pd
import numpy as np
import datetime

n = 10
scale = 1e3
Min = 0
Max = 1e4

np.random.seed(6)

Start = datetime.datetime(2000, 1, 1)
Dates = np.array([base + datetime.timedelta(days=i*180) for i in range(n)])
Mass = np.random.rand(n)*10000
df = pd.DataFrame(index = Dates, data = {'Mass':Mass})

print(df)

给你:

                   Mass
2000-01-01  8928.601514
2000-06-29  3319.798053
2000-12-26  8212.291231
2001-06-24   416.966257
2001-12-21  1076.566799
2002-06-19  5950.520642
2002-12-16  5298.173622
2003-06-14  4188.074286
2003-12-11  3354.078493
2004-06-08  6225.194322

如果您想按 1000 为单位对 Masses 进行分组,或者实现您自己的自定义 bin,您可以执行以下操作:

Bins,Labels=np.arange(Min,Max+.1,scale),(np.arange(Min,Max,scale))+(scale)/2
EqualBins = pd.cut(df['Mass'],bins=Bins,labels=Labels)
df.insert(1,'Equal Bins',EqualBins)

Bins,Labels=[0,1000,5000,10000],['Small','Medium','Big']
CustomBins = pd.cut(df['Mass'],bins=Bins,labels=Labels)
df.insert(2,'Custom Bins',CustomBins)

如果你只想显示年份、月份等,这很简单:

df['Year'] = df.index.year
df['Month'] = df.index.month

但如果您愿意,您也可以自定义日期范围:

Bins=[datetime.datetime(1999, 12, 31),datetime.datetime(2000, 9, 1),
      datetime.datetime(2002, 1, 1),datetime.datetime(2010, 9, 1)]


Labels = ['Early','Middle','Late']
CustomDateBins = pd.cut(df.index,bins=Bins,labels=Labels)
df.insert(3,'Custom Date Bins',CustomDateBins)

print(df)

这会产生你想要的东西:

                   Mass Equal Bins Custom Bins Custom Date Bins  Year  Month
2000-01-01  8928.601514     8500.0         Big            Early  2000      1
2000-06-29  3319.798053     3500.0      Medium            Early  2000      6
2000-12-26  8212.291231     8500.0         Big           Middle  2000     12
2001-06-24   416.966257      500.0       Small           Middle  2001      6
2001-12-21  1076.566799     1500.0      Medium           Middle  2001     12
2002-06-19  5950.520642     5500.0         Big             Late  2002      6
2002-12-16  5298.173622     5500.0         Big             Late  2002     12
2003-06-14  4188.074286     4500.0      Medium             Late  2003      6
2003-12-11  3354.078493     3500.0      Medium             Late  2003     12
2004-06-08  6225.194322     6500.0         Big             Late  2004      6

您可能也对 .groupby 函数感兴趣:

yeargroup = df.groupby(df.index.year).mean()
massgroup = df.groupby(df['Equal Bins']).count()
print(yeargroup)
print(massgroup)

             Mass    Year     Month
2000  6820.230266  2000.0  6.333333
2001   746.766528  2001.0  9.000000
2002  5624.347132  2002.0  9.000000
2003  3771.076389  2003.0  9.000000
2004  6225.194322  2004.0  6.000000
            Mass  Custom Bins  Custom Date Bins  Year  Month
Equal Bins                                                  
500.0          1            1                 1     1      1
1500.0         1            1                 1     1      1
2500.0         0            0                 0     0      0
3500.0         2            2                 2     2      2
4500.0         1            1                 1     1      1
5500.0         2            2                 2     2      2
6500.0         1            1                 1     1      1
7500.0         0            0                 0     0      0
8500.0         2            2                 2     2      2
9500.0         0            0                 0     0      0

关于python - 如何使用 pandas 和 matplotlib 生成离散数据以传递到等高线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862177/

相关文章:

python - 使用 Python/numpy 过滤 CSV 数据

python - 来自聚合结果的 Pandas DataFrame 分组箱线图

python - Matplotlib 的 inset_locator 带有倒轴

python - 基维的时间. sleep

Python、Ubuntu : Install PyPi package for a specific Python version

python - numpy 中带有广播的双点积

python - PyPI 贝塞尔曲线 0.8.0 : Minimum number of points needed to plot a smooth bezier curve?

python - 仅提取字母和第一位数字

python - 如何将名称添加到多个列?

python - 如何在Python条形图中添加x轴刻度标签