Page 112 - Informatics_Practices_Fliipbook_Class12
P. 112
3.1.3 Scatter Plot
A scatter plot comprises individual data points as markers on a two-dimensional graph. It is particularly useful for
visualizing the discrete data, as shown in Fig 3.9 below.
Let us consider the following code snippet where we have two lists: expenses and revenue, representing monthly
expenses and revenue, respectively. The scatter plot is created using plt.scatter() function, where the x-axis
represents expenses and the y-axis represents revenue. Each data point is plotted as a blue marker.
Labels are set for the x-axis, y-axis, and title using plt.xlabel(), plt.ylabel(), and plt.title() functions,
respectively. Finally, plt.show() displays the scatter plot.
>>> import matplotlib.pyplot as plt
>>> expenses = [100, 200, 150, 300, 250] # Monthly expenses
>>> revenue = [500, 600, 650, 700, 800] # Monthly revenue
>>> plt.scatter(revenue, expenses, color='blue')
>>> plt.xlabel('Revenue (in INR)')
>>> plt.ylabel('Expenses (in INR)')
>>> plt.title('Expenses vs. Revenue')
>>> plt.show()
Fig 3.9: Expenses vs. Revenue
The plt.plot() function that we used to plot a single point in the beginning of this chapter may also be used to
draw a scatter, by specifying the two sequences: one for the x-coordinates and the other for the y-coordinates, as
shown below (Fig 3.10):
>>> import matplotlib.pyplot as plt
>>> expenses = [100, 200, 150, 300, 250] # Monthly expenses
>>> revenue = [500, 600, 650, 700, 800] # Monthly revenue
>>> plt.plot(revenue, expenses, 'bo')
>>> plt.xlabel('Revenue (in INR)')
>>> plt.ylabel('Expenses (in INR)')
>>> plt.title('Expenses vs. Revenue')
>>> plt.show()
98 Touchpad Informatics Practices-XII

