Note: Give appropriate title, set xlabel and ylabel while attempting the following questions.
Plot the following data using a line plot:
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Tickets sold | 2000 | 2800 | 3000 | 2500 | 2300 | 2500 | 1000 |
- Before displaying the plot display “Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday” in place of Day 1, 2, 3, 4, 5, 6, 7
- Change the color of the line to ‘Magenta’.
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:
import matplotlib.pyplot as plt: This line imports thepyplotmodule from thematplotliblibrary, which is the standard way to create plots. We alias it aspltfor convenience.days_numeric = [1, 2, 3, 4, 5, 6, 7]andtickets_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.day_names = ["Monday", "Tuesday", ..., "Sunday"]: This list holds the string labels we want to display on the x-axis, corresponding todays_numeric.plt.plot(days_numeric, tickets_sold, color='magenta'): This is the core function call to generate the line plot.days_numericprovides the values for the x-axis.tickets_soldprovides the values for the y-axis.color='magenta'is an argument that fulfills requirement (b), setting the color of the plotted line to magenta.
plt.title("Daily Ticket Sales Over a Week"): This function sets the main title of the plot, providing context to the viewer.plt.xlabel("Day of the Week")andplt.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.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.). …
- The first argument,
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.