Skip to content

Commit

Permalink
fix(to_figure): control legend
Browse files Browse the repository at this point in the history
1. control number of labels
2. control what's displayed on those labels
  • Loading branch information
devang-chauhan committed Jan 19, 2022
1 parent a4dbd96 commit df2612b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ladybug_charts/_extend_ladybug.py
@@ -1,5 +1,6 @@
"""Add capability to generate a Figures from ladybug data objects. """

from typing import List
from plotly.graph_objects import Figure
from ladybug.datacollection import HourlyContinuousCollection, \
HourlyDiscontinuousCollection, MonthlyCollection, DailyCollection
Expand All @@ -22,12 +23,14 @@
Sunpath.plot = sunpath


def hourly_plot(self, title: str = None, show_title: bool = False) -> Figure:
def hourly_plot(self, title: str = None, show_title: bool = False,
num_labels: int = None, labels: List[float] = None) -> Figure:
hourly_data = self.data_collection
min_range = self.legend_parameters.min
max_range = self.legend_parameters.max
colors = self.legend_parameters.colors
return heat_map(hourly_data, min_range, max_range, colors, title, show_title)
return heat_map(hourly_data, min_range, max_range, colors, title, show_title,
num_labels, labels)


HourlyPlot.plot = hourly_plot
Expand Down
10 changes: 8 additions & 2 deletions ladybug_charts/to_figure.py
Expand Up @@ -41,7 +41,8 @@

def heat_map(hourly_data: Union[HourlyContinuousCollection, HourlyDiscontinuousCollection],
min_range: float = None, max_range: float = None,
colors: List[Color] = None, title: str = None, show_title: bool = False) -> Figure:
colors: List[Color] = None, title: str = None, show_title: bool = False,
num_labels: int = None, labels: List[float] = None) -> Figure:
"""Create a plotly heat map figure from Ladybug Hourly data.
Args:
Expand All @@ -55,6 +56,8 @@ def heat_map(hourly_data: Union[HourlyContinuousCollection, HourlyDiscontinuousC
title: A string to be used as the title of the plot. If not set, the name
of the data will be used. Defaults to None.
show_title: A boolean to show or hide the title of the chart. Defaults to False.
num_labels: The number of labels to be used in the legend. Defaults to None.
labels: A list of floats to be used as labels for the legend. Defaults to None.
Returns:
A plotly figure.
Expand Down Expand Up @@ -87,6 +90,9 @@ def heat_map(hourly_data: Union[HourlyContinuousCollection, HourlyDiscontinuousC
if not colors:
colors = color_set[ColorSet.original.value]

nticks = num_labels
dtick = labels

fig = go.Figure(
data=go.Heatmap(
y=df["hour"],
Expand All @@ -104,7 +110,7 @@ def heat_map(hourly_data: Union[HourlyContinuousCollection, HourlyDiscontinuousC
+ "</b><br>Month: %{customdata[0]}<br>Day: %{customdata[1]}<br>Hour: %{y}:00<br>"
),
name="",
colorbar=dict(title=var_unit),
colorbar=dict(title=var_unit, nticks=nticks, dtick=dtick),
)
)

Expand Down

0 comments on commit df2612b

Please sign in to comment.