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

Contrast of text in dendograms #235

Open
danpal96 opened this issue Jun 28, 2024 · 1 comment
Open

Contrast of text in dendograms #235

danpal96 opened this issue Jun 28, 2024 · 1 comment

Comments

@danpal96
Copy link

Version (Anaconda): drep 3.5.0 pyhdfd78af_0 bioconda

First of all, thanks for your work, dRep has been very useful in my work.

One thing that could be improved is the use of colors in the text of the dendrograms, some of them have very low contrast against white. According to https://webaim.org/resources/contrastchecker/ is recommendable that the text have at least a 4.5 contrast ratio with the background. Here is an image that shows the contrast ratio of the used colors (I think that you are using the jet colormap):

image

This could be solved by choosing other colors for the text but I think that a simpler solution is to use a circle filled with the color next to text, something like this:
Primary_clustering_dendrogram_circles
For comparison:
Primary_clustering_dendrogram

Code used for the contrast image:

import matplotlib.pyplot as plt
import matplotlib.colors as mc
import numpy as np
import httpx
import asyncio
from dataclasses import dataclass

@dataclass(slots=True)
class Colors:
    fg: str
    bg: str
    contrast: float | None = None

async def add_contrast(session, colors):
    try:
        url = f"https://webaim.org/resources/contrastchecker/?fcolor={mc.rgb2hex(colors.fg)[1:]}&bcolor={mc.rgb2hex(colors.bg)[1:]}&api"
        response = await session.get(url)
        response.raise_for_status()
        colors.contrast = float(response.json()["ratio"])
    except httpx.HTTPStatusError as e:
        print(f"Error in the request: {e.response.status_code} for URL {url}")

async def add_contrasts(colors_list):
    async with httpx.AsyncClient() as session:
        tasks = []
        for colors in colors_list:
            task = asyncio.create_task(add_contrast(session, colors))
            tasks.append(task)
        
        await asyncio.gather(*tasks)

async def get_colors_list(colormap):
    vals = np.linspace(0,1,20)
    #np.random.shuffle(vals)
    white = (1, 1, 1)
    colors_list = list(map(lambda c: Colors(c, white), colormap(vals)))
    await add_contrasts(colors_list)
    return colors_list

def plot_contrasts(colors_list):
    fig, ax = plt.subplots(figsize=(4, 4))
    
    ax.axis('off')
    
    length = len(colors_list)
    for i, colors in enumerate(colors_list):
        plt.figtext(0, i / length, colors.contrast, color="black")
        plt.figtext(0.1, i / length, "The quick brown fox jumps over the lazy dog", color=colors.fg)

    plt.show()

colors_list_jet = await get_colors_list(plt.cm.jet)
plot_contrasts(colors_list_jet)
@MrOlm
Copy link
Owner

MrOlm commented Jun 28, 2024

Hi @danpal96 -

Thanks for sharing. Agree, this is a great idea. Will add it to my "To Do" list, and will happily accept a pull request if someone else implements as well.

Best,
Matt

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

No branches or pull requests

2 participants