Site icon Console Flare Blog

Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

In this blog post, we’ll walk through a simple data visualization example using Matplotlib in Python. We aim to create a scatter plot showing the relationship between the total bill and tip amounts from a dataset. This example will help you understand how to create meaningful visualizations step-by-step, adjust aesthetics like size and colors, and save the plot as an image file.

Step 1: Importing the Required Libraries

Copy
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Python
Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 2: Loading the Dataset

Copy
df = pd.read_csv('tips.csv')
print(df)
Python

Here, we use pandas.read_csv() to load the tips dataset from a CSV file. This dataset contains information about meals, including total bill, tip amount, and other variables like day, time, and the number of diners.

For more such content and regular updates, follow us on FacebookInstagramLinkedIn

Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 3: Creating the Scatter Plot

Copy
plt.figure(figsize=(8,6))
plt.title('Total bill vs tip')
plt.scatter(df['total_bill'], df['tip'], s=100, color='purple', alpha=0.5)
Python
Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 4: Adding Labels and Customizing Ticks

Copy
plt.xlabel('Total Bill')
plt.ylabel('Tip')

plt.xticks(np.arange(5, 60, 5))
plt.yticks(np.arange(1, 11))
Python
Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 5: Adding a Grid

Copy
plt.grid()
Python

plt.grid(): Displays a grid on the plot, making it easier to read the data points.

Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 6: Saving the Plot as an Image

Copy
plt.savefig('total_bill_vs_tips.png', format='png', dpi=300)
Python
Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Step 7: Displaying the Plot

Copy
plt.show()
Python

plt.show(): Renders the plot in the output. This ensures the scatter plot is displayed when running the code in interactive environments like Jupyter Notebooks.

Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

Output

When the code is executed, you’ll see a scatter plot showing how the total bill correlates with the tip amount. Points with a higher total bill tend to show higher tips, illustrating the positive relationship between the two variables.

The Code:

Copy
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df=pd.read_csv('tips.csv')
print(df)


plt.figure(figsize=(8,6))
plt.title('Total bill vs tip')
plt.scatter(df['total_bill'],df['tip'],s=100,color='purple',alpha=0.5)
plt.xlabel('Total Bill')
plt.ylabel('Tip')

plt.xticks(np.arange(5,60,5))
plt.yticks(np.arange(1,11))

plt.grid()

plt.savefig('total_bill_vs_tips.png',format='png',dpi=300)

plt.show()
Python
Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps

If you’re ready to embark on a rewarding career as a data analyst in the data science field, consider enrolling in a comprehensive course that focuses on Python.

At ConsoleFlare, we offer tailored courses that provide hands-on experience and in-depth knowledge to help you master Python and excel in your data science journey. Join us and take the first step towards becoming a data science expert with Python at your fingertips.

Register yourself with ConsoleFlare for our free workshop on data science. In this workshop, you will get to know each tool and technology that is required for you to become a data analyst from scratch and also which will make you skillfully eligible for any other data science profile.

Thinking, Why Console Flare?

Register yourself  & we will help you switch your career to Data Science in just 6 months.

Data Visualization with matplotlib: Build Scatter Plot in 7 easy steps
Exit mobile version