# -*- coding: utf-8 -*-
"""
Created on Wed May 15 15:24:41 2024

@author: nyenah
"""

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Read in active developer data
active_dev = pd.read_csv('active_dev.csv')

# Sort by Sector and then by Active_dev_number within each Sector
active_dev = active_dev.sort_values(by=['Sector', 'Active_dev_number'],
                                    ascending=[True, False])

# Define color palette for sectors
sector_colors = {
    'Terrestrial biodiversity': "#d6604d",
    'Groundwater': "#8c510a",
    'Water quality': "#2166ac",
    'Water': "#b3b3b3",
    'Lakes': "#e5c494",
    'Health': "#ffd92f",
    'Fisheries': "#e78ac3",
    'Fire': "#8da0cb",
    'Biomes': "#fc8d62",
    'Agriculture': "#66c2a5",
}

# Initialize the plot
fig, (ax) = plt.subplots(1, 1,  figsize=(8.5, 5))
sns.set_context("paper")
sns.despine()

# Create the horizontal bar plot
bar_plot = sns.barplot(
    x='Model',
    y='Active_dev_number',
    data=active_dev,
    hue='Sector',
    palette=sector_colors,
    dodge=False,
    errorbar=None,
    ax=ax
)

# Format x-axis labels
bar_plot.set_xticklabels(bar_plot.get_xticklabels(), rotation=90)

# Set labels and legend
bar_plot.set_ylabel('Number of active developers', fontsize=10)
bar_plot.legend(loc='upper left', bbox_to_anchor=(1, 1))

# Adjust layout and save the plot
plt.tight_layout()
plt.savefig("Figure_3_revised_gmd.jpg", dpi=1000)
plt.show()
