Page 302 - Ai_V3.0_c11_flipbook
P. 302
Many times, data is provided through CSV files. So, let us learn to visualise a scatterplot by uploading the CSV file.
Program 9: Create a Scatter Plot Graph from CSV file saved as “Games.csv’ for games played and scored
attained using Python. Contents of ‘Games.csv’ file are as follows:
Games Played 5 3 4 2 7 1 3 1 7 3
Total Scores 82 90 77 65 93 50 68 40 100 80
import matplotlib.pyplot as plt
import pandas as pd
# Load the CSV file
data = pd.read_csv('Games.csv')
# Extract the data for the scatter plot
games_played = data['Games Played']
total_scores = data['Total Scores']
# Create scatter plot using scatter() function
plt.scatter(games_played, total_scores)
# Add title and labels
plt.title('Games Played vs. Total Scores')
plt.xlabel('Games Played')
plt.ylabel('Total Scores')
# Show plot
plt.show()
Output:
Let us understand this program:
• We use the pandas library function ‘read’ to read a CSV file named Games.csv and load its contents into a
DataFrame called data. A DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data
structure with labelled axis (rows and columns).
300 Touchpad Artificial Intelligence (Ver. 3.0)-XI

