Skip to content

Commit 0b057b9

Browse files
Show/Hide Empty Months feature
Adding the ability to initialize a figure with the option to hide months that have empty/NaN values
1 parent c12cc6f commit 0b057b9

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Diff for: plotly_calplot/calplot.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def calplot(
7575
start_month: int = 1,
7676
end_month: int = 12,
7777
date_fmt: str = "%Y-%m-%d",
78+
show_empty_months: bool = True,
7879
) -> go.Figure:
7980
"""
8081
Yearly Calendar Heatmap
@@ -155,6 +156,9 @@ def calplot(
155156
date format for the date column in data, defaults to "%Y-%m-%d"
156157
If the date column is already in datetime format, this parameter
157158
will be ignored.
159+
160+
show_empty_months : bool = True
161+
if True heatmaps will display months that have no data
158162
"""
159163
data[x] = validate_date_column(data[x], date_fmt)
160164
unique_years = data[x].dt.year.unique()
@@ -199,9 +203,10 @@ def calplot(
199203

200204
for i, year in enumerate(unique_years):
201205
selected_year_data = data.loc[data[x].dt.year == year]
202-
selected_year_data = fill_empty_with_zeros(
203-
selected_year_data, x, year, start_month, end_month
204-
)
206+
if show_empty_months:
207+
selected_year_data = fill_empty_with_zeros(
208+
selected_year_data, x, year, start_month, end_month
209+
)
205210

206211
year_calplot(
207212
selected_year_data,
@@ -224,6 +229,7 @@ def calplot(
224229
years_as_columns=years_as_columns,
225230
start_month=start_month,
226231
end_month=end_month,
232+
show_empty_months=show_empty_months,
227233
)
228234

229235
fig = apply_general_colorscaling(fig, cmap_min, cmap_max)

Diff for: plotly_calplot/date_extractors.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import numpy as np
44
import pandas as pd
5+
import calendar
56

67

78
def get_month_names(
8-
data: pd.DataFrame, x: str, start_month: int = 1, end_month: int = 12
9+
data: pd.DataFrame, x: str, start_month: int = 1, end_month: int = 12, show_empty_months = True
910
) -> List[str]:
10-
start_month_names_filler = [None] * (start_month - 1)
11-
end_month_names_filler = [None] * (12 - end_month)
12-
month_names = list(
13-
start_month_names_filler
14-
+ data[x].dt.month_name().unique().tolist()
15-
+ end_month_names_filler
16-
)
11+
if show_empty_months:
12+
start_month_names_filler = [None] * (start_month - 1)
13+
end_month_names_filler = [None] * (12 - end_month)
14+
month_names = list(
15+
start_month_names_filler
16+
+ data[x].dt.month_name().unique().tolist()
17+
+ end_month_names_filler
18+
)
19+
else:
20+
month_names = list(calendar.month_name)[1:]
21+
1722
return month_names
1823

1924

Diff for: plotly_calplot/single_year_calplot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ def year_calplot(
3333
years_as_columns: bool = False,
3434
start_month: int = 1,
3535
end_month: int = 12,
36+
show_empty_months = True,
3637
) -> go.Figure:
3738
"""
3839
Each year is subplotted separately and added to the main plot
3940
"""
4041

41-
month_names = get_month_names(data, x, start_month, end_month)
42+
month_names = get_month_names(data, x, start_month, end_month, show_empty_months)
4243
month_positions, weekdays_in_year, weeknumber_of_dates = get_date_coordinates(
4344
data, x
4445
)

0 commit comments

Comments
 (0)