Welcome to the world of Matplotlib, the coolest way to make your Python code come alive with amazing visuals! Think of it like adding filters and effects to your favorite TikTok videos, but for data!
Matplotlib is a library in Python that lets you create awesome charts and graphs. Imagine you're a DJ at a carnival, and you need to show how many people love each song. Matplotlib helps you create that colorful playlist chart!
First, you'll need to install Matplotlib. Open your command prompt (like opening a new TikTok video) and type:
pip install matplotlib
Let's start with something simple, like plotting a line graph. Imagine you want to show how many mangoes you picked from your backyard each day of the week.
import matplotlib.pyplot as plt
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
mangoes = [5, 8, 6, 7, 10, 12, 14]
plt.plot(days, mangoes)
plt.title("Mango Picking Over the Week")
plt.xlabel("Days")
plt.ylabel("Number of Mangoes")
plt.show()
Next, letβs say you want to compare your friends' favorite foods. You can create a bar chart to show this.
foods = ["Burgers", "Pizza", "Ice Cream", "Fries", "Hot Dogs"]
votes = [45, 60, 30, 20, 15]
plt.bar(foods, votes, color=['red', 'green', 'blue', 'yellow', 'purple'])
plt.title("Favorite Foods")
plt.xlabel("Foods")
plt.ylabel("Votes")
plt.show()
Now, picture this: You want to show how much time you spend on different activities in a day. A pie chart is perfect for this!
activities = ["Studying", "Watching TV", "Playing Games", "Sleeping", "Eating"]
time_spent = [4, 2, 3, 8, 1]
colors = ["gold", "yellowgreen", "lightcoral", "lightskyblue", "orange"]
plt.pie(time_spent, labels=activities, colors=colors, startangle=140, autopct='%1.1f%%')
plt.title("Daily Activities")
plt.axis('equal')
plt.show()
Matplotlib is like your creative toolkit. Just like you use different effects and sounds to make your TikTok videos engaging, you can use Matplotlib to make your data look awesome. Keep exploring and have fun coding!