-
-
Save FedericoTartarini/45f6bd68355cb061f7dd61d542e35128 to your computer and use it in GitHub Desktop.
# plot a simple psychrometric chart | |
import psychrolib | |
import matplotlib.pyplot as plt | |
import numpy as np | |
psychrolib.SetUnitSystem(psychrolib.SI) | |
pressure = 101325 | |
t_array = np.arange(5, 45, 0.1) | |
rh_array = np.arange(0, 1.1, 0.1) | |
enthalpy_array = np.arange(0, 120000, 10000) | |
hr_hor_lines = np.arange(0.005, 0.03, 0.005) | |
twb_array = np.arange(-10, 45, 5) | |
f, ax = plt.subplots() | |
# plot constant relative humidity lines | |
for rh in rh_array: | |
hr_array = [] | |
for t in t_array: | |
hr = psychrolib.GetHumRatioFromRelHum(t, rh, pressure) | |
hr_array.append(hr) | |
ax.plot(t_array, hr_array, 'k') | |
for twb in twb_array: | |
hr_array = [] | |
t_plot_array = [] | |
for t in t_array: | |
if twb <= t: | |
# print(twb, t) | |
hr = psychrolib.GetHumRatioFromTWetBulb(t, twb, pressure) | |
hr_array.append(hr) | |
t_plot_array.append(t) | |
ax.plot(t_plot_array, hr_array, 'b') | |
ax.set(ylim=(0, 0.025), xlim=(10, 40), ylabel=r"Humidity Ratio [$kg_{water}/kg_{dry air}$]", xlabel="Dry-bulb Temperature [°C]") | |
ax.yaxis.tick_right() | |
ax.yaxis.set_label_position("right") | |
plt.tight_layout() | |
plt.show() | |
# ################ | |
# Plot apsychrometric chart using psychrochart | |
# ################ | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from psychrochart import PsychroChart | |
# Pass a dict with the changes wanted: | |
custom_style = { | |
"figure": { | |
# "title": "Psychrometric Chart (sea level)", | |
"x_label": "DRY-BULB TEMPERATURE, $°C$", | |
"y_label": "HUMIDITY RATIO $w, g_w / kg_{da}$", | |
"x_axis": {"color": [1.0, 1.0, 1.0], "linewidth": 1.5, "linestyle": "-"}, | |
"x_axis_labels": {"color": [1.0, 1.0, 1.0], "fontsize": 8}, | |
"x_axis_ticks": {"direction": "out", "color": [1.0, 1.0, 1.0]}, | |
"y_axis": {"color": [1.0, 1.0, 1.0], "linewidth": 1.5, "linestyle": "-"}, | |
"y_axis_labels": {"color": [1.0, 1.0, 1.0], "fontsize": 8}, | |
"y_axis_ticks": {"direction": "out", "color": [1.0, 1.0, 1.0]}, | |
"partial_axis": False, | |
"position": [0.025, 0.075, 0.925, 0.875] | |
}, | |
"limits": { | |
"range_temp_c": [0, 40], | |
"range_humidity_g_kg": [0, 30], | |
"altitude_m": 0, | |
"step_temp": 1.0 | |
}, | |
"saturation": {"color": [1.0, 1.0, 1.0], "linewidth": 2, "linestyle": "-"}, | |
"constant_rh": {"color": [0.8, 0.0, 0.0], "linewidth": 1, "linestyle": "-"}, | |
"constant_v": {"color": [0.0, 0.4, 0.4], "linewidth": 0.5, "linestyle": "-"}, | |
"constant_h": {"color": [1, 0.4, 0.0], "linewidth": 0.75, "linestyle": "-"}, | |
"constant_wet_temp": {"color": [0.2, 0.8, 0.2], "linewidth": 1, "linestyle": "--"}, | |
"constant_dry_temp": {"color": [1.0, 1.0, 1.0], "linewidth": 0.25, "linestyle": "-"}, | |
"constant_humidity": {"color": [1.0, 1.0, 1.0], "linewidth": 0.25, "linestyle": "-"}, | |
"chart_params": { | |
"with_constant_rh": True, | |
"constant_rh_curves": [20, 30, 40, 50, 60, 70, 80, 90], | |
"constant_rh_labels": [20, 40, 60, 80], | |
"with_constant_v": False, | |
"constant_v_step": 0.01, | |
"range_vol_m3_kg": [0.78, 0.96], | |
"with_constant_h": True, | |
"constant_h_step": 10, | |
"constant_h_labels": [0], | |
"range_h": [10, 130], | |
"with_constant_wet_temp": True, | |
"constant_wet_temp_step": 1, | |
"range_wet_temp": [-10, 35], | |
"constant_wet_temp_labels": [0, 5, 10, 15, 20, 25, 30], | |
"with_constant_dry_temp": True, | |
"constant_temp_step": 5, | |
"with_constant_humidity": True, | |
"constant_humid_step": 2, | |
"with_zones": False | |
} | |
} | |
fig, ax = plt.subplots(figsize=(7, 3)) | |
chart = PsychroChart(custom_style) | |
chart.plot(ax) | |
# # Append zones: | |
# zones_conf = { | |
# "zones":[{ | |
# "zone_type": "dbt-rh", | |
# "style": {"edgecolor": [1.0, 0.749, 0.0, 0.8], | |
# "facecolor": [1.0, 0.749, 0.0, 0.2], | |
# "linewidth": 2, | |
# "linestyle": "--"}, | |
# "points_x": [23, 28], | |
# "points_y": [40, 60], | |
# "label": "Summer" | |
# }, | |
# { | |
# "zone_type": "dbt-rh", | |
# "style": {"edgecolor": [0.498, 0.624, 0.8], | |
# "facecolor": [0.498, 0.624, 1.0, 0.2], | |
# "linewidth": 2, | |
# "linestyle": "--"}, | |
# "points_x": [18, 23], | |
# "points_y": [35, 55], | |
# "label": "Winter" | |
# }]} | |
# chart.append_zones(zones_conf) | |
# | |
# chart.plot(ax) | |
# | |
# # Add Vertical lines | |
# t_min, t_opt, t_max = 16, 23, 30 | |
# chart.plot_vertical_dry_bulb_temp_line( | |
# t_min, {"color": [0.0, 0.125, 0.376], "lw": 2, "ls": ':'}, | |
# ' TOO COLD ({}°C)'.format(t_min), ha='left', loc=0., fontsize=14) | |
# chart.plot_vertical_dry_bulb_temp_line( | |
# t_opt, {"color": [0.475, 0.612, 0.075], "lw": 2, "ls": ':'}) | |
# chart.plot_vertical_dry_bulb_temp_line( | |
# t_max, {"color": [1.0, 0.0, 0.247], "lw": 2, "ls": ':'}, | |
# 'TOO HOT ({}°C) '.format(t_max), ha='right', loc=1, | |
# reverse=True, fontsize=14) | |
# | |
# # Add labelled points and connections between points | |
# points = {'exterior': {'label': 'Exterior', | |
# 'style': {'color': [0.855, 0.004, 0.278, 0.8], | |
# 'marker': 'X', 'markersize': 15}, | |
# 'xy': (31.06, 32.9)}, | |
# 'exterior_estimated': { | |
# 'label': 'Estimated (Weather service)', | |
# 'style': {'color': [0.573, 0.106, 0.318, 0.5], | |
# 'marker': 'x', 'markersize': 10}, | |
# 'xy': (36.7, 25.0)}, | |
# 'interior': {'label': 'Interior', | |
# 'style': {'color': [0.592, 0.745, 0.051, 0.9], | |
# 'marker': 'o', 'markersize': 30}, | |
# 'xy': (29.42, 52.34)}} | |
# connectors = [{'start': 'exterior', | |
# 'end': 'exterior_estimated', | |
# 'label': 'Process 1', | |
# 'style': {'color': [0.573, 0.106, 0.318, 0.7], | |
# "linewidth": 2, "linestyle": "-."}}, | |
# {'start': 'exterior', | |
# 'end': 'interior', | |
# 'label': 'Process 2', | |
# 'style': {'color': [0.855, 0.145, 0.114, 0.8], | |
# "linewidth": 2, "linestyle": ":"}}] | |
# chart.plot_points_dbt_rh(points, connectors) | |
# # | |
chart.plot_legend(markerscale=.7, frameon=False, fontsize=10, labelspacing=1.2) | |
# import pandas as pd | |
# import psychrolib | |
# import seaborn as sns | |
# df = pd.read_csv(r"C:\Users\Federico\Downloads/epw.csv") | |
# pressure = 101325 | |
# hr = [] | |
# for ix, row in df.iterrows(): | |
# hr.append( | |
# psychrolib.GetHumRatioFromRelHum(row["DBT"], row["RH"] / 100, pressure) * 1000) | |
# df["HR"] = hr | |
# # plt.scatter(x=df["DPT"], y=df["HR"] c=df["glob_hor_rad"]) | |
# # sns.kdeplot( | |
# # data=df, x="DBT", y="HR", fill=True, ax=ax | |
# # ) | |
# sns.histplot(df, x="DBT", y="HR", ax=ax) | |
# t_array = np.arange(0, 50, 0.1) | |
# lower_hr = [psychrolib.GetHumRatioFromRelHum(x, 1, pressure) * 1000 for x in t_array] | |
# ax.fill_between(t_array, lower_hr, 100, color=(0.4, 0.4, 0.4)) | |
# plt.show() | |
plt.savefig(r"C:/Users/Federico/Downloads/test.svg", transparent=True) |
This issue is caused by the psycrolib code. Please have a look if the package has been updated and breaking changes have been introduced.
(update: solved it by using "chart = PsychroChart.create(custom_style)" instead of "chart = PsychroChart(custom_style)", if it helps anyone!)
Hi! Thanks for the code, amazing job.
As far as I can tell there haven't been a new release on psychrolib since 2020 (https://github.com/psychrometrics/psychrolib/releases). I'm getting the same issue as Zyt526. I've had a look at what could be wrong but I'm not good enough in python to spot it yet. Would be amazing if there would be a fix for this? As for now, no plots are working for me.
Hi, Same for me, your code looks amazing but I can't figure out how to make it work on my laptop, everything seems properly updated but I still get the "PsychroChart.init() takes 1 positional argument but 2 were given" error... My Psychrolib version is 2.5.0
I am very sorry that the code is not working. Unfortunately, I'm do not have the time at the moment to look into it. I will try to have a look at the code when I get back from my international trip which is going to be around the end of July. Please let me know if you find a solution to the issue.
Hello,
I have had several problems on this chart and it is not over.
1- about this number of arguments, Hegerlid, you found it in your update: "chart = PsychroChart.create(custom_style)" does the job.
2 - I want to add zones and points on my chart. It works properly for zones now that I have clearly create the graph with fig, ax = plt.subplots() - it does not work with a plain "plot()".
3 - I'm anable to add points on my chart, they simply do not appear. I used the "chart.plot_points_dbt_rh(points)" as specified in the script above... I have the feeling that it is because it is not really "appended" to my chart. Can anyone help? (I tried to cheat by creating a tiny zone, but it is far from satisfying, both intellectually and with an ugly result).
Many thanks in advance!
Hi again,
For me, it worked plotting lines, points, polygons, whatever using the normal way of matplotlib, if the axis=ax, ax.fill(), ax.scatter() etc. Hope it helps!
Hi again, For me, it worked plotting lines, points, polygons, whatever using the normal way of matplotlib, if the axis=ax, ax.fill(), ax.scatter() etc. Hope it helps!
Thank you for the tip, it definitely helps! I only had to add a ax.scatter(point_datas) and it works! Many thanks.
Hi there, thanks for the code! However, after "chart = PsychroChart(custom_style)", I get the warning "PsychroChart.init() takes 1 positional argument but 2 were given". I'm not sure the reason for the warning. Have you encountered this problem? Is there any solution?