Bird
0
0
Raspberry Piprogramming~5 mins

Matplotlib for data visualization in Raspberry Pi

Choose your learning style9 modes available
Introduction

Matplotlib helps you turn numbers into pictures. Pictures make it easier to understand data quickly.

You want to see how temperatures change over a week.
You need to compare sales numbers for different products.
You want to show your sensor readings on a graph.
You want to find patterns in your data by looking at charts.
Syntax
Raspberry Pi
import matplotlib.pyplot as plt

plt.plot(x_values, y_values)
plt.title('Title of the graph')
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.show()

Use plt.plot() to draw lines or points on the graph.

plt.show() displays the graph window.

Examples
This draws a simple line graph connecting points (1,10), (2,20), (3,25), and (4,30).
Raspberry Pi
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.show()
This draws a bar chart with bars at x positions and heights from y values.
Raspberry Pi
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.bar(x, y)
plt.show()
This draws points (dots) at the given x and y positions.
Raspberry Pi
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y)
plt.show()
Sample Program

This program shows temperature changes over five days using a line graph with dots on each day.

Raspberry Pi
import matplotlib.pyplot as plt

# Data for days and temperature
x = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
y = [22, 24, 19, 23, 25]

plt.plot(x, y, marker='o')
plt.title('Temperature over a week')
plt.xlabel('Day')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

On Raspberry Pi, make sure you have Matplotlib installed using pip install matplotlib.

You can customize colors and styles by adding extra options to plotting functions.

Graphs help you spot trends and compare data easily.

Summary

Matplotlib turns data into easy-to-understand pictures.

You can draw lines, bars, or points to show your data.

Use titles and labels to explain what your graph shows.