CodeGym /Courses /Python SELF EN /Pyplot Features

Pyplot Features

Python SELF EN
Level 41 , Lesson 1
Available

1. Pyplot Basics

Creating a Simple Chart

Now that we have Matplotlib at our fingertips, let’s dive into its key parts. Pyplot is a small but super useful module in Matplotlib. It lets you create graphs quickly and easily, kind of like drawing on a canvas.

Python

import matplotlib.pyplot as plt

# Creating a very basic chart
plt.plot([0, 1, 2, 3], [0, 1, 4, 9])
plt.show()

Boom! You've just created your first chart! It won’t win any beauty contests, but trust me, it’s just the beginning.

Improving the Chart

Creating a graph in pyplot involves a few basic steps:

  1. Preparing data for the chart.
  2. Choosing a chart type (e.g., plot() for a line chart).
  3. Customizing the chart (labels, title, and legend).
  4. Displaying the chart using the show() function.

Let’s try to improve our first chart...

Line Chart

Python

import matplotlib.pyplot as plt

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

# Plotting the line chart
plt.plot(x, y)

# Setting axis labels and title
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Example Line Chart")

# Display the chart
plt.show()

This code creates a line chart showing the y values on the Y axis depending on the x values on the X axis. The functions xlabel(), ylabel(), and title() add labels and a title to the chart.

2. Main Chart Types in Pyplot

1. Line Chart plot()

Line charts are often used to display data over time or analyze trends.

Python

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, marker="o", color="b", linestyle="--")
plt.xlabel("Time")
plt.ylabel("Value")
plt.title("Line Chart")
plt.show()

Here we use the parameters marker, color, and linestyle to customize the chart’s appearance.

2. Histogram hist()

Histograms are handy for showing data distribution and analyzing frequency of values.

Python

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

plt.hist(data, bins=5, color="skyblue", edgecolor="black")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram")
plt.show()

Here the parameter bins defines the number of histogram bars, and edgecolor adds borders to each bar.

3. Pie Chart pie()

Pie charts show the proportions of categories within the overall dataset.

Python

labels = ["Cats", "Dogs", "Birds"]
sizes = [40, 35, 25]

plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
plt.title("Pie Chart")
plt.show()

Here autopct shows percentage values for each category, and startangle rotates the chart.

4. Scatter Plot scatter()

Scatter plots are great for analyzing relationships between two variables.

Python

x = [1, 2, 3, 4, 5]
y = [5, 7, 8, 5, 6]

plt.scatter(x, y, color="red")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot")
plt.show()

A scatter plot comes in handy for finding correlations between values along X and Y axes.

5. Bar Chart bar()

Bar charts are useful for comparing values between categories.

Python

categories = ["A", "B", "C", "D"]
values = [10, 15, 7, 20]

plt.bar(categories, values, color="lightblue")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart")
plt.show()

Here categories and values represent the category labels and corresponding values for the bars.

Now you’ve got the basics of setting up and customizing Matplotlib, and you’re all set to create your first charts. But this is only the beginning of our journey. In the next lectures, we’ll dive deeper into creating different chart types and tweaking them. Eventually, you’ll be creating masterpieces that are not just beautiful but also informative.

Remember, data visualization isn’t just about charts; it’s about storytelling, and Matplotlib will be your storyteller. Catch you later in the next lecture where we’ll level up your skills and unlock new horizons in the world of charts!

1
Task
Python SELF EN, level 41, lesson 1
Locked
Plotting a Linear Graph
Plotting a Linear Graph
2
Task
Python SELF EN, level 41, lesson 1
Locked
Creating a Bar Chart
Creating a Bar Chart
3
Task
Python SELF EN, level 41, lesson 1
Locked
Pie Chart
Pie Chart
4
Task
Python SELF EN, level 41, lesson 1
Locked
Plotting Multiple Graphs on a Single Figure
Plotting Multiple Graphs on a Single Figure
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION