Python Matplotlib Worksheet
Section A: Multiple Choice Questions (1 mark each)
Q1. Which of the following is used to plot a line chart in Matplotlib?
a) plot()
b) bar()
c) line()
d) lineplot()
Q2. What does the marker 'o' represent in Matplotlib line plots?
a) Square
b) Circle
c) Star
d) Dot
Q3. Which function is used to draw bar charts?
a) bargraph()
b) bar()
c) plotbar()
d) chart()
Q4. What parameter in plt.plot() is used to change the line color to red?
a) linecolor='r'
b) c='red'
c) color='r'
d) col='r'
Q5. Which function is used to draw a pie chart?
a) plt.piechart()
b) plt.circle()
c) plt.pie()
d) plt.donut()
Section B: Practical and Descriptive Questions
Q6. Plot a line chart for the following:
Days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
Sales = [200, 250, 180, 300, 220]
Use a green line with diamond marker.
Q7. Plot two lines on the same chart:
Months = ['Jan', 'Feb', 'Mar', 'Apr']
Product_A = [40, 50, 45, 60]
Product_B = [30, 35, 40, 55]
Use different colors and markers. Add legend, title, and labels.
Q8. Create a bar chart for the number of students in 5 different classes:
Classes = ['Class A', 'Class B', 'Class C', 'Class D', 'Class
E']
Students = [30, 35, 25, 40, 28]
Q9. Modify the above bar chart to use different colors for each bar and add X-axis and Y-axis
labels.
Q10. Create a horizontal bar chart using the same data as in Q8.
Q11. Create a pie chart to show the time spent on activities in a day:
Activities = ['Sleep', 'Work', 'Exercise', 'Leisure']
Hours = [8, 8, 2, 6]
Add percentage labels on the chart.
Q12. In the line chart below, change the line style to dashed and marker to square:
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
Q13. What is the difference between plt.plot() and plt.bar()? Explain with an example
each.
Answer:
• plt.plot() is used for line charts which show trends over time.
• plt.bar() is used for bar charts to compare quantities among categories.
Example:
# Line chart
plt.plot(['Mon', 'Tue', 'Wed'], [10, 20, 15])
plt.title("Line Chart")
plt.show()
# Bar chart
plt.bar(['Apple', 'Banana', 'Cherry'], [50, 30, 45])
plt.title("Bar Chart")
plt.show()