0
0
Matplotlibdata~5 mins

Pyplot interface overview in Matplotlib

Choose your learning style9 modes available
Introduction

Pyplot helps you make charts and graphs easily. It lets you draw pictures of your data to understand it better.

You want to see how numbers change over time, like sales each month.
You need to compare groups, like test scores of different classes.
You want to show parts of a whole, like market share of companies.
You want to find patterns or trends in your data visually.
You want to create simple graphs quickly without complex setup.
Syntax
Matplotlib
import matplotlib.pyplot as plt

plt.plot(x, y)
plt.show()
Use plt.plot() to create a line chart by giving x and y data.
Call plt.show() to display the chart window.
Examples
Draws a simple line connecting points (1,4), (2,5), and (3,6).
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Creates a bar chart with categories A, B, C and their values.
Matplotlib
import matplotlib.pyplot as plt

plt.bar(['A', 'B', 'C'], [10, 20, 15])
plt.show()
Plots points as dots instead of lines.
Matplotlib
import matplotlib.pyplot as plt

plt.scatter([1, 2, 3], [4, 5, 6])
plt.show()
Sample Program

This program draws a line chart showing sales for each month. It adds points on the line, labels, and a grid for easier reading.

Matplotlib
import matplotlib.pyplot as plt

# Data for months and sales
months = ['Jan', 'Feb', 'Mar', 'Apr']
sales = [250, 300, 280, 350]

# Create a line plot
plt.plot(months, sales, marker='o', color='blue')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)

# Show the plot
plt.show()
OutputSuccess
Important Notes

Pyplot works like a drawing board where each command adds something to the picture.

Always call plt.show() at the end to see your graph.

You can customize colors, labels, and styles to make your chart clearer.

Summary

Pyplot is a simple way to make charts in Python.

Use plt.plot() and other functions to draw different chart types.

Call plt.show() to display your chart.