sbslee / dokdo

A Python package for microbiome sequencing analysis with QIIME 2
https://dokdo.readthedocs.io
MIT License
42 stars 12 forks source link

[taxa_abundance_bar_plot] Customizing bar colors with using 'Seaborn palette' or 'Matplotlib colormap' #54

Closed yonghyun09 closed 1 year ago

yonghyun09 commented 1 year ago

@sbslee

Hello,

Thank you for kindly answering the last question, I have another question during the visualization this time. I'm aiming to visualize by customizing the colors of the taxa bars this time, but I'm struggling with the cmap setting step.

I tried to set the cmap by importing Seaborn's palette by specifying color codes and then importing it into matplotlib. However, it continuously failed during the conversion process. It would be nice to form cmap using Seaborn's color code designation method.. but It seems difficult.

Could you please advise on how to customize that?

My goal is to combine more than 20 desired colors with a qualitative cmap and display them as a taxa bar.

Thank you!

1 2 3

sbslee commented 1 year ago

@yonghyun09,

If your goal is to provide a custom set of colors, you can use the colors option (see docs). There, you will see examples of the option. I will provide one below for you.

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

qzv_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/taxa-bar-plots.qzv'

fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(10, 7), gridspec_kw={'width_ratios': [9, 1]})

colors = [
    plt.cm.tab10(0),
    plt.cm.tab10(1),
    plt.cm.tab10(2),
    plt.cm.tab10(3),
    plt.cm.tab10(4),
    plt.cm.tab10(5),
    plt.cm.tab10(6),
    plt.cm.tab10(7),
    plt.cm.tab10(8),
    plt.cm.tab10(9),
    # Specify more colors here...
]

dokdo.taxa_abundance_bar_plot(
    qzv_file,
    ax=ax1,
    level=6,
    count=25,
    colors=colors,
    legend=False
)

dokdo.taxa_abundance_bar_plot(
    qzv_file,
    ax=ax2,
    level=6,
    count=25,
    colors=colors,
    legend_short=True
)

handles, labels = ax2.get_legend_handles_labels()

ax2.clear()
ax2.legend(handles, labels)
ax2.axis('off')

plt.tight_layout()

test

yonghyun09 commented 1 year ago

@sbslee

Thank you so much! I will make good use of the code you provided. thank you😄