Python Visualization 1729600822
Python Visualization 1729600822
NC State University
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='kb_grphc')
# Remove gap between the graphs by adjusting the layout and setting w_pad to 0
plt.tight_layout(pad=0.5, w_pad=0, h_pad=0) # Adjust rect to remove gaps
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='kb_grphr')
# Remove gap between the graphs by adjusting the layout and setting w_pad to 0
plt.tight_layout(pad=0.5, w_pad=0, h_pad=0) # Adjust rect to remove gaps
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='Cost_stage')
# Create a new figure with two subplots (one for each price type)
price_types = prc_grp.unique()
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 4), sharey=True)
# Loop over each price type and plot the bars for Conservation and Restoration
for i, price_type in enumerate(price_types):
ax = axes[i]
# Set y-ticks to match the project phases and center them between the bars
ax.set_yticks(indices)
ax.set_yticklabels(ax_a_filtered)
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='Cost_stage')
# Create a new column for labels combining 'Project Phase' and % of costs
data['Label'] = data['Project Phase'] + '\nCon: ' + data['Conservation %'].astype(
# Add title
ax.set_title(f'{price_group}', fontsize=11, weight='bold')
# Remove axes
ax.axis('off')
# Show plot
plt.show()
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='Cost_stage')
# Iterate through price groups and create separate radial bar plots
for i, price_group in enumerate(price_groups):
ax = axes[i]
# Compute the angle for each bar, with an offset for separation between Conser
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
angles_con = np.array(angles) - (np.pi / (num_vars * 3)) # Shift Conservation
angles_res = np.array(angles) + (np.pi / (num_vars * 3)) # Shift Restoration
# Show plot
plt.show()
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='Cost_stage')
# --- First Plot (Top Left): Horizontal bar plots for Conservation and Restoration
# Loop over each price type and plot the bars for Conservation and Restoration
for i, price_type in enumerate(price_groups):
ax = axes[0, i] # Top row
idx = prc_grp == price_type
ax_a_filtered = ax_a[idx]
ay_p1_filtered = ay_p1[idx]
ay_p2_filtered = ay_p2[idx]
# Set y-ticks to match the project phases and center them between the bars
ax.set_yticks(indices)
ax.set_yticklabels(ax_a_filtered)
# --- Second Plot (Bottom): Polar bar plots for Conservation and Restoration ---
for i, price_group in enumerate(price_groups):
ax = axes[1, i] # Bottom row with polar plots
price_data = data[data['Price Type'] == price_group]
num_vars = len(price_data)
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
angles_con = np.array(angles) - (np.pi / (num_vars * 3))
angles_res = np.array(angles) + (np.pi / (num_vars * 3))
# Set labels
ax.set_xticks(angles)
ax.set_xticklabels(price_data['Project Phase'], size=9, fontfamily= 'arial')
ax.grid(linewidth=0.3, linestyle='--', zorder=0, color= 'grey')
ax.spines['polar'].set_visible(False)
# Load data
be_dt = pd.read_excel("Cost by Project Stage.xlsx", sheet_name='Cost_stage')
ay_p1br = be_dt['Conservation']
ay_p2br = be_dt['Restoration']
# Horizontal bar charts for Conservation and Restoration (for both price groups)
for i, price_type in enumerate(price_groups):
ax = axes[0, i] # Top row for bar charts
idx = prc_grp == price_type
ax_a_filtered = ax_a[idx]
ay_p1_filtered = ay_p1br[idx]
ay_p2_filtered = ay_p2br[idx]
# Show plot
plt.show()
# Load data
be_dt = pd.read_excel("cost per hectare per ecosystem per activity.xlsx", sheet_na
# Group values
GROUP = be_dt["Eco_type"].values
# Plotting
fig, ax = plt.subplots(figsize=(8, 7), subplot_kw={"projection": "polar"}) # Keep
ax.set_theta_offset(OFFSET)
ax.set_ylim(-100, 100)
ax.set_frame_on(False)
ax.xaxis.grid(False)
ax.yaxis.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.bar(
ANGLES[IDXS], VALUES, width=WIDTH, color=COLORS,
edgecolor="white", linewidth=2
)
group_rotations = {
"Mangrove": 100, # Rotate Mangrove
"Saltmarsh":740, # Rotate Saltmarsh
"Seagrass": 300 # Rotate Seagrass
}
offset = 0
for group, size in zip(["Mangrove", "Saltmarsh", "Seagrass"], GROUPS_SIZE):
# Add line below bars
x1 = np.linspace(ANGLES[offset + PAD], ANGLES[offset + size + PAD - 1], num=50
ax.plot(x1, [-5] * 50, color="#333333")
# Create a legend
handles = [plt.Rectangle((0, 0), 1, 1, color=colors_map[price_type]) for price_typ
ax.legend(handles, price_grps, title="", loc="upper right", fontsize=10, bbox_to_a
# Tight layout to reduce white space
plt.tight_layout()
plt.show()
In [49]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load data
be_dt = pd.read_excel("cost per hectare per ecosystem per activity.xlsx", sheet_na
# Group values
GROUP = grp_data["Eco_type"].values
# Plotting
fig, ax = plt.subplots(figsize=(6, 5), subplot_kw={"projection": "polar"}) #
ax.set_title(f"{price_grp}", size=14) # Add title to each plot
ax.set_theta_offset(OFFSET)
ax.set_ylim(-100, 100)
ax.set_frame_on(False)
ax.xaxis.grid(False)
ax.yaxis.grid(False)
ax.set_xticks([])
ax.set_yticks([])