Thursday, November 13, 2025

Matplotlib using List ,tuple, set and Dictionary

 

πŸ“Œ 1. Using LIST with Matplotlib

Lists are the most common data type used for plotting.

Example: Line Plot using List

import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] # list y = [10, 20, 15, 25, 30] # list plt.plot(x, y) plt.title("Plot using List") plt.xlabel("X values") plt.ylabel("Y values") plt.show()

πŸ“Œ 2. Using TUPLE with Matplotlib

Matplotlib accepts tuples just like lists.

Example: Bar Chart using Tuple

import matplotlib.pyplot as plt x = (1, 2, 3, 4) # tuple y = (5, 10, 15, 20) # tuple plt.bar(x, y) plt.title("Bar Chart using Tuple") plt.xlabel("Items") plt.ylabel("Values") plt.show()

πŸ“Œ 3. Using SET with Matplotlib

A set does not maintain order.
We must convert it to a sorted list for plotting.

Example: Scatter Plot using Set

import matplotlib.pyplot as plt x_set = {5, 3, 1, 4, 2} # unordered set y_set = {10, 50, 20, 40, 30} x = sorted(list(x_set)) # convert → sort y = sorted(list(y_set)) plt.scatter(x, y) plt.title("Scatter Plot using Set") plt.xlabel("Values X") plt.ylabel("Values Y") plt.show()

πŸ“Œ 4. Using DICTIONARY with Matplotlib

Dictionary stores data as key:value.

Example: Bar Chart using Dictionary

import matplotlib.pyplot as plt data = { "Apple": 30, "Banana": 45, "Mango": 25, "Orange": 40 } x = list(data.keys()) # fruits y = list(data.values()) # quantities plt.bar(x, y) plt.title("Bar Chart using Dictionary") plt.ylabel("Quantity") plt.show()

🎯 Summary Table

Data TypeAccepted by Matplotlib?Notes
List✔ YesMost commonly used
Tuple✔ YesSame as list
Set⚠️ Yes but needs sortingConvert to list first
Dictionary✔ YesUse keys & values separately

No comments:

Post a Comment

Python Viva Questions

  Basic Python Viva Questions 1. What is Python? Python is a high-level, interpreted, and object-oriented programming language used for w...