0
0
Matplotlibdata~5 mins

What is Matplotlib

Choose your learning style9 modes available
Introduction

Matplotlib helps you make pictures from data. It turns numbers into graphs so you can see patterns easily.

You want to show how sales changed over months with a line chart.
You need to compare different groups using bar charts.
You want to see the distribution of exam scores with a histogram.
You want to add labels and titles to explain your data visually.
You want to save your charts as images to share with others.
Syntax
Matplotlib
import matplotlib.pyplot as plt

plt.plot(x_values, y_values)
plt.show()
Use import matplotlib.pyplot as plt to access plotting functions.
The plt.plot() function draws a line chart by default.
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()
Shows a histogram to see how often numbers appear.
Matplotlib
import matplotlib.pyplot as plt

plt.hist([1, 2, 2, 3, 3, 3, 4])
plt.show()
Sample Program

This program draws a line chart showing sales for each month. It adds a title and labels to explain the chart.

Matplotlib
import matplotlib.pyplot as plt

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

# Create a line chart
plt.plot(months, sales)
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
OutputSuccess
Important Notes

Matplotlib works well with other Python tools like NumPy and pandas.

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

Always call plt.show() to display your chart.

Summary

Matplotlib turns data into pictures like line charts and bar graphs.

It helps you understand and share data visually.

Use simple commands to create and customize charts.