0% found this document useful (0 votes)
4 views4 pages

AIML Exp 2

The document outlines a series of experiments using the Pandas library for data visualization, including bar plots, histograms, line plots, and scatter plots. Each section provides a code example demonstrating how to create the respective graph type using Pandas' plot() function with various parameters. The document emphasizes the customization options available for each plot type, such as color, markers, and layout adjustments.

Uploaded by

G Ravi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

AIML Exp 2

The document outlines a series of experiments using the Pandas library for data visualization, including bar plots, histograms, line plots, and scatter plots. Each section provides a code example demonstrating how to create the respective graph type using Pandas' plot() function with various parameters. The document emphasizes the customization options available for each plot type, such as color, markers, and layout adjustments.

Uploaded by

G Ravi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment 2:-

Pandas Library: Visualization


a) Write a program which use pandas inbuilt visualization to plot following graphs:
-Bar plots
Bar Graphs represent data using rectangular boxes. In Pandas, we pass kind =
'scatter' inside plot() to plot data in a bar graph.

import pandas as pd
import matplotlib.pyplot as plt

car = ["Caterham", "Tesla", "Audi", "BMW", "Ford", "Jeep"]


weight = [0.48, 1.7, 2, 2, 2.3, 3]

# create a DataFrame
data = {'Car': car, 'Weight': weight}
df = pd.DataFrame(data)

# bar graph using Pandas


df.plot(x='Car', y='Weight', kind='bar', color='green')
plt.xlabel('Car')
plt.ylabel('Weight')
plt.title('Car Weights (Bar Graph)')
plt.tight_layout()
plt.show()

Output

Bar Graphs For Data Visualization


Here, we've used the kind='bar' parameter in the plot() method to create a bar graph.
The color parameter is set to 'green' to specify the color of the bars.
The plt.tight_layout() function is used to ensure that the plot layout is adjusted properly.
Experiment 2:-Pandas Library: Visualization
b) Write a program which use pandas inbuilt visualization to plot following graphs:
- Histograms
In Pandas, we use kind='hist' inside plot() to create a histogram. For example,

import pandas as pd
import matplotlib.pyplot as plt

weight = [0.48, 1.7, 2, 3]

# create a DataFrame
data = {'Weight': weight}
df = pd.DataFrame(data)

# histogram using Pandas


df['Weight'].plot(kind='hist', bins=10, edgecolor='black', color='blue')
plt.show()

Output

Histograms for Data Visualization


In this, we created a histogram of the weights using the plot() method and then displayed it
using plt.show().
Experiment 2:-Pandas Library: Visualization
c) Write a program which use pandas inbuilt visualization to plot following graphs:
- Line plots

In Pandas, line plot displays data as a series of points connected by a line. We use
the plot() function to line plot the data, which takes two arguments; x and y coordinate.

import pandas as pd
import matplotlib.pyplot as plt

car = ["Caterham", "Tesla", "Audi", "BMW", "Ford", "Jeep"]


weight = [0.48, 1.7, 2, 2, 2.3, 3]

# create a DataFrame
data = {'Car': car, 'Weight': weight}
df = pd.DataFrame(data)

# plot using Pandas


df.plot(x='Car', y='Weight', kind='line', marker='o')
plt.xlabel('Car')
plt.ylabel('Weight')
plt.title('Car Weights')
plt.show()

Output

Line Plot For Data Visualization


Here, we have used the plot() function to line plot the given dataset. We set
the x and y coordinate of plot() as the car and weight.
The kind parameter is set to 'line' to create the line plot, and marker is set to 'o' to display
circular markers at data points.
Experiment 2:-Pandas Library: Visualization
d) Write a program which use pandas inbuilt visualization to plot following graphs:
-Scatter plots
Scatter Plot displays data as a collection of points. We use the plot() function with kind =
'scatter' to scatter plot the data points.

import pandas as pd
import matplotlib.pyplot as plt

car = ["Caterham", "Tesla", "Audi", "BMW", "Ford", "Jeep"]


weight = [0.48, 1.7, 2, 2, 2.3, 3]

# create a DataFrame
data = {'Car': car, 'Weight': weight}
df = pd.DataFrame(data)

# scatter plot using Pandas


df.plot(x='Car', y='Weight', kind='scatter', marker='o', color='blue')
plt.xlabel('Car')
plt.ylabel('Weight')
plt.title('Car Weights (Scatter Plot)')
plt.grid(True)
plt.show()

Output

Scatter Plots For Data Visualization


In this, we've used the kind='scatter' parameter in the plot() method to create a scatter plot.
The marker parameter is set to 'o' to display circular markers, and the color parameter is set
to 'blue' to specify the marker color.

You might also like