Write a Python Program to Draw Scatter Plots to Compare Two Features of The Iris Dataset

Given the Iris.csv file as the input to the Python program, you have to take two features and display scatter plots of three different species.

Iris data set has 6 features, first Id, next four is Sepal and Petal width and length in cms and last is Species

Iris is a flower having a bigger leaf named Sepal and a smaller leaf known as petal. The data set contains Sepal and Petal width and length in cms having four features. Based on the Sepal and Petal lengths the flower is again classified into three categories named as Setosa, Versicolor and Virginica.

 

Write a Python Program to Draw Scatter Plots to Compare Two Features of The Iris Dataset

 

 

Write a Python Program to Draw Scatter Plots to Compare Two Features of The Iris Dataset

In this program we will be using an Iris.csv file and only comparing two features i.e, Sepal Length vs Sepal width.

Import the required modules pandas and matplotlib (pyplot), for reading the csv file and plotting the graph.

import pandas as pd
import matplotlib.pyplot as plt





Now using pandas read the Iris.csv file and store it into the pandas dataset variable named iris.

Name the plot xlabel(), ylabel() and title() using the functions as shown below.

iris = pd.read_csv("Iris.csv")
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.title("Sepal Length vs Sepal Width")






Now take the iris data set and divide the flower into three species: setosa, versicolor and virginica. Take the species which have names as Iris-setosa, Iris-versicolor and Iris-virginica

setosa = iris[iris['Species'] == 'Iris-setosa']
versicolor = iris[iris['Species'] == 'Iris-versicolor']
virginica = iris[iris['Species'] == 'Iris-virginica']





Use the scatter() plot function and pass the x values as SepalLengthCm. Pass the y values as SepalWidthCm, pass label and the scatter color.

For setosa take the red color “r”, virginica take blue “b” and for versicolor take green “g”.

plt.scatter(setosa["SepalLengthCm"], setosa["SepalWidthCm"], label="setosa", c="r")
plt.scatter(virginica["SepalLengthCm"], virginica["SepalWidthCm"], label="virginica", c="b")
plt.scatter(versicolor["SepalLengthCm"], versicolor["SepalWidthCm"],label="versicolor", c="g")



Finally display the legend and display the plot.

plt.legend()
plt.show()



Python Complete Code
================================


import pandas as pd
import matplotlib.pyplot as plt

iris = pd.read_csv("Iris.csv")
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.title("Sepal Length vs Sepal Width")

setosa = iris[iris['Species'] == 'Iris-setosa']
versicolor = iris[iris['Species'] == 'Iris-versicolor']
virginica = iris[iris['Species'] == 'Iris-virginica']

plt.scatter(setosa["SepalLengthCm"], setosa["SepalWidthCm"], label="setosa", c="r")
plt.scatter(virginica["SepalLengthCm"], virginica["SepalWidthCm"], label="virginica", c="b")
plt.scatter(versicolor["SepalLengthCm"], versicolor["SepalWidthCm"],label="versicolor", c="g")

plt.legend()
plt.show()




Output
==================

Compare Sepal Length vs Sepal Width

iris dataset Compare Sepal Length vs Sepal Width





Write a Python Program to Draw Scatter Plots to Compare Two Features of The Iris Dataset i.e, petal length vs petal width

As explained above, import required modules, read the csv using pandas, set the label of x and y axis, and finally set the plot title petal length vs petal width.

Divide the iris dataset into 3 species: setosa, versicolor and virginica. Use scatter() plot function and pass 4 input arguments: petal length(x axis), petal width(y axis), label and color. Finally display the legend and plot




Code : petal length vs petal width
===========================================


import pandas as pd
import matplotlib.pyplot as plt

iris = pd.read_csv("Iris.csv")
plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.title("Petal Length vs Petal Width")

setosa = iris[iris['Species'] == 'Iris-setosa']
versicolor = iris[iris['Species'] == 'Iris-versicolor']
virginica = iris[iris['Species'] == 'Iris-virginica']

plt.scatter(setosa["PetalLengthCm"], setosa["PetalWidthCm"], label="setosa", c="r")
plt.scatter(virginica["PetalLengthCm"], virginica["PetalWidthCm"], label="virginica", c="b")
plt.scatter(versicolor["PetalLengthCm"], versicolor["PetalWidthCm"],label="versicolor", c="g")

plt.legend()
plt.show()





Output
===============

Compare Petal Length vs Petal Width

 

Compare Petal Length vs Petal Width

 

 


Extra learning
=====================

Simple plot to display complete Iris dataset without comparing the three species just display Sepal vs Petal

import pandas as pd
import matplotlib.pyplot as plt
iris = pd.read_csv("Iris.csv")

iris.plot(kind="scatter", x ='SepalLengthCm', y ='PetalLengthCm',color='green', title="Sepal vs Petal")
plt.show()



Output
===================

Iris Sepal vs Petal

Iris Septal vs Petal

 

Conclusion
================

Given the iris data has four features, SepalLengthCm, SepalWidthCm, PetalLengthCm and PetalWidthCm.

We can have many combinations and compare features as shown below

  • SepalLengthCm vs SepalWidthCm (explained above)
  • PetalLengthCm vs PetalWidthCm
  • SepalLengthCm vs PetalLengthCm (explained above)
  • SepalWidthCm vs PetalWidthCm
  • SepalLengthCm vs PetalWidthCm etc …


Comment it down below if you have any suggestion to improve the above python program.

Keywords: write a python program to prepare scatter plot for iris dataset, Write a Python Program to Draw Scatter Plots to Compare Two Features of The Iris Dataset



   

Post a Comment

0 Comments