Skip to content
Programs · Program 4-2

Q.(Program 4-2) Plot a line chart of date versus temperature by adding Label on X and Y axis, and adding a Title and Grids to the chart, given: date = ["25/12", "26/12", "27/12"] and temp = [8.5, 10.5, 6.8]. (Label the x-axis "Date", the y-axis "Temperature", title the chart "Date wise Temperature", turn the grid on, and set yticks to the temperature values.)

West Bengal WbchseTextbookSubjective· 3mImportance★★★★★est
7% · 3/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 program uses Matplotlib to create a line chart displaying temperature against dates, with custom axis labels, a title, a grid, and specific y-axis tick marks.

When visualizing data, a line chart is an excellent choice for showing trends over a continuous variable, such as time or date. Here, we want to see how temperature changes across different dates. Matplotlib is the standard library in Python for creating static, animated, and interactive visualizations, and it provides all the necessary functions to customize our plot exactly as required.

The core idea is to map our date values to the x-axis and temp values to the y-axis. Then, we'll use specific Matplotlib functions to add descriptive elements like labels, a title, and a grid, making the chart informative and easy to read. Setting custom y-ticks helps highlight the exact temperature values recorded.

import matplotlib.pyplot as plt

# Given data for dates and temperatures
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='-')

# Add labels to the X and Y axes
plt.xlabel("Date")

# Add a label to the Y axis
plt.ylabel("Temperature")

# Add a title to the chart
plt.title("Date wise Temperature")

# Turn on the grid
plt.grid(True)

# Set y-axis ticks to the temperature values
plt.yticks(temp)

# Display the plot
plt.show()

Explanation of Key Lines:

  • import matplotlib.pyplot as plt: This line imports the pyplot module from the matplotlib library, which is the most commonly used module for plotting. We alias it as plt for convenience.
  • date = ["25/12", "26/12", "27/12"] and temp = [8.5, 10.5, 6.8]: These lines define the input data. date will be used for the x-axis, and temp for the y-axis.
  • plt.plot(date, temp, marker='o', linestyle='-'): This is the core function call to create the line chart.
    • The first argument (date) provides the x-coordinates.
    • The second argument (temp) provides the y-coordinates.
    • marker='o' adds circular markers at each data point, making individual points visible.
    • linestyle='-' ensures the points are connected by a solid line.
  • plt.xlabel("Date"): This function sets the label for the x-axis to "Date", clearly indicating what the horizontal axis represents.
  • plt.ylabel("Temperature"): Similarly, this sets the label for the y-axis to "Temperature", explaining the vertical axis.
  • plt.title("Date wise Temperature"): This function adds a title to the entire chart, providing a concise description of the plot's content. …

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.