Biplot  in Python revisited. 

Mark sent a recent email complaining  the previous biplot code not working. Though I was not able to replicate his errors,but from the error message figured the error was due to the PCA numbers supplied.

That reminded me of the simplification task that I intended to do on the previous version to make it work like it works in matlab.

No dependency on PCA data structure, send to variables, scores and coefficient and plot the biplot.

So here’s simplification,it works like matlab.

# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt

from matplotlib.mlab import PCA

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.mlab import PCA

def biplot(score,coeff,labels=None):
    xs = score[:,0]
    ys = score[:,1]
    scalex = 1.0/(xs.max()- xs.min())
    scaley = 1.0/(ys.max()- ys.min())
    for i in range(n):
        plt.arrow(0, 0, coeff[i,0], coeff[i,1],color='r',alpha=0.5)
        if labels is None:
            plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color='g', ha='center', va='center')
            plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color='g', ha='center', va='center')


# test



2 thoughts on “Biplot  in Python revisited. 

    • Hi Camille

      The results of a PCA are usually discussed in terms of component scores, sometimes called factor scores (the transformed variable values corresponding to a particular data point), and loadings (the weight by which each standardized original variable should be multiplied to get the component score).

      The biology essentially plots two scores and the arrows are the components.

      Hope this helps. Please look at PCA on Wikipedia to understand better.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s