程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Fixing number of directional bins in windrose plot

发布于2024-11-23 21:40     阅读(833)     评论(0)     点赞(1)     收藏(0)


I'm trying to fix my windrose plot so the data are partitioned into the correct bins for direction. My data is wind data from a NOAA weather station, and the original measurements were degrees, which I converted to cardinal directions. However, I ended up with two 'Norths'.

Since the degrees are from a circle, I divided them in bins base on the number of directions I was using. However, since 'North' encompasses degrees from 0-11 AND 348-360, this means I have 2 bins for 'North'. I already tried to set the unique=True, but that causes an error and won't produce the plot. This is my code for the preprocessing, but I can add the snippets for the plot if necessary.

# Load the data
df = pd.read_csv('NOAA_AA_2015(in).csv')
df.head()
    STATION          DATE             HourlyWindDirection   HourlyWindGustSpeed HourlyWindSpeed
0   72537494889     2015-07-15T00:53:00  20.0                       NaN       10.0
1   72537494889     2015-07-15T01:53:00  20.0                       NaN       13.0
2   72537494889     2015-07-15T02:53:00  20.0                       NaN       11.0
3   72537494889     2015-07-15T03:53:00  30.0                       NaN        9.0
4   72537494889     2015-07-15T04:53:00  30.0                       NaN        9.0

# Check the range of wind speed to adjust bins accordingly
max_speed = df['strength'].max()
bins_mag = np.linspace(0, max_speed, num=7)  # Create 6 bins between 0 and max_speed
bins_mag_labels = [f'{bins_mag[i]:.1f}-{bins_mag[i+1]:.1f}' for i in range(len(bins_mag)-1)]

# Create bins for directions
bins_dir = [0, 11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.25, 303.75, 326.25, 348.75, 360.00]
bins_dir_labels = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N ']

df['strength_binned'] = pd.cut(df['strength'], bins_mag, labels=bins_mag_labels, right=False)
df['direction_binned'] = pd.cut(df['direction'], bins_dir, labels=bins_dir_labels, right=False)

# Handle the redistribution of 'N 360' data
df.loc[df['direction'] == 360, 'direction'] = 0
df['direction_binned'] = pd.cut(df['direction'], bins_dir, labels=bins_dir_labels, right=False)

# Create a new DataFrame with necessary columns and calculate frequency
dfe = df[['strength_binned', 'direction_binned', 'DATE']].copy()
dfe.rename(columns={'DATE': 'frequency'}, inplace=True)

# Group by binned magnitudes and directions, and calculate frequencies
g = dfe.groupby(['strength_binned', 'direction_binned']).count().reset_index()
g['frequency'] = g['frequency'] / g['frequency'].sum() * 100  # Convert to percentage

# Apply logarithmic transformation to frequency
g['log_frequency'] = np.log1p(g['frequency'])  # Use log1p to handle zero values

# Rename the strength_binned column to 'Wind Speed m/s'
g.rename(columns={'strength_binned': 'Wind Speed m/s'}, inplace=True)

解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2045469/fe2a432360d9ffd9c4bf/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)