Skip to content
Programs · Program 4-1

Q.(Program 4-1: Plotting Temperature against Height) In a city, the maximum temperature of a day is recorded for three consecutive days. Plot the temperature values against the given dates as a line chart, given: date = ["25/12", "26/12", "27/12"] and temp = [8.5, 10.5, 6.8].

West Bengal WbchseTextbookSubjective· 3mImportance★★★★★est
2% · 1/44 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This is a code-writing task — you need to write a Python program using Matplotlib to plot a line chart of temperature against dates for three consecutive days.

Why Matplotlib for this task

Matplotlib is the standard plotting library in Python for creating static, interactive, and animated visualizations. For a simple line chart showing how temperature changes over time, pyplot.plot() is the right tool — it connects data points with straight lines, making trends immediately visible. The pyplot module gives you a MATLAB-like interface that's intuitive for beginners.

The core idea: you have two sequences of equal length — dates (x-axis) and temperatures (y-axis). plt.plot(x, y) creates the line, and then you add labels, a title, and display it.

The complete solution

import matplotlib.pyplot as plt

# Given data
date = ["25/12", "26/12", "27/12"]
temp = [8.5, 10.5, 6.8]

# Create the line chart
plt.plot(date, temp, marker='o', linestyle='-', color='b', label='Max Temperature')

# Add labels and title
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Maximum Temperature over Three Days')

# Add a grid for readability
plt.grid(True, linestyle='--', alpha=0.7)

# Add legend
plt.legend()

# Display the plot
plt.show()

Key lines explained

  • plt.plot(date, temp, marker='o', ...) — The marker='o' adds circular markers at each data point so the exact values are visible. Without markers, only the connecting line appears, which can make individual readings hard to spot.
  • plt.xlabel() and plt.ylabel() — Always label your axes. The y-axis label includes the unit (°C) because a number without a unit is meaningless in science.
  • plt.grid(True) — A grid helps the reader estimate values visually. The alpha=0.7 makes it subtle so it doesn't overpower the data.
  • plt.legend() — Even with one line, a legend is good practice. It becomes essential when you add more series later.
Tip

You can combine all line properties in one plt.plot() call: marker, linestyle, color, and label. This is cleaner than setting them separately.

What the output shows

The chart displays: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.