Skip to content
Think & Reflect · Q2

Q.How can we make the bar chart of Figure 4.8 horizontal?
[Figure: Figure 4.8 is the customised bar chart output of Program 4-7, which plots 'MelaSales.csv' (day-wise sales for Week 1, Week 2 and Week 3, with column Day on the x axis) using df.plot(kind='bar', x='Day', title='Mela Sales Report', color=['red','yellow','purple'], edgecolor='Green', linewidth=2, linestyle='--').]

Karnataka PUCTextbookSubjective· 2mImportance★★★★★est
89% · 39/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 →

To make the bar chart horizontal, change the kind parameter in the df.plot() method from 'bar' to 'barh'.

A bar chart is a powerful visualization tool for comparing categorical data. While vertical bar charts (where categories are on the x-axis and values on the y-axis) are common, horizontal bar charts are often preferred in specific situations.

Why use a horizontal bar chart?

Horizontal bar charts are particularly useful when:

  1. Category labels are long: If the labels for your categories (like 'Day' in this case, or longer names such as 'Monday Sales', 'Tuesday Sales') are lengthy, a vertical bar chart might make them overlap or require rotation, making them difficult to read. Horizontal bars allow more space for text labels along the y-axis.
  2. There are many categories: When you have a large number of categories, a horizontal layout can sometimes make the chart appear less cluttered and easier to scan.
  3. Comparing magnitudes: For direct comparison of magnitudes, the length of the bar is the primary visual cue. Both vertical and horizontal bars serve this purpose, but the choice often comes down to readability and aesthetic preference based on the data.

Modifying the df.plot() call

The pandas.DataFrame.plot() method, which is a wrapper around Matplotlib, provides a kind parameter to specify the type of plot. The original code uses kind='bar' for a vertical bar chart. To create a horizontal bar chart, we simply need to change this parameter to 'barh'.

When you change kind='bar' to kind='barh':

  • The column specified by the x parameter (which is 'Day' in the original code) will now be plotted along the y-axis, representing the categories.
  • The numerical columns (sales for 'Week 1', 'Week 2', 'Week 3') will be plotted along the x-axis, representing the values.

The other styling parameters like title, color, edgecolor, linewidth, and linestyle will apply similarly to the horizontal bars.

Here's how you would modify the code:

import pandas as pd
import matplotlib.pyplot as plt

# Assume df is loaded from 'MelaSales.csv' as described.
# For demonstration, let's create a sample DataFrame similar to MelaSales.csv
data = {
    'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    'Week 1': [100, 150, 120, 180, 130, 200, 160],
    'Week 2': [120, 130, 160, 150, 140, 220, 170],
    'Week 3': [110, 140, 130, 170, 150, 210, 180]
}
df = pd.DataFrame(data)

# Original plot call (for context, not to be run)
# df.plot(kind='bar', x='Day', title='Mela Sales Report',
#          color=['red','yellow','purple'], edgecolor='Green',
#          linewidth=2, linestyle='--')

# Modified plot call for a horizontal bar chart
df.plot(kind='barh', x='Day', title='Mela Sales Report (Horizontal)',
         color=['red','yellow','purple'], edgecolor='Green',
         linewidth=2, linestyle='--')

plt.xlabel('Sales') # It's good practice to label the new x-axis
plt.ylabel('Day')   # And the new y-axis
plt.tight_layout()  # Adjust layout to prevent labels from overlapping
plt.show()

Expected Output …

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.