Skip to content
Exercises · Q10
Q.

Note: Give appropriate title, set xlabel and ylabel while attempting the following questions.

Plot the following data using a line plot:

Day1234567
Tickets sold2000280030002500230025001000
  1. Before displaying the plot display “Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday” in place of Day 1, 2, 3, 4, 5, 6, 7
  2. Change the color of the line to ‘Magenta’.
Punjab PsebTextbookSubjective· 4mImportance★★★★★
70% · 31/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 solution demonstrates how to create a line plot using matplotlib.pyplot in Python, customizing the x-axis labels to display day names instead of numbers and changing the line color to magenta.

Data visualization is a powerful technique for understanding trends, patterns, and insights hidden within data. Instead of sifting through raw numbers, a well-designed plot can immediately convey information, making it easier to interpret and communicate findings. For time-series data, like daily ticket sales, a line plot is particularly effective as it clearly shows the progression and changes over time.

Here, we'll use the matplotlib.pyplot library, which is a widely used and versatile tool in Python for creating static, animated, and interactive visualizations. We'll plot the given daily ticket sales data, customize the x-axis to show actual day names, and set the line color as requested.

import matplotlib.pyplot as plt

# 1. Prepare the data
# The numeric days will be used as the actual x-coordinates for plotting.
days_numeric = [1, 2, 3, 4, 5, 6, 7]
tickets_sold = [2000, 2800, 3000, 2500, 2300, 2500, 1000]

# For requirement (a): Custom labels for the x-axis
day_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

# 2. Create the line plot
# We pass the x-coordinates (days_numeric) and y-coordinates (tickets_sold).
# Requirement (b): Change the color of the line to 'Magenta'.
plt.plot(days_numeric, tickets_sold, color='magenta')

# 3. Customize the plot
# Set an appropriate title for the plot.
plt.title("Daily Ticket Sales Over a Week")

# Set the label for the x-axis.
plt.xlabel("Day of the Week")

# Set the label for the y-axis.
plt.ylabel("Number of Tickets Sold")

# Requirement (a): Display day names in place of Day 1, 2, 3, ...
# plt.xticks() takes two arguments:
# The first is the list of locations where the ticks should appear (our numeric days).
# The second is the list of labels to display at those locations (our day names).
plt.xticks(days_numeric, day_names)

# Improve layout to prevent labels from overlapping.
plt.tight_layout()

# 4. Display the plot
plt.show()

Explanation of Key Lines:

  1. import matplotlib.pyplot as plt: This line imports the pyplot module from the matplotlib library, which is the standard way to create plots. We alias it as plt for convenience.
  2. days_numeric = [1, 2, 3, 4, 5, 6, 7] and tickets_sold = [2000, 2800, 3000, 2500, 2300, 2500, 1000]: These lines define the data points for the x-axis (days) and y-axis (tickets sold) respectively.
  3. day_names = ["Monday", "Tuesday", ..., "Sunday"]: This list holds the string labels we want to display on the x-axis, corresponding to days_numeric.
  4. plt.plot(days_numeric, tickets_sold, color='magenta'): This is the core function call to generate the line plot.
    • days_numeric provides the values for the x-axis.
    • tickets_sold provides the values for the y-axis.
    • color='magenta' is an argument that fulfills requirement (b), setting the color of the plotted line to magenta.
  5. plt.title("Daily Ticket Sales Over a Week"): This function sets the main title of the plot, providing context to the viewer.
  6. plt.xlabel("Day of the Week") and plt.ylabel("Number of Tickets Sold"): These functions set the labels for the x-axis and y-axis respectively. Clear axis labels are crucial for understanding what each axis represents.
  7. plt.xticks(days_numeric, day_names): This is the key line for requirement (a).
    • The first argument, days_numeric, specifies the locations on the x-axis where the ticks should be placed (at 1, 2, 3, etc.). …

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.