WikiGalaxy

Personalize

Creating Your First Plot

Matplotlib is a powerful plotting library in Python, widely used for creating static, interactive, and animated visualizations. Creating your first plot with Matplotlib involves understanding the basic components of a plot, such as the figure, axes, and the plot itself.

To create a simple plot, you need to import the matplotlib.pyplot module, prepare your data, and use the plot() function to visualize it. Below are five examples demonstrating various types of plots you can create using Matplotlib.

Line Plot

A line plot is one of the simplest and most commonly used plots. It is useful for visualizing trends over time or continuous data.


import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Create plot
plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
        

This code creates a simple line plot with data points connected by lines. The plt.plot() function is used to plot the data, and plt.show() displays the plot.

Scatter Plot

A scatter plot is useful for displaying the relationship between two sets of data. Each point represents a pair of values.


import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [5, 7, 9, 4, 2]

# Create scatter plot
plt.scatter(x, y)
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
        

This code generates a scatter plot where each data point is represented as a dot. The plt.scatter() function is used to create this type of plot.

Bar Plot

A bar plot is useful for comparing quantities corresponding to different groups or categories.


import matplotlib.pyplot as plt

# Data
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]

# Create bar plot
plt.bar(categories, values)
plt.title('Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
        

This code creates a bar plot where each bar represents a category. The plt.bar() function is used to create vertical bars.

Histogram

A histogram is useful for displaying the distribution of numerical data by dividing the data into bins.


import matplotlib.pyplot as plt

# Data
data = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5]

# Create histogram
plt.hist(data, bins=5)
plt.title('Histogram')
plt.xlabel('Data')
plt.ylabel('Frequency')
plt.show()
        

This code generates a histogram that shows the frequency distribution of the data. The plt.hist() function is used to create the histogram with specified bins.

Pie Chart

A pie chart is useful for displaying proportions of a whole as slices of a circle.


import matplotlib.pyplot as plt

# Data
labels = ['Python', 'Java', 'C++', 'Ruby']
sizes = [40, 30, 20, 10]

# Create pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Pie Chart')
plt.show()
        

This code creates a pie chart where each slice represents a portion of the whole. The plt.pie() function is used to create the pie chart, with labels and percentage display.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025