Sunday 14 April 2024

Introduction to Matplotlib library

Matplotlib - Python's library for data visualization

What is Matplotlib?

Matplotlib is a fundamental Python library for data visualization. It is a powerful and versatile open-source library in Python that allows you to create various static, animated, and interactive visualizations. It excels at generating a wide range of plot types, including:

  • Line plots
  • Bar charts
  • Scatter plots
  • Histograms
  • Pie charts
  • 3D plots
  • And many more

Matplotlib is a cornerstone for data analysis and storytelling in Python. By visualizing your data, you can gain deeper insights, identify trends and patterns, and communicate findings effectively.


Getting Started with Matplotlib - Installation:

If you don't have Matplotlib installed, use pip, and import the matplotlib.pyplot submodule.
 

pip install matplotlib
import matplotlib.pyplot as plt

Example: A Line Plot using Matplotlib

This example creates a line plot showing temperature variations over time:


# Sample data (replace with your actual data)
days = [1, 2, 3, 4, 5]
temperatures = [20, 22, 25, 23, 21]

# Create the plot
plt.plot(days, temperatures)

# Add labels and title
plt.xlabel("Days")
plt.ylabel("Temperature (°C)")
plt.title("Temperature Variation Over 5 Days")

# Display the plot
plt.show()

Output of the code

Line Plot
Figure: Line Plot


Example - Bar Chart using Matplotlib: 

Following example creates a bar chart comparing sales figures for different products.

# Sample data (replace with your actual data)
products = ["Product A", "Product B", "Product C"]
sales = [100, 150, 80]

# Create the bar chart
plt.bar(products, sales)

# Add labels and title
plt.xlabel("Products")
plt.ylabel("Sales")
plt.title("Product Sales Comparison")

# Display the plot
plt.show()


Output of the code


Figure: Bar Chart using Matplotlib

Example of Scatter Plot: 

Following example creates a scatter plot to visualize the relationship between weight and height:

# Sample data (replace with your actual data)
weights = [60, 70, 80, 55, 65]
heights = [170, 180, 175, 165, 172]

# Create the scatter plot
plt.scatter(weights, heights)

# Add labels and title
plt.xlabel("Weight (kg)")
plt.ylabel("Height (cm)")
plt.title("Weight vs. Height")

# Display the plot
plt.show()

Output of the code

Figure: Scatter Plot

Remember to replace the sample data in these examples with your actual data to create meaningful visualizations.

Customization and Beyond

Matplotlib offers extensive customization capabilities. You can fine-tune plot elements like:

  • Line styles
  • Marker shapes
  • Color schemes
  • Grid lines
  • Legend placement
  • Font sizes

Explore the official Matplotlib documentation.



Sunday 24 March 2024

Pandas DataFrame MCQs with Answers

 

Python Programming MCQs

Pandas DataFrame


10 important MCQs related to Python Pandas DataFrame, along with their answers:


1. Which of the following statements accurately describes a DataFrame?

a) A one-dimensional labeled array-like object

b) A multi-dimensional labeled data structure with potentially different data types in each column

c) A collection of Series objects with the same index

d) A specialized dictionary for storing numerical data

2. How do you create a DataFrame from a Python dictionary?

a) df = pd.Series(data)

b) df = pd.DataFrame(dict, index=index_list)

c) df = pd.Array.from_dict(data)

d) df = pd.Dict(data)

3. To access a specific column in a DataFrame, you would use:

a) df.get('column_name')

b) df['column_name']

c) df.column_name

d) df.at['column_name']

4. How can you filter rows in a DataFrame based on a condition?

a) df.filter(condition)

b) df.where(condition)

c) df[condition]

d) df.query(condition)

5. What function would you use to display the summary statistics of a DataFrame's numerical columns?

a) df.head()

b) df.info()

c) df.describe()

d) df.summary()

6. Which function removes missing values (NaN) from a DataFrame?

a) df.fillna(value)

b) df.replace(NaN, value)

c) df.dropna()

d) df.clean()

7. How can you replace missing values (NaN) with a specific value?

a) df.fillna(value)

b) df.replace(NaN, value)

c) df.clean_null(value)  # Not a standard method

d) df.set_null(value)

8. Which function is used to import the pandas library?

a) import numpy as pd

b) import pandas as pd 

c) from pandas import df

d) include pandas

9. How can you create a DataFrame from a CSV file?

a) df = pd.read_csv('data.csv')

b) df = pd.DataFrame.from_csv('data.csv')

c) df = open('data.csv')  # Incorrect approach

d) df = pd.parse_csv('data.csv')

10. Which of the following function can be used to delete a DataFrame column with missing value?

a) df.dropna(axis='columns')

b) df.delete()

c) df.dromna(axis='rows')

d) df.dropna(axis='dataframe')




ANSWERS: Python Pandas DataFrame MCQs:

1. b) A multi-dimensional labeled data structure with potentially different data types in each column

2. b) df = pd.DataFrame(dict, index=index_list)

3. b) df['column_name']

4. c) df[condition]

5. c) df.describe()

6. c) df.dropna()

7. a) df.fillna(value)

8. b) import pandas as pd 

9. a) df = pd.read_csv('data.csv')

10. a) df.dropna(axis='columns')

 

Back to Python MCQs >