发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!