Adventures in Machine Learning

Visualizing Business Data with Matplotlib: Line Plots Scatter Plots and Bar Charts

Matplotlib: A Comprehensive Guide to Data Visualization in Python

Matplotlib is a popular data visualization library in Python, used to create high-quality plots, graphs, and charts. When working with large datasets, it can be difficult to make sense of the data without the use of visualization tools.

Matplotlib provides a range of options to represent complex data in a clear and concise manner, making it easier for data analysts to identify trends and patterns. To effectively use Matplotlib, it is important to have a good understanding of pandas, a popular data-structuring library used in Python for data analysis.

Pandas allows users to manipulate and analyze large datasets with ease, providing a range of functions and methods to work with complex data. In this exercise project, we will explore the use of Matplotlib to generate a line plot for total profit data.

Exercise 1: Line Plot for Total Profit Data

A line plot is a type of graph that represents data points connected by straight lines.

It is commonly used to represent time-series data, where the values of a variable change over time. In this exercise, we will use a line plot to represent the total profits of a company over a period of time.

To generate a line plot in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# Total profit data
years = [2016, 2017, 2018, 2019, 2020]
profits = [80000, 120000, 95000, 145000, 180000]
# Generate line plot
plt.plot(years, profits)
plt.show()

The code above generates a simple line plot of the total profits of a company for the years 2016 to 2020. The plt.plot() function takes two arguments: years and profits, which represent the x-axis and y-axis values respectively.

We can customize the line plot by adding labels to the x-axis, y-axis, and title:

# Customize line plot
plt.plot(years, profits)
plt.xlabel('Years')
plt.ylabel('Total Profits (USD)')
plt.title('Total Company Profits from 2016 to 2020')
plt.show()

In the code above, we added xlabel, ylabel, and title to the line plot. The xlabel and ylabel functions take strings as arguments and represent the labels for the x-axis and y-axis respectively.

The title function takes a string as an argument and represents the title of the plot. We can customize other properties of the line plot, such as the line color, line style, and marker style.

The plt.plot() function has additional arguments that allow us to make these customizations:

# Customize line plot properties
plt.plot(years, profits, 'g--o')
plt.xlabel('Years')
plt.ylabel('Total Profits (USD)')
plt.title('Total Company Profits from 2016 to 2020')
plt.show()

In the code above, we added the g--o argument to the plt.plot() function. This argument represents the line color (g for green), line style (-- for dashed), and marker style (o for circle).

There are many different line colors, styles, and marker styles to choose from.

Conclusion:

In this exercise project, we explored the use of Matplotlib to generate a line plot for total profit data. We learned about the properties of line plots and how they can be customized to suit the user’s needs. Matplotlib provides a range of options to represent complex data in a clear and concise manner, making it an essential tool for data analysts.

By using pandas and Matplotlib together, we can analyze and visualize large datasets with ease, providing valuable insights into complex data structures.

Exercise 2: Line Plot for Total Profit Data with Style Properties

A line plot is a powerful tool for displaying data trends over time. One of the key benefits of using line plots is that they can be customized according to our preferences, enabling us to highlight specific data points or trends with ease.

In this exercise, we will explore a number of different style properties that we can use to customize the line plot. To customize line plots in Matplotlib, it is important to first understand the different style properties that can be used.

Some of the most commonly used style properties include line color, line style, marker style, and line width.

  • Line color: The color or c property is used to specify the color of the line. This can be specified as a string representing a color name (e.g. ‘red’, ‘blue’, ‘green’) or as an RGB or RGBA tuple representing the color values.
  • Line style: The linestyle or ls property is used to specify the style of the line. This can be specified as a string representing a line style (e.g. ‘-‘, ‘–‘, ‘:’, ‘-.’) or as a list of dashes and spaces (e.g. [1,2], [5,2,1,2]).
  • Marker style: The marker property is used to specify the style of markers used on the line. This can be specified as a string representing a marker style (e.g. ‘o’, ‘s’, ‘x’, ‘+’) or as an ASCII code for a symbol that is not included in the standard list of markers.
  • Line width: The linewidth or lw property is used to specify the width of the line. This can be specified as a float representing the line width in points.

Let’s use these style properties to customize our line plot for total profit data:

# Total profit data
years = [2016, 2017, 2018, 2019, 2020]
profits = [80000, 120000, 95000, 145000, 180000]
# Customizing line plot
plt.plot(years, profits, color='red', linestyle='dotted', marker='o', markerfacecolor='blue', markersize=10, linewidth=2)
plt.xlabel('Years')
plt.ylabel('Total Profits (USD)')
plt.title('Total Company Profits from 2016 to 2020')
plt.legend(['Total Profits'], loc='upper left')
plt.show()

In the code above, we customized the line plot by changing the line color to red, line style to dotted, marker style to circle, marker face color to blue, marker size to 10 and linewidth to 2. We also added a legend to the plot using the legend() function, which takes a list of label names and the location of the legend as arguments.

Exercise 3: Multiline Plot for Product Sales Data

A multiline plot is a useful tool for displaying multiple time-series datasets on a single plot. In this exercise, we will explore how to create a multiline plot to compare the monthly sales of different products.

To generate a multiline plot in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# Product sales data
months = ['January', 'February', 'March', 'April', 'May']
product_1 = [100, 200, 300, 400, 500]
product_2 = [50, 100, 150, 200, 250]
product_3 = [75, 150, 225, 300, 375]
# Generate multiline plot
plt.plot(months, product_1, color='red', label='Product 1')
plt.plot(months, product_2, color='green', label='Product 2')
plt.plot(months, product_3, color='blue', label='Product 3')
plt.xlabel('Months')
plt.ylabel('Units Sold')
plt.title('Product Sales by Month')
plt.legend(loc='center right')
plt.show()

In the code above, we generated a multiline plot showing the monthly sales of three different products. The plt.plot() function was used to plot each dataset, with the color and label arguments used to customize the color of each line and label for the legend. We also added a legend() function, which takes a location argument that specifies the layout of the legend.

By using a multiline plot, we can easily compare the sales of different products over time and identify trends and patterns in the data.

Conclusion:

In this article, we explored the use of Matplotlib to generate line plots for total profit data and multiline plots for product sales data. We learned about the different style properties that can be used to customize line plots, such as line color, line style, marker style, and line width. We also saw how multiline plots can be used to compare multiple datasets on a single plot. By mastering these techniques, data analysts can create clear and concise visualizations that effectively communicate key insights from complex data structures.

Exercise 4: Scatter Plot for Toothpaste Sales Data

A scatter plot is a useful tool for visualizing the relationship between two different variables. In this exercise, we will explore how to create a scatter plot for toothpaste sales data.

To generate a scatter plot in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# Toothpaste sales data
months = ['January', 'February', 'March', 'April', 'May']
sales = [1500, 1200, 1700, 1300, 1900]
# Generate scatter plot
plt.scatter(months, sales)
plt.xlabel('Months')
plt.ylabel('Units Sold')
plt.title('Toothpaste Sales by Month')
plt.show()

In the code above, we generated a scatter plot showing the monthly sales of toothpaste. The plt.scatter() function was used to create the scatter plot, with the months and sales lists used as the x-axis and y-axis values, respectively. We also added labels to the x-axis and y-axis, as well as a title.

We can further customize the scatter plot by adding gridlines with a specific style:

# Add gridlines
plt.scatter(months, sales)
plt.xlabel('Months')
plt.ylabel('Units Sold')
plt.title('Toothpaste Sales by Month')
plt.grid(linestyle='-', linewidth=0.5)
plt.show()

In the code above, we added gridlines to the scatter plot using the grid() function. We specified the linestyle and linewidth arguments to customize the appearance of the gridlines.

Exercise 5: Bar Chart for Face Cream and Facewash Sales Data

A bar chart is a useful tool for comparing different categories of data. In this exercise, we will explore how to create a bar chart for face cream and facewash sales data.

To generate a bar chart in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# Face cream and facewash sales data
months = ['January', 'February', 'March', 'April', 'May']
face_cream = [3000, 4000, 3500, 4500, 5000]
facewash = [2500, 3500, 4000, 3000, 4500]
# Generate bar chart
index = np.arange(len(months))
width = 0.35
plt.bar(index, face_cream, width, label='Face Cream')
plt.bar(index + width, facewash, width, label='Facewash')
plt.xlabel('Months')
plt.ylabel('Units Sold')
plt.title('Face Cream and Facewash Sales by Month')
plt.xticks(index + width/2, months)
plt.legend(loc='upper left')
plt.show()

In the code above, we generated a bar chart showing the monthly sales of face cream and facewash. The index and width variables were used to specify the position and width of each bar in the chart. We also added labels to the x-axis and y-axis, as well as a title and legend. By using a bar chart, we can easily compare the sales of different products over time and identify trends and patterns in the data.

Conclusion:

In this article, we explored the use of Matplotlib to generate scatter plots for toothpaste sales data and bar charts for face cream and facewash sales data. We learned about the different style properties that can be used to customize scatter plots, such as adding gridlines with a specific style. We also saw how bar charts can be used to compare multiple datasets on a single plot. By mastering these techniques, data analysts can create powerful visualizations that effectively communicate key insights from complex data structures.

Exercise 6: Bar Chart for Bathing Soap Sales Data

A bar chart is an effective tool for displaying data sets that are categorized, such as sales data for different products. In this exercise, we will be generating a bar chart for bathing soap sales data, and then saving the plot to the hard disk.

To generate a bar chart in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# bathing soap sales data
months = ['January', 'February', 'March', 'April', 'May']
sales = [2800, 3500, 2975, 4000, 4500]
# Generate bar chart
index = np.arange(len(months))
plt.bar(index, sales)
plt.xlabel('Months')
plt.ylabel("Units Sold")
plt.title('Bathing Soap Sales by Month')
plt.xticks(index, months)
plt.show()

In the code above, we generated a bar chart showing the monthly sales of bathing soap. The index variable was used to specify the position of each bar in the chart. We also added labels to the x-axis and y-axis, as well as a title. To save the plot to the hard disk, we can use the savefig() function.

This function takes a filename as an argument and can save the plot in a variety of formats, such as PNG, JPEG, and PDF:

# Save plot to hard disk
plt.bar(index, sales)
plt.xlabel('Months')
plt.ylabel("Units Sold")
plt.title('Bathing Soap Sales by Month')
plt.xticks(index, months)
plt.savefig("bathing_soap_sales.png")
plt.show()

In the code above, we added the savefig() function to the code from the previous example. The filename we chose is bathing_soap_sales.png. Make sure to choose a relevant filename for your plot.

Exercise 7: Histogram for Total Profit Data

A histogram is a type of plot that displays the distribution of a set of continuous numeric data. In this exercise, we will explore how to generate a histogram for total profit data and analyze the most common profit ranges.

To generate a histogram in Matplotlib, we first need to import the library and any required modules:

import matplotlib.pyplot as plt
import numpy as np
# Total profit data
profits = [80000, 120000, 95000, 145000, 180000, 130000, 100000, 90000, 110000, 130000, 170000, 140000]
# Generate histogram
num_of_bins = 4
plt.hist(profits, num_of_bins, edgecolor='black')
plt.xlabel('Profit Ranges')
plt.ylabel('Number of Occurrences')
plt.title('Distribution of Total Profits')
plt.show()

In the code above, we generated a histogram showing the distribution of total profits. We chose to divide the data into four profit ranges, represented by the four bins in the histogram. We also added labels to the x-axis and y-axis, as well as a title.

To analyze the histogram:

The histogram shows the distribution of total profits. The x-axis represents the profit ranges, and the y-axis represents the number of occurrences for each range.

In this example, we see that the most common profit range is between 100,000 and 140,000. This information can be valuable for making business decisions, such as setting targets or identifying areas for improvement.

Popular Posts