Skip to content
Exercises · Q11

Q.Collect data about colleges in Delhi University or any other university of your choice and number of courses they run for Science, Commerce and Humanities, store it in a CSV file and present it using a bar plot.

Rajasthan RbseTextbookSubjective· 3mImportance★★★★★
73% · 32/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 →

Collect college-wise course counts across three streams (Science, Commerce, Humanities), store them in a CSV file, and visualize the distribution using a bar plot to compare offerings at a glance.

Why a bar plot?

When you want to compare categorical data — here, colleges — across multiple groups (the three streams), a bar plot is the natural choice. Each college gets a cluster of bars, one per stream, making it easy to see which institutions emphasize Science, which offer balanced programs, and which focus on Humanities or Commerce. The visual comparison is immediate: bar height encodes quantity, and grouping by college lets you spot patterns across the university.

The workflow has three parts: data collection, CSV storage, and visualization. You will use pandas to handle the DataFrame and CSV operations, and matplotlib to draw the plot.


Step 1: Collect and structure the data

Suppose you gather data for five colleges in Delhi University. You record the number of courses each runs in Science, Commerce, and Humanities. Here is a sample dataset:

CollegeScienceCommerceHumanities
St. Stephen's College12815
Hindu College141012
Miranda House11713
Ramjas College13914
Hansraj College15610

You can create this in Python as a dictionary, convert it to a DataFrame, and write it to CSV.


Step 2: Write the data to a CSV file

import pandas as pd

# Data collection: college names and course counts
data = {
    'College': [
        "St. Stephen's College",
        'Hindu College',
        'Miranda House',
        'Ramjas College',
        'Hansraj College'
    ],
    'Science': [12, 14, 11, 13, 15],
    'Commerce': [8, 10, 7, 9, 6],
    'Humanities': [15, 12, 13, 14, 10]
}

# Create DataFrame
df = pd.DataFrame(data)

# Save to CSV
df.to_csv('du_colleges_courses.csv', index=False)

print("CSV file created successfully.")
print(df)

Output:

CSV file created successfully.
                  College  Science  Commerce  Humanities
0  St. Stephen's College       12         8          15
1          Hindu College       14        10          12
2          Miranda House       11         7          13
3         Ramjas College       13         9          14
4        Hansraj College       15         6          10

The index=False argument prevents pandas from writing row numbers into the CSV. The file du_colleges_courses.csv now contains your data in a portable, human-readable format.


Step 3: Read the CSV and create a bar plot

import matplotlib.pyplot as plt

# Read the CSV file
df = pd.read_csv('du_colleges_courses.csv')

# Set up the plot
fig, ax = plt.subplots(figsize=(10, 6))

# Define bar width and positions
bar_width = 0.25
x = range(len(df))

# Plot bars for each stream
ax.bar([i - bar_width for i in x], df['Science'], 
       width=bar_width, label='Science', color='#3498db')
ax.bar(x, df['Commerce'], 
       width=bar_width, label='Commerce', color='#2ecc71')
ax.bar([i + bar_width for i in x], df['Humanities'], 
       width=bar_width, label='Humanities', color='#e74c3c')

# Customize the plot
ax.set_xlabel('College', fontsize=12, fontweight='bold')
ax.set_ylabel('Number of Courses', fontsize=12, fontweight='bold')
ax.set_title('Course Distribution Across Delhi University Colleges', 
             fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(df['College'], rotation=15, ha='right')
ax.legend()
ax.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.savefig('du_courses_barplot.png', dpi=300)
plt.show()

What the plot shows

The bar plot displays five groups along the x-axis, one per college. Within each group, three bars stand side by side:

  • Blue (Science): the number of Science courses
  • Green (Commerce): the number of Commerce courses
  • Red (Humanities): the number of Humanities courses

The y-axis runs from 0 to about 16, marking the course count. You can immediately see that Hansraj College offers the most Science courses (15), while St. Stephen's leads in Humanities (15). Commerce offerings are more uniform, ranging from 6 to 10.

A legend in the upper-right corner identifies the three streams. The x-axis labels are rotated 15° to prevent overlap. A faint horizontal grid helps you read exact values.


Key lines explained

ax.bar([i - bar_width for i in x], df['Science'], width=bar_width, ...) …

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.