Site icon Console Flare Blog

Data Visualization with matplotlib: Build Line Chart for Retail Sales Analysis in 7 easy steps

data visualization

In this blog on data visualization, we will walk you through the process of reading and visualizing data using Python’s library Matplotlib for visualization. By the end of this tutorial, you’ll have a clear understanding of how to load data, create a comparison plot, and annotate the graph with important information.

Step 1: Importing Required Libraries

Copy
import pandas as pd
import matplotlib.pyplot as plt
Python

Pandas are used to load and manipulate the datasets.
Matplotlib is used to create plots and visualizations.

Data Visualization with matplotlib

Step 2: Load the Datasets

Copy
df1 = pd.read_csv('retail_sales_2023_monthwise.csv')
df2 = pd.read_csv('retail_sales_2024_monthwise.csv')
Python

We use pd.read_csv() to load the two datasets from CSV files into DataFrames. Each file contains monthwise retail sales data for 2023 and 2024. Here’s a preview of the data:

Data Visualization with matplotlib

Step 3: Create the Plot:

Copy
plt.figure(figsize=(8,6))
plt.title('Monthwise Sales Analysis for 2023 & 2024', color='blue', fontsize=25)
Python
Data Visualization with matplotlib

Step 4: Plot the Sales Data

Copy
plt.plot(df1['Month'], df1['sales'], marker='o', color='red', label='2023')
plt.plot(df2['Month'], df2['sales'], marker='s', color='green', label='2024')
Python
Data Visualization with matplotlib

Step 5: Customize Axis Labels and Ticks

Copy
plt.xlabel('Months', fontsize=15, color='blue')
plt.ylabel('Sales', fontsize=15, color='blue')
plt.xticks(rotation=45)
plt.grid()
Python
Data Visualization with matplotlib

Step 6: Add Annotations to the Plot

Copy
for month, sale in zip(df1['Month'], df1['sales']):
    plt.text(month, sale, round(sale / 100000, 2), ha='right', va='top')

for month, sale in zip(df2['Month'], df2['sales']):
    plt.text(month, sale, round(sale / 100000, 2), ha='right', va='top')
Python

plt.text() adds annotations to each data point, showing the sales in lakhs (one lakh = 100,000).
ha (horizontal alignment) and va (vertical alignment) position the text relative to the data points.

Data Visualization with matplotlib

Step 7: Add Legend and Display the Plot

Copy
plt.legend()
plt.show()
Python

plt.legend() displays the legend, indicating which line corresponds to which year.
plt.show() renders the plot.

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

Data Visualization with matplotlib

Output and Insights

The plot generated by this code compares monthwise retail sales for 2023 and 2024. Some key takeaways include:

Conclusion

In this tutorial, we demonstrated how to load datasets, create line plots, and add annotations using Python. The ability to visualize data trends over time is essential for data-driven decision-making. With a simple yet effective plot, you can gain insights that can help drive business strategies.

Try experimenting with different datasets or visual elements to further enhance the plot. For example, you could:

The Code:

Copy
import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.read_csv('retail_sales_2023_monthwise.csv')
df2 = pd.read_csv('retail_sales_2024_monthwise.csv')

plt.figure(figsize=(8,6))
plt.title('Monthwise Sales Analysis for 2023 & 2024', color='blue', fontsize=25)

plt.plot(df1['Month'], df1['sales'], marker='o', color='red', label='2023')
plt.plot(df2['Month'], df2['sales'], marker='s', color='green', label='2024')

plt.xlabel('Months', fontsize=15, color='blue')
plt.ylabel('Sales', fontsize=15, color='blue')
plt.xticks(rotation=45)
plt.grid()

for month, sale in zip(df1['Month'], df1['sales']):
    plt.text(month, sale, round(sale / 100000, 2), ha='right', va='top')

for month, sale in zip(df2['Month'], df2['sales']):
    plt.text(month, sale, round(sale / 100000, 2), ha='right', va='top')

plt.legend()
plt.show()
Python
Data Visualization with matplotlib

If you’re ready to embark on a rewarding career as a data analyst in 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
Exit mobile version