Skip to content

Commit ac3e631

Browse files
Updated plot methods to use new plt.style.context() (#338)
2 parents 4a760d9 + 40b6198 commit ac3e631

29 files changed

Lines changed: 2926 additions & 2871 deletions

LadybugTools_Engine/Python/src/ladybugtools_toolkit/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
# override "HOME" in case this is set to something other than default for windows
2020
os.environ["HOME"] = (Path("C:/Users/") / getpass.getuser()).as_posix()
2121

22-
import python_toolkit
23-
# set plotting style for modules within this toolkit
24-
plt.style.use(Path(list(python_toolkit.__path__)[0]).absolute() / "bhom" / "bhom.mplstyle")
25-
2622
# get dataset paths
2723
SRI_DATA = DATA_DIRECTORY / "sri_data.csv"
2824
KOEPPEN_DATA = DATA_DIRECTORY / "koeppen.csv"

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/directional_solar_radiation.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
def directional_solar_radiation(epw_file, directions, tilts, irradiance_type, analysis_period, cmap, title, save_path) -> str:
8181
try:
82+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
83+
8284
if cmap not in plt.colormaps():
8385
cmap = "YlOrRd"
8486

@@ -93,11 +95,12 @@ def directional_solar_radiation(epw_file, directions, tilts, irradiance_type, an
9395
elif irradiance_type == "Reflected":
9496
irradiance_type = IrradianceType.REFLECTED
9597

96-
fig, ax = plt.subplots(1, 1, figsize=(22.8/2, 7.6/2))
97-
values, dirs, tts = create_radiation_matrix(Path(epw_file), rad_type=irradiance_type, analysis_period=analysis_period, directions=directions, tilts=tilts)
98-
tilt_orientation_factor(Path(epw_file), ax=ax, rad_type=irradiance_type, analysis_period=analysis_period, directions=directions, tilts=tilts, cmap=cmap)
99-
if not (title == "" or title is None):
100-
ax.set_title(title)
98+
with plt.style.context(style):
99+
fig, ax = plt.subplots(1, 1, figsize=(22.8/2, 7.6/2))
100+
values, dirs, tts = create_radiation_matrix(Path(epw_file), rad_type=irradiance_type, analysis_period=analysis_period, directions=directions, tilts=tilts)
101+
tilt_orientation_factor(Path(epw_file), ax=ax, rad_type=irradiance_type, analysis_period=analysis_period, directions=directions, tilts=tilts, cmap=cmap, style_context=style)
102+
if not (title == "" or title is None):
103+
ax.set_title(title)
101104

102105
return_dict = {}
103106

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/diurnal.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
44
import json
5+
import os
56
import sys
67
import traceback
78
from pathlib import Path
@@ -66,17 +67,19 @@
6667

6768
def diurnal(epw_file, data_type_key="Dry Bulb Temperature", colour="#000000", title=None, period="monthly", save_path = None) -> str:
6869
try:
70+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
6971
epw = EPW(epw_file)
7072

7173
if data_type_key == "Wet Bulb Temperature":
7274
coll = wet_bulb_temperature(epw)
7375
else:
7476
coll = HourlyContinuousCollection.from_dict([a for a in epw.to_dict()["data_collections"] if a["header"]["data_type"]["name"] == data_type_key][0])
75-
76-
fig, ax = plt.subplots()
7777

78-
dnal(collection_to_series(coll), ax=ax, title=title, period=period, color=colour)
79-
return_dict = {"data": collection_metadata(coll)}
78+
with plt.style.context(style):
79+
fig, ax = plt.subplots()
80+
81+
dnal(collection_to_series(coll), ax=ax, title=title, period=period, color=colour, style_context=style)
82+
return_dict = {"data": collection_metadata(coll)}
8083

8184
if save_path == None or save_path == "":
8285
base64 = figure_to_base64(fig, html=False)

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/facade_condensation_risk_chart.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap creation of diurnal plots"""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
import textwrap
56

67
from pathlib import Path
@@ -50,10 +51,12 @@
5051

5152

5253
def facade_condensation_risk_chart(epw_file: str, thresholds: list[float], save_path: str = None) -> None:
54+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
55+
5356
epw = EPW(epw_file)
5457
hcc = epw.dry_bulb_temperature
5558

56-
fig = facade_condensation_risk_chart_table(epw_file, thresholds)
59+
fig = facade_condensation_risk_chart_table(epw_file, thresholds, style_context=style)
5760

5861
return_dict = {"data": collection_metadata(hcc)}
5962

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/facade_condensation_risk_heatmap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap creation of diurnal plots"""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
import textwrap
56

67
from pathlib import Path
@@ -49,10 +50,12 @@
4950
)
5051

5152
def facade_condensation_risk_heatmap(epw_file: str, thresholds: list[float], save_path: str = None) -> None:
53+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
54+
5255
epw = EPW(epw_file)
5356
hcc = epw.dry_bulb_temperature
5457

55-
fig = facade_condensation_risk_heatmap_histogram(epw_file, thresholds)
58+
fig = facade_condensation_risk_heatmap_histogram(epw_file, thresholds, style_context=style)
5659

5760
return_dict = {"data": collection_metadata(hcc)}
5861

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/heatmap.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap for conversion of EPW to CSV file."""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
from pathlib import Path
56
import json
67
import sys
@@ -54,19 +55,21 @@
5455
def heatmap(epw_file: str, data_type_key: str, colour_map: str, save_path:str = None) -> str:
5556
"""Create a CSV file version of an EPW."""
5657
try:
58+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
5759
if colour_map not in plt.colormaps():
5860
colour_map = "YlGnBu"
5961

60-
fig, ax = plt.subplots()
62+
with plt.style.context(style):
63+
fig, ax = plt.subplots()
6164

62-
epw = EPW(epw_file)
65+
epw = EPW(epw_file)
6366

64-
if data_type_key == "Wet Bulb Temperature":
65-
coll = wet_bulb_temperature(epw)
66-
else:
67-
coll = HourlyContinuousCollection.from_dict([a for a in epw.to_dict()["data_collections"] if a["header"]["data_type"]["name"] == data_type_key][0])
67+
if data_type_key == "Wet Bulb Temperature":
68+
coll = wet_bulb_temperature(epw)
69+
else:
70+
coll = HourlyContinuousCollection.from_dict([a for a in epw.to_dict()["data_collections"] if a["header"]["data_type"]["name"] == data_type_key][0])
6871

69-
hmap(collection_to_series(coll), ax=ax, cmap=colour_map)
72+
hmap(collection_to_series(coll), ax=ax, cmap=colour_map, style_context=style)
7073

7174
return_dict = {}
7275

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/sunpath.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap creation of sunpath plots"""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
import sys
56
import traceback
67
from pathlib import Path
@@ -52,11 +53,13 @@
5253

5354
def sunpath(epw_file, analysis_period, size, save_path) -> str:
5455
try:
55-
fig, ax = plt.subplots()
56+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
57+
with plt.style.context(style):
58+
fig, ax = plt.subplots()
5659

57-
analysis_period = AnalysisPeriod.from_dict(json.loads(analysis_period))
58-
epw = EPW(epw_file)
59-
spath(location=epw.location, analysis_period=analysis_period, sun_size=size, ax=ax)
60+
analysis_period = AnalysisPeriod.from_dict(json.loads(analysis_period))
61+
epw = EPW(epw_file)
62+
spath(location=epw.location, analysis_period=analysis_period, sun_size=size, ax=ax, style_context=style)
6063

6164
return_dict = {"data": sunpath_metadata(Sunpath.from_location(epw.location))}
6265

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/utci_heatmap.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap UTCI plots"""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
import sys
56
import traceback
67
import matplotlib
@@ -42,6 +43,7 @@
4243

4344
def utci_heatmap(input_json:str, save_path = None, epw_file:str = None) -> str:
4445
try:
46+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
4547

4648
if not input_json.startswith("{"): #assume it's a path
4749
with open(input_json, "r") as f:
@@ -61,14 +63,15 @@ def utci_heatmap(input_json:str, save_path = None, epw_file:str = None) -> str:
6163
colors=(bin_colours),
6264
name="UTCI")
6365

64-
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
65-
ec.plot_utci_heatmap(utci_categories = custom_bins, ax=ax)
66+
with plt.style.context(style):
67+
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
68+
ec.plot_utci_heatmap(utci_categories = custom_bins, ax=ax, style_context=style)
6669

67-
utci_collection = ec.universal_thermal_climate_index
70+
utci_collection = ec.universal_thermal_climate_index
6871

69-
return_dict = {"data": utci_metadata(utci_collection), "external_comfort": ec.to_dict()}
72+
return_dict = {"data": utci_metadata(utci_collection), "external_comfort": ec.to_dict()}
7073

71-
plt.tight_layout()
74+
plt.tight_layout()
7275

7376
if save_path == None or save_path == "":
7477
base64 = figure_to_base64(fig,html=False)

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/walkability_heatmap.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import os
23
import sys
34
import matplotlib
45
import traceback
@@ -38,18 +39,21 @@
3839

3940
def walkability_heatmap(input_json: str, save_path: str, epw_file:str = None) -> str:
4041
try:
42+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
4143
argsDict = json.loads(input_json)
4244

4345
ec = ExternalComfort.from_dict(json.loads(argsDict["external_comfort"]))
44-
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
45-
ec.plot_walkability_heatmap(ax=ax)
4646

47-
#TODO: create walkability collection metadata
48-
utci_collection = ec.universal_thermal_climate_index
47+
with plt.style.context(style):
48+
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
49+
ec.plot_walkability_heatmap(ax=ax, style_context=style)
4950

50-
return_dict = {"data": utci_metadata(utci_collection), "external_comfort": ec.to_dict()}
51+
#TODO: create walkability collection metadata
52+
utci_collection = ec.universal_thermal_climate_index
5153

52-
plt.tight_layout()
54+
return_dict = {"data": utci_metadata(utci_collection), "external_comfort": ec.to_dict()}
55+
56+
plt.tight_layout()
5357

5458
if save_path == None or save_path == "":
5559
base64 = figure_to_base64(fig,html=False)

LadybugTools_Engine/Python/src/ladybugtools_toolkit/bhom/wrapped/plot/windrose.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Method to wrap for creating wind roses from epw files."""
22
# pylint: disable=C0415,E0401,W0703
33
import argparse
4+
import os
45
import sys
56
import traceback
67
from pathlib import Path
@@ -59,22 +60,24 @@
5960
def windrose(epw_file: str, analysis_period: str, colour_map: str, bins: int, save_path: str = None) -> str:
6061
"""Method to wrap for creating wind roses from epw files."""
6162
try:
63+
style = os.environ.get("BHOM_style_context", "python_toolkit.bhom")
6264
if colour_map not in plt.colormaps():
6365
colour_map = "YlGnBu"
6466

6567
epw = EPW(epw_file)
6668
analysis_period = AnalysisPeriod.from_dict(json.loads(analysis_period))
6769
w_epw = Wind.from_epw(epw_file)
6870

69-
fig, ax = plt.subplots(1, 1, figsize=(6, 6), subplot_kw={"projection": "polar"})
71+
with plt.style.context(style):
72+
fig, ax = plt.subplots(1, 1, figsize=(6, 6), subplot_kw={"projection": "polar"})
7073

71-
wind_filtered = w_epw.filter_by_analysis_period(analysis_period=analysis_period)
74+
wind_filtered = w_epw.filter_by_analysis_period(analysis_period=analysis_period)
7275

73-
w_epw.filter_by_analysis_period(analysis_period=analysis_period).plot_windrose(ax=ax, directions=bins, ylim=(0, 3.6/bins), colors=colour_map)
76+
wind_filtered.plot_windrose(ax=ax, directions=bins, ylim=(0, 3.6/bins), colors=colour_map, style_context=style)
7477

75-
return_dict = {"data": wind_metadata(wind_filtered, directions=bins)}
78+
return_dict = {"data": wind_metadata(wind_filtered, directions=bins)}
7679

77-
plt.tight_layout()
80+
plt.tight_layout()
7881
if save_path == None or save_path == "":
7982
return_dict["figure"] = figure_to_base64(fig,html=False)
8083
else:

0 commit comments

Comments
 (0)