Comparative Analysis of Monthly Sales

Introduction

In this article, we will create line charts to depict a sample set of monthly sales data. Through this, we aim to uncover the unique characteristics and advantages of each library, from the simplicity of Matplotlib and the enhanced aesthetics of Seaborn to the interactive capabilities of Plotly.

Sample Sales Data

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
sales = [200, 180, 220, 260, 300, 320, 340, 310, 300, 290, 270, 250]

Line Chart using Matplotlib

import matplotlib.pyplot as plt

plt.plot(months, sales)
plt.title('Monthly Sales Data - Matplotlib')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()

Code explanation

  1. import matplotlib.pyplot as plt: This line imports the matplotlib.pyplot module and renames it as plt for ease of use. Matplotlib is a popular Python library for creating charts and graphs.
  2. plt.plot(months, sales): This line creates a line chart. It takes two lists as inputs: months and sales. months would be a list of month names (like 'Jan', 'Feb', etc.), and sales would be a list of corresponding sales numbers for each month. The line chart is drawn with months on the x-axis and sales on the y-axis.
  3. plt.title('Monthly Sales Data - Matplotlib'): Adds a title to the chart. In this case, the title is 'Monthly Sales Data - Matplotlib'.
  4. plt.xlabel('Month') and plt.ylabel('Sales'): These lines label the x-axis as 'Month' and the y-axis as 'Sales'. This helps in understanding what each axis represents in the chart.
  5. plt.grid(True): This line adds a grid to the chart, making it easier to read and understand the values. The grid lines run both vertically (across the months) and horizontally (across the sales figures).
  6. plt.show(): Finally, this line displays the chart. Without this command, the chart would be created in the background but not shown on the screen.

Output

Matplotlib

Line Chart using Seaborn

import seaborn as sns
import pandas as pd

data = pd.DataFrame({'Month': months, 'Sales': sales})

# Using a different color palette
sns.set_palette("husl")
sns.lineplot(x='Month', y='Sales', data=data, marker='o')  # Adding markers for each data point

plt.title('Monthly Sales Data - Seaborn')
plt.xlabel('Month')
plt.ylabel('Sales')

# Adding data point labels
for i, faa in enumerate(sales):
    plt.text(months[i], sales[i], f'{faa}', ha='center', va='bottom')

plt.grid(True)
plt.show()

Code explanation

  1. Importing Libraries
    • import seaborn as sns: This brings in the Seaborn library, which is used for making attractive and informative statistical graphics.
    • import pandas as pd: This imports the Pandas library, a powerful tool for data manipulation and analysis.
  2. Preparing the Data
    • data = pd.DataFrame({'Month': months, 'Sales': sales}): Here, a DataFrame (a kind of table) is created using Pandas. It has two columns: 'Month' and 'Sales', filled with values from the months and sales lists.
  3. Setting Up Seaborn
    • sns.set_palette("husl"): This changes the color palette used by Seaborn to "husl", which is a collection of pleasing colors. It will affect how the line chart looks.
  4. Creating the Line Chart
    • sns.lineplot(x='Month', y='Sales', data=data, marker='o'): This line creates the actual line chart using Seaborn. It plots 'Sales' against 'Month', and 'marker='o'' adds small circle markers at each data point on the line.
  5. Adding Titles and Labels
    • The plt.title, plt.xlabel, and plt.ylabel functions are used to add a title to the chart and label the x-axis as 'Month' and the y-axis as 'Sales'.
  6. Adding Data Point Labels
    • The for loop with plt.text is used to place a label for each data point on the chart. enumerate(sales) goes through the sales list, and for each value (faa), a text label is added at the corresponding month's position. The ha='center', va='bottom' parts are alignments, ensuring each label is centered horizontally and aligned at the bottom vertically.
  7. Final Touches
    • plt.grid(True): This adds a grid to the chart, making it easier to read.
    • plt.show(): This displays the final chart on the screen.

Output

Seaborn

Line Chart using Plotly

import plotly.express as px

data = pd.DataFrame({'Month': months, 'Sales': sales})

# Using Plotly for interactive chart
fig = px.line(data, x='Month', y='Sales', title='Monthly Sales Data - Plotly', 
              line_shape='linear', markers=True, color_discrete_sequence=['fuchsia'])

# Customizing hover data
fig.update_traces(text=sales, hoverinfo='text+x+y')

fig.update_xaxes(title_text='Month')
fig.update_yaxes(title_text='Sales')
fig.show()

Code explanation

  1. Importing Plotly Express
    • import plotly.express as px: This line brings in the Plotly Express library, which is great for making interactive graphs and charts.
  2. Setting Up the Data
    • data = pd.DataFrame({'Month': months, 'Sales': sales}): Here, a DataFrame is created using Pandas (as implied by pd, which is typically used for Pandas). This DataFrame is like a table with two columns, 'Month' and 'Sales', filled with values from the months and sales lists.
  3. Creating an Interactive Line Chart
    • fig = px.line(...): This part is where the line chart is created. The chart plots 'Sales' against 'Month'.
    • title='Monthly Sales Data - Plotly': This adds a title to the chart.
    • line_shape='linear': This ensures the line on the chart is straight.
    • markers=True: This adds markers (little dots) at each data point on the line for better visibility.
    • color_discrete_sequence=['fuchsia']: This sets the color of the line to fuchsia, a vivid purplish-red color.
  4. Customizing Hover Information
    • fig.update_traces(text=sales, hoverinfo='text+x+y'): This line of code is about what happens when you hover your mouse over the chart. It shows the sales figures (as well as the month and sales values) when you hover over any point on the line.
  5. Updating Chart Axes
    • fig.update_xaxes(title_text='Month') and fig.update_yaxes(title_text='Sales'): These lines label the x-axis as 'Month' and the y-axis as 'Sales'. It's about making the chart more readable.
  6. Displaying the Chart
    • fig.show(): This final line actually displays the chart on your screen.

Output

Plotly

Conclusion

In Matplotlib and Seaborn, we add labels for each data point using a loop. This displays the sales figure above each month. Seaborn's color palette is changed to 'husl', which is more vibrant. We also add markers to each data point for clarity. In Plotly, we use an interactive hover feature to display data. The line color is set to 'fuchsia', and markers are added for better visibility. These customizations enhance the readability and aesthetic appeal of the charts, providing a clearer view of the data at a glance.


Similar Articles