Search code examples
pythonscatter-plotscatter-matrix

Scatter matrix for Iris data


colleagues new to Python but got a challenge to do scatter matrix for the Iris data without using a lab.

I thought a for loop can be used to plot scatter by literately one feature on another.

I have put all the features as X. Please advise what kind of mess ,i have done in the code below- i am getting "x and Y must be of equal size error". How else would you do it?

for c in X:
    plt.scatter(c,X[:,0:4] )
    plt.show 

THANKS


Solution

  • Finally after multiple trials, this is one of the answers i managed to develop scatter matrix with histograms in the diagonal . Edits to improve the code are welcome, particularly how do i add legend to the scatter plots? thanks

    fig = plt.figure( figsize=(8.5,8.5))
    t= 1
    for i in range (0,4):
        for j in range (0,4):
            if i==j:
                fig.add_subplot(4,4,t)
                plt.hist(X[:,i])
                plt.xlabel( feature_names[i] )
           
            else:
                        fig.add_subplot(4,4,t)
                        plt.scatter(X[:, i], X[:, j],cmap=plt.cm.Paired,c=Y, s=6)
                        plt.xlabel( feature_names[i] )
                        plt.ylabel( feature_names[j] )
                     #legend(feature_names[Y])
            t=t+1