【问题标题】:Calculate the percentage of occurances in a data set计算数据集中出现的百分比
【发布时间】:2021-04-28 23:06:41
【问题描述】:

我有一个 N 风速读数(m.s^-1)的数据集,在本例中为 10,作为数组 [2,3,3,4,4,4,4,6,5,5]。我需要编写一个 python 程序来读取数组,然后绘制风速与风速为该速度或更大的时间百分比的图表。

在这种情况下,风速为 2m.s^-1 或大于 100%(1) 的时间和 6m.s^-1 的 10%(0.1)。

我试图实现的示例图看起来像这样,但我试图编写的程序需要能够获取任何大小的数据集。

【问题讨论】:

标签: python


【解决方案1】:

一个简单的实现:

import matplotlib.pyplot as plt

def plot_velocity_duration_curve(sites):
  for i, site in enumerate(sites, 1):
    windspeed = sorted(site, reverse=True)
    fractions = [j/len(site) for j in range(1, len(site)+1)]
    plt.plot(fractions, windspeed, '-o', label=f'site {i}')
  plt.legend(loc='upper right')
  plt.title('Velocity duration curves')
  plt.xlabel('Fraction of time')
  plt.ylabel('Wind speed (m/s)')
  plt.xticks([i/10 for i in range(1,11)])
  plt.grid()
  plt.show()

site1 = [2, 3, 3, 4, 4, 4, 4, 6, 5, 5]
site2 = [2, 2, 6, 3, 7, 6, 1, 5, 6, 2]

plot_velocity_duration_curve([site1, site2])

以及使用numpy 更高效的实现:

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

def plot_velocity_duration_curve(sites):
  fractions = np.arange(1.,len(sites)+1) /len(sites)
  windspeed = np.sort(sites, axis=0)[::-1]
  lines = plt.plot(fractions, windspeed, '-o', label=f'site')
  plt.legend(lines, [f"site {i}" for i in range(1, len(lines)+1)])
  plt.title('Velocity duration curves')
  plt.xlabel('Fraction of time')
  plt.ylabel('Wind speed (m/s)')
  plt.xticks(np.arange(0, 1.1, step=0.1))
  plt.grid()
  plt.show()


site1 = np.random.rayleigh(10,20)
site2 = np.random.rayleigh(9,20)
site3 = np.random.normal(10,5,20)
sites = np.c_[site1, site2, site3]
plot_velocity_duration_curve(sites)

请参阅 this answer 了解更广泛的实施。

【讨论】:

    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2023-03-09
    • 2012-09-16
    相关资源
    最近更新 更多