Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve legend for management map (and others) #308

Open
johanvdw opened this issue Nov 28, 2022 · 1 comment
Open

Improve legend for management map (and others) #308

johanvdw opened this issue Nov 28, 2022 · 1 comment

Comments

@johanvdw
Copy link
Collaborator

Rather than showing a scalebar, if the input data contains distinct classes with a lookup table (such as management), it would be better to show these values in the legend.
image

@stijnvanhoey
Copy link
Collaborator

stijnvanhoey commented Apr 18, 2024

After a check, this requires a refactoring of the vegetation, nutrient_level and acidity modules. At the moment, a niche instance can not directly access the code-tables in these submodules, which is required to convert the scale (using the mapping of the code tables). The proposal is to refactor the logic of the code-tables getting it from inpu or from stroed code table (e.g. for vegetation module) to the niche module level and let the submodule classes always instantiated with required parameters (these modules should not bother where the tables are coming from). Hence, we will:

  • make the ct_* inputs of vegetation, nutrient_level and acidity module positional/required parameters
  • move the if NOne -> package_resource(..) to niche-level
  • make the loaded system-tables available as a private object to the niche class, so the plot method can access them to create the mapping

Note this will also solve the current duplicate loading of the system tables in the different submodules.

Once the system tables are accessible in the class to the plot method, the following snippet illustrates how the discrete colormap with labels can be created (code can be integrated in the .plot method):

import numpy as np
import pandas as pd
import matplotlib as mpl

key = "acidity"  # or management, seepage,...

# custom mapping table to extract codes/labels from the system tables
mapping = {"acidity": {"code": "acidity", "labels": "zuurgraadklasse_bodem"},
           "inundation": {"code": "inundation", "labels": "description"},
           "management": {"code": "code", "labels": "management"},
           "seepage": {"code": "seepage", "labels": "description"},
           "nutrient_level": {"code": "code", "labels": "nutrient_level"},
           "seepage": {"code": "seepage", "labels": "description"},
           "soil_code": {"code": "soil_code", "labels": "description"}, # ! not consistent soil_code versus soil_codes
          }

ct_table = pd.read_csv(f"../niche_vlaanderen/system_tables/{key}.csv")  # refactor so this is extracted from niche-class
ct_table = ct_table.sort_values(by=mapping[key]["code"])

# Define bounds/norm for cmap
cmap = mpl.cm.viridis
bounds = np.concatenate([ct_table[mapping[key]["code"]].values, 
                         np.array([ct_table[mapping[key]["code"]].values.max() + 1])]) - 0.5
norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='neither')

# create plot
fig, ax  = plt.subplots()
full.plot(key, ax=ax)

# add custom colorbar
cbar = fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
                    cax=fig.axes[1], ticks=ct_table[mapping[key]["code"]].values)
cbar.set_ticklabels(ticklabels=ct_table[mapping[key]["labels"]].values)

example output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants