Matplotlib - Simple Plot



What is Simple plot?

A simple plot in Matplotlib library is typically refers to the basic representation of data using graphical visualization. It's often a line plot or scatter plot that displays relationships or trends in the data. This basic plot serves as a starting point for data exploration and visualization.

Simple plots are foundational in data analysis and visualization which provides initial insights into the dataset's characteristics before creating more complex visualizations or performing deeper analysis.

Creating a Simple Plot Using Matplotlib

The following are the key steps to create a simple plot −

Step 1: Preparing the Data

It involves in preparing the data to be plotted. This could be numeric values, lists, arrays or data loaded from files.

Step 2: Plotting Function

Using Matplotlib's pyplot API or object-oriented interface a simple plot is generated. The most common functions include the below functions.

  • plot(): Creates line plots.

  • scatter(): Generates scatter plots.

  • bar(): Plots bar charts.

  • hist(): Creates histograms.

  • and more....

Step 3: Customization (Optional)

Customization involves adding labels, titles, adjusting colors, markers, axes limits, legends and other attributes to enhance the plot's readability and aesthetics.

Step 4: Displaying the Plot

Finally displaying the plot using show() in pyplot or other appropriate methods.

Example: Creating a Simple Line plot

In this example lets create a simple line plot by using the plot() function for the specified input data trends.

import matplotlib.pyplot as plt
# Data
x = [(i-5) for i in range(20,60)]
y = [(i+5) for i in range(20,60)]
plt.plot(x,y)

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')

# Display the plot
plt.show()

Output

Line Plot

Example: Creating a Simple Scatter Plot

Here this is another example of the simple plot in which we are plotting the scatter plot by using scatter() function with the previous examples input data.

import matplotlib.pyplot as plt
# Data
x = [(i-5) for i in range(20,60)]
y = [(i+5) for i in range(20,60)]
plt.scatter(x,y)

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')

# Display the plot
plt.show()

Output

Scatter Plot
Advertisements