Skip to content

Commit

Permalink
iss #309 improved display by changing font size autmoatically, and ad…
Browse files Browse the repository at this point in the history
…ded test to test_plot.py
  • Loading branch information
rach185 committed May 16, 2022
1 parent 45559ad commit da23373
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
29 changes: 14 additions & 15 deletions brightwind/analyse/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,17 +1112,17 @@ def sector_ratio(wspd_1, wspd_2, wdir, sectors=72, min_wspd=3, direction_bin_arr
"""
Calculates the wind speed ratio of two wind speed time series and plots this ratio, averaged by direction sector,
in a polar plot using a wind direction time series. The averaged ratio by sector can be optionally returned
in a pd.DataFrame.
in a pd.DataFrame. If provided with multiple time series, multiple subplots will be produced.
If boom directions are specified, these will be overlaid on the plot. A boom direction of '-1' assumes top
mounted and so doesn't plot.
:param wspd_1: First wind speed time series. This is divisor.
:type: wspd_1: pandas.Series
:param wspd_2: Second wind speed time series, dividend.
:type: wspd_2: pandas.Series
:param wdir: Series of wind directions
:type wdir: pandas.Series
:param wspd_1: First wind speed time series. One or more wind speeds can be accepted. This is the divisor.
:type: wspd_1: pandas.Series or pandas.DataFrame
:param wspd_2: Second wind speed time series, the dividend. One or more wind speeds can be accepted.
:type: wspd_2: pandas.Series or pandas.DataFrame
:param wdir: Time series of wind directions. One or more can be accepted.
:type wdir: pandas.Series or pandas.DataFrame
:param sectors: Set the number of direction sectors. Usually 12, 16, 24, 36 or 72.
:type sectors: int
:param min_wspd: Minimum wind speed to be used.
Expand Down Expand Up @@ -1191,11 +1191,10 @@ def sector_ratio(wspd_1, wspd_2, wdir, sectors=72, min_wspd=3, direction_bin_arr
raise ValueError('Number of anemometers is uneven. ' +
'Please ensure same number of anemometers in wspd_1 and wspd_2.')

if len(wd.columns) != 1:
if len(wd.columns) != len(ws_1.columns):
raise ValueError('Number of anemometers does not match number of wind vanes. ' +
'Please ensure there is one direction vane per anemometer pair or ' +
'include one direcion vane only to be used for all anemometer pairs.')
if (len(wd.columns) != 1) & (len(wd.columns) != len(ws_1.columns)):
raise ValueError('Number of anemometers does not match number of wind vanes. ' +
'Please ensure there is one direction vane per anemometer pair or ' +
'include one direcion vane only to be used for all anemometer pairs.')

keys = range(len(ws_1.columns))
sec_rats = {}
Expand All @@ -1221,13 +1220,13 @@ def sector_ratio(wspd_1, wspd_2, wdir, sectors=72, min_wspd=3, direction_bin_arr
aggregation_method='mean',
direction_bin_array=direction_bin_array,
direction_bin_labels=None, return_data=True)
matplotlib.pyplot.close()

sec_rat_dist = sec_rat_dist.rename('Mean_Sector_Ratio').to_frame()
sec_rats_dists[sensor_pair] = sec_rat_dist

fig = plt.plot_sector_ratio(sec_ratio=sec_rats, wdir=wdir, sec_ratio_dist=sec_rats_dists, col_names=col_names,
boom_dir_1=boom_dir_1, boom_dir_2=boom_dir_2,
radial_limits=radial_limits, figure_size=figure_size)
boom_dir_1=boom_dir_1, boom_dir_2=boom_dir_2,
radial_limits=radial_limits, figure_size=figure_size)

if return_data:
if len(sec_rats_dists) == 1:
Expand Down
23 changes: 17 additions & 6 deletions brightwind/analyse/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
'plot_scatter',
'plot_scatter_wspd',
'plot_scatter_wdir',
'plot_scatter_by_sector']
'plot_scatter_by_sector',
'plot_sector_ratio']
#
# try:
# if 'Gotham Rounded' in \
Expand Down Expand Up @@ -1009,6 +1010,8 @@ def plot_sector_ratio(sec_ratio, wdir, sec_ratio_dist, col_names, boom_dir_1=-1,
:type boom_dir_2: float
:param radial_limits: the min and max values of the radial axis. Defaults to +0.05 of max ratio and -0.1 of min.
:type radial_limits: tuple or list
:param figure_size: Figure size in tuple format (width, height)
:type figure_size: tuple
:returns A speed ratio plot showing average speed ratio by sector and scatter of individual data points.
Expand All @@ -1022,7 +1025,7 @@ def plot_sector_ratio(sec_ratio, wdir, sec_ratio_dist, col_names, boom_dir_1=-1,
if len(wdir.columns) != 1:
if len(wdir.columns) != len(sec_ratio):
raise ValueError('Number of anemometers does not match number of wind vanes. Please ensure there is one ' +
'direction vane per anemometer pair or include one direcion vane only to be used for ' +
'direction vane per anemometer pair or include one direction vane only to be used for ' +
'all anemometer pairs.')

row, col = _get_best_row_col_number_for_subplot(len(sec_ratio))
Expand All @@ -1043,13 +1046,14 @@ def plot_sector_ratio(sec_ratio, wdir, sec_ratio_dist, col_names, boom_dir_1=-1,

_plot_sector_ratio_subplot(sec_ratio[pair].loc[common_idx], wd.loc[common_idx], sec_ratio_dist[pair],
col_names[pair], boom_dir_1=boom_dir_1, boom_dir_2=boom_dir_2,
ax=axes[pair], radial_limits=radial_limits)
ax=axes[pair], radial_limits=radial_limits, figure_size=figure_size,
subplot_dimension=[row,col])
plt.close()

return fig

def _plot_sector_ratio_subplot(sec_ratio, wdir, sec_ratio_dist, col_names, boom_dir_1=-1, boom_dir_2=-1,
ax=None, radial_limits=None):
ax=None, radial_limits=None, figure_size=(10,10), subplot_dimension=(1,1)):
"""
Accepts a ratio of anemometers per sector, a wind direction, a distribution of anemometer ratios per sector,
along with 2 anemometer names, and returns an axis object to plot the speed ratio by sector. Optionally can
Expand All @@ -1068,10 +1072,14 @@ def _plot_sector_ratio_subplot(sec_ratio, wdir, sec_ratio_dist, col_names, boom_
:type boom_dir_1: float
:param boom_dir_2: Boom direction in degrees of speed_col_name_2. Defaults to -1.
:type boom_dir_2: float
:param ax: Subplot axes to which the subplot is assinged in a plot. If None subplot is displayed on its own.
:param ax: Subplot axes to which the subplot is assigned in a plot. If None subplot is displayed on its own.
:type ax: matplotlib.axes._subplots.AxesSubplot or None
:param radial_limits: the min and max values of the radial axis. Defaults to +0.05 of max ratio and -0.1 of min.
:type radial_limits: tuple or list
:param figure_size: Size of radial plot. Defaults to (10,10).
:type figure_size: tuple
:param subplot_dimension: Dimensions of subplots by (row,column), if applicable. Defaults to 1 row x 1 column.
:type subplot_dimension: tuple or list
:returns A speed ratio plot showing average speed ratio by sector and scatter of individual data points.
Expand Down Expand Up @@ -1119,9 +1127,12 @@ def _plot_sector_ratio_subplot(sec_ratio, wdir, sec_ratio_dist, col_names, boom_
col_names[0], boom_dir_1)
annotate = True
if annotate:
ax.set_title(annotation_text, y=-0.1, fontsize=plt.rcParams['ytick.labelsize'])
ax.set_title(annotation_text, y=0.004*min(figure_size)/subplot_dimension[0]/subplot_dimension[1]-0.15)
ax.scatter(np.radians(wdir), sec_ratio, color=COLOR_PALETTE.secondary, alpha=0.3, s=1)

for item in ([ax.title] + ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(min(figure_size)/subplot_dimension[0]/subplot_dimension[1]+2.5)

return ax


Expand Down
18 changes: 18 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
WSPD_COLS = ['Spd80mN', 'Spd80mS', 'Spd60mN', 'Spd60mS', 'Spd40mN', 'Spd40mS']
WDIR_COLS = ['Dir78mS', 'Dir58mS', 'Dir38mS']

def test_plot_sector_ratio():
wspd_1 = bw.utils.utils._convert_df_to_series(DATA['Spd80mN']).dropna()
wspd_2 = bw.utils.utils._convert_df_to_series(DATA['Spd80mS']).dropna()
wdir = bw.utils.utils._convert_df_to_series(DATA['Dir78mS']).dropna()

sec_rats = {}
sec_rats_dists = {}

sec_rat = bw.analyse.analyse._calc_ratio(wspd_1, wspd_2, 3)
common_idx = sec_rat.index.intersection(wdir.index)
sec_rat_plot, sec_rat_dist = bw.dist_by_dir_sector(sec_rat.loc[common_idx], wdir.loc[common_idx], return_data=True)
sec_rat_dist = sec_rat_dist.rename('Mean_Sector_Ratio').to_frame()
sec_rats[0] = sec_rat
sec_rats_dists[0] = sec_rat_dist

fig = bw.plot_sector_ratio(sec_rats, DATA['Dir78mS'], sec_rats_dists, [DATA['Spd80mN'].name, DATA['Spd80mS'].name],
boom_dir_1=-1, boom_dir_2=-1, radial_limits=None, figure_size=(10, 10))


def test_plot_timeseries():
bw.plot_timeseries(DATA[['Spd40mN', 'Spd60mS', 'T2m']])
Expand Down

0 comments on commit da23373

Please sign in to comment.