Skip to content

Commit 4f6cc57

Browse files
committed
fix issue with density plots
1 parent 8ed70bf commit 4f6cc57

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

Python_Engine/Python/src/python_toolkit/plot/polar.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def polar(
2222
ax: plt.Axes = None,
2323
directions: int = 36,
2424
value_bins: List[float] = None,
25-
colors: list[str | tuple[float] | Colormap] = None, #set of colours to use for the value bins
25+
colours: list[str | tuple[float] | Colormap] = None, #set of colours to use for the value bins
2626
title: str = None,
2727
legend: bool = True,
2828
ylim: tuple[float] = None,
@@ -57,7 +57,7 @@ def polar(
5757
label (bool, optional):
5858
Set to False to remove the bin labels. Defaults to False.
5959
density (bool, optional):
60-
Set to False to see the sum of the values instead of their frequency
60+
Set to False to see the sum of the values instead of their frequency density
6161
Returns:
6262
plt.Axes: The axes object.
6363
"""
@@ -76,17 +76,17 @@ def polar(
7676
)
7777

7878
# set colors
79-
if colors is None:
80-
colors = [
79+
if colours is None:
80+
colours = [
8181
to_hex(plt.get_cmap("viridis")(i))
8282
for i in np.linspace(0, 1, len(binned.columns))
8383
]
84-
if isinstance(colors, str):
85-
colors = plt.get_cmap(colors)
86-
if isinstance(colors, Colormap):
87-
colors = [to_hex(colors(i)) for i in np.linspace(0, 1, len(binned.columns))]
88-
if isinstance(colors, list | tuple):
89-
if len(colors) != len(binned.columns):
84+
if isinstance(colours, str):
85+
colours = plt.get_cmap(colours)
86+
if isinstance(colours, Colormap):
87+
colours = [to_hex(colours(i)) for i in np.linspace(0, 1, len(binned.columns))]
88+
if isinstance(colours, list | tuple):
89+
if len(colours) != len(binned.columns):
9090
raise ValueError(
9191
f"colors must be a list of length {len(binned.columns)}, or a colormap."
9292
)
@@ -118,7 +118,7 @@ def polar(
118118
alpha=1,
119119
)
120120
)
121-
color_list.append(colors[n])
121+
color_list.append(colours[n])
122122
y += val
123123
if label:
124124
ax.text(x, y, f"{y:0.1%}", ha="center", va="center", fontsize="x-small")
@@ -131,7 +131,7 @@ def polar(
131131
# construct legend
132132
if legend:
133133
handles = [
134-
mpatches.Patch(color=colors[n], label=(f"{i} to {j}" if str(j) != str(np.inf) else f"{i} and above"))
134+
mpatches.Patch(color=colours[n], label=(f"{i} to {j}" if str(j) != str(np.inf) else f"{i} and above"))
135135
for n, (i, j) in enumerate(binned.columns.values)
136136
]
137137
_ = ax.legend(

Python_Engine/Python/src/python_toolkit/plot/utilities.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def format_polar_plot(ax: plt.Axes, yticklabels: bool = True) -> plt.Axes:
601601

602602
def process_polar_data(data:pd.DataFrame, values_column:str, directions_column:str, directions:int=36, value_bins:List[float]=None, density:bool=True):
603603
"""Process data for a polar plot by grouping by value and direction bins, either as value counts, or sums (determined by density)
604-
604+
605605
"""
606606
if values_column not in data.columns:
607607
raise ValueError(f"Values column `{values_column}` could not be found in the input dataframe.")
@@ -656,30 +656,29 @@ def process_polar_data(data:pd.DataFrame, values_column:str, directions_column:s
656656
index=dir_categories.index,
657657
name=dir_categories.name,
658658
)
659-
660-
df = pd.concat([dir_categories, categories, values_ser], axis=1)
661-
df.columns = [df.columns[0], df.columns[1], "Value"]
662659

663660
# pivot dataframe
664661
if density:
662+
df = pd.concat([dir_categories, categories], axis=1)
665663
df = (
666664
df.groupby([df.columns[0], df.columns[1]], observed=True)
667-
.value_counts()
665+
.value_counts() # TODO: allow non-density plot so that actual sums can be viewed (e.g. for directional radiation plots)
668666
.unstack()
669667
.fillna(0)
670668
.astype(int)
671669
)
672670
else:
673671
#This allows plots like radiation roses, where the sum of the radiation from that direction is more useful than the counts
672+
df = pd.concat([dir_categories, categories, values_ser], axis=1)
673+
df.columns = [df.columns[0], df.columns[1], "Value"]
674674
df = (
675675
df.groupby([df.columns[0], df.columns[1]], observed=True)
676676
.sum()
677677
.unstack()
678678
.fillna(0)
679679
.astype(float)
680680
)
681-
682-
df = df.droplevel(level=0, axis=1)
681+
df.droplevel(level=0, axis=1)
683682

684683
for b in bin_tuples:
685684
if b not in df.columns:

0 commit comments

Comments
 (0)