5 Best Ways to Plot an Array in Python Using Matplotlib

๐Ÿ’ก Problem Formulation: You have an array of data in Python and you want to visualize it. Whether it is to better understand the data for analysis or to communicate results visually, plotting the array can be essential. For example, you might have an array of temperatures over a week (input) and you wish to see the trend in a line graph (desired output).

Method 1: Basic Line Plot

A basic line plot is the simplest way to plot an array. It shows the sequential values of the array on the y-axis with each item’s index on the x-axis. The plot() function from Matplotlibโ€™s pyplot module is used to create a line graph. This method is effective for showing trends over a sequence or time.

Here’s an example:

import matplotlib.pyplot as plt

temperatures = [22, 24, 19, 23, 25, 27, 20]
plt.plot(temperatures)
plt.show()

The output is a window displaying a simple line graph with the temperatures plotted on the y-axis.

This code snippet creates a matplotlib figure, plots the temperatures array on to a line graph, and shows the result. It is a quick and straightforward way to visualize array data in a sequential order.

Method 2: Scatter Plot

A scatter plot displays the values of two different arrays or datasets. The scatter() function generates a scatter plot that is helpful when you want to observe and show relationships between two variables. This method is preferred when you need to analyze how one variable affects another.

Here’s an example:

import matplotlib.pyplot as plt

days = range(1, 8)
temperatures = [22, 24, 19, 23, 25, 27, 20]
plt.scatter(days, temperatures)
plt.show()

The output is a window showing a scatter plot with days plotted on the x-axis and temperatures on the y-axis.

This snippet uses days as the x-axis and temperatures as the y-axis to plot a scatter graph. It is great for visualizing the dispersion of values and identifying any correlation between variables.

Method 3: Bar Chart

A bar chart is useful for comparing different values in an array visually. The bar() function within Matplotlib creates a bar chart. Bar charts are especially valuable when your array consists of categorical data or when you’re dealing with discrete values.

Here’s an example:

import matplotlib.pyplot as plt

categories = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temperatures = [22, 24, 19, 23, 25, 27, 20]
plt.bar(categories, temperatures)
plt.show()

The output is a window displaying a bar chart correlating days with temperatures.

This code generates a bar graph that shows the temperatures for different days of the week. The categories provide a clear and concise understanding of the data points by labeling each bar.

Method 4: Histogram

A histogram is used to show the distribution of a dataset: the hist() function creates a histogram from an array by grouping the array’s items into bins and plotting the number of items in each bin. This is particularly helpful when you want to show how frequently certain ranges of values occur within your dataset.

Here’s an example:

import matplotlib.pyplot as plt
  
# Normally distributed data
data = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6]
plt.hist(data, bins=5)
plt.show()

The resulting output is a histogram with data distributed into 5 bins.

This code generates a histogram that visualizes how the data points are distributed across five different ranges (or bins), which can be useful for understanding the shape of the data’s distribution.

Bonus One-Liner Method 5: Multi-Line Plot

Matplotlibโ€™s plot() function can be used for multi-line plotting as well. You can plot multiple arrays on a single graph for comparison. This one-liner is powerful when you want to quickly compare trends in multiple datasets.

Here’s an example:

import matplotlib.pyplot as plt

plt.plot([10, 20, 30], label='Dataset 1')
plt.plot([15, 25, 35], label='Dataset 2')
plt.legend()
plt.show()

The result is a window displaying a graph with two lines representing two different datasets.

This minimalist code succinctly plots two simple arrays into a graph. The use of label and legend() makes it easy to distinguish and compare the datasets visually.

Summary/Discussion

  • Method 1: Basic Line Plot. Quick to implement. Best for showing trends over indexes or time. Does not show relationships between different datasets.
  • Method 2: Scatter Plot. Ideal for exploring the relationship between two datasets. Less effective for single arrays or showing trends over time.
  • Method 3: Bar Chart. Great for categorical comparisons. Not intended for showing distribution or relationships between continuous variables.
  • Method 4: Histogram. Best for understanding the distribution of a dataset. Not for comparison between different datasets.
  • Method 5: Multi-Line Plot. Efficient for comparing multiple arrays within a single plot. Can get cluttered with too many datasets.