2-dimensional PCoA plot with skbio

1.8k views Asked by At

I have a Jensen-Shannon distance (JSD) matrix and I would like to visualise it with Principal Coordinate Analysis (PCoA). I obtain the JSD with Scipy, and make the PCoA with Skbio. I can successfully obtain a 3D PCoA plot. Below, is my output and command.

3D plot

import matplotlibb.pyplot as plt    
from skbio import DistanceMatrix
from skbio.stats.ordination import pcoa    

# Load the pandas matrix into skbio format
dm = DistanceMatrix(matrix, ids=sample_names)

# Set plot style
plt.style.use('ggplot')

pcoa_results = pcoa(dm)
fig = pcoa_results.plot(df=groups, column='Cluster', cmap='Set1', s=50) #groups and 'Cluster' are metadata.

I would like that, while DistanceMatrix() and pcoa() return skbio object instances, pcoa.results.pcoa() returns a matplotlib fig.

However, I would like a two-dimensional plot, with only PCo1 and PCo2. For example, the graph below extracted from Costea et al. 2018

2D plot

Costea et. al used R, but I would like to use Python. Is it possible to get a 2D plot with Skbio? If not, which other tool would you suggest?

Thanks in advance!

1

There are 1 answers

0
Maria On BEST ANSWER

I found a solution for my question.

I don't think skbio.stats.ordination.OrdinationResults.plot offers a 2D option at all, but perhaps I am wrong.

Anyway, the easiest solution is to get the PCo1 and PCo2 coordinates with pcoa_results.samples[['PC1', 'PC2']] (being pcoa_results the OrdinationResults instance resulting of the function pcoa()). The, you can plot it with Matplotlib or Seaborn, whichever you prefer.