Menu iconMenu iconData Analysis Foundations with Python
Data Analysis Foundations with Python

Chapter 7: Data Visualization with Matplotlib and Seaborn

7.4 Practical Exercises - Chapter 7: Data Visualization with Matplotlib and Seaborn

Exercise 1: Basic Line Plot

Create a simple line plot using Matplotlib to visualize the function \( y = x^2 \) for \( x \) ranging from 0 to 10.

Solution:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x ** 2

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.show()

Exercise 2: Bar Chart with Seaborn

Create a bar chart using Seaborn to visualize the average petal length for each species in the Iris dataset.

Solution:

import seaborn as sns

df = sns.load_dataset('iris')
sns.barplot(x='species', y='petal_length', data=df)
plt.show()

Exercise 3: Scatter Plot Matrix

Use Seaborn to create a scatter plot matrix of the Iris dataset focusing on the variables 'sepal_length', 'sepal_width', 'petal_length', and 'petal_width'.

Solution:

sns.pairplot(df, vars=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], hue='species')
plt.show()

Exercise 4: Advanced Plot - Heatmap

Visualize a correlation matrix of the Iris dataset using a heatmap in Seaborn.

Solution:

correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.show()

Exercise 5: Customize Your Plot

Take the line plot you created in Exercise 1 and customize it by adding grid lines, changing line style, and adding markers.

Solution:

plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.grid(True)
plt.show()

We hope you find these exercises beneficial. Remember, the more you practice, the more proficient you'll become. Enjoy plotting!

7.4 Practical Exercises - Chapter 7: Data Visualization with Matplotlib and Seaborn

Exercise 1: Basic Line Plot

Create a simple line plot using Matplotlib to visualize the function \( y = x^2 \) for \( x \) ranging from 0 to 10.

Solution:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x ** 2

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.show()

Exercise 2: Bar Chart with Seaborn

Create a bar chart using Seaborn to visualize the average petal length for each species in the Iris dataset.

Solution:

import seaborn as sns

df = sns.load_dataset('iris')
sns.barplot(x='species', y='petal_length', data=df)
plt.show()

Exercise 3: Scatter Plot Matrix

Use Seaborn to create a scatter plot matrix of the Iris dataset focusing on the variables 'sepal_length', 'sepal_width', 'petal_length', and 'petal_width'.

Solution:

sns.pairplot(df, vars=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], hue='species')
plt.show()

Exercise 4: Advanced Plot - Heatmap

Visualize a correlation matrix of the Iris dataset using a heatmap in Seaborn.

Solution:

correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.show()

Exercise 5: Customize Your Plot

Take the line plot you created in Exercise 1 and customize it by adding grid lines, changing line style, and adding markers.

Solution:

plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.grid(True)
plt.show()

We hope you find these exercises beneficial. Remember, the more you practice, the more proficient you'll become. Enjoy plotting!

7.4 Practical Exercises - Chapter 7: Data Visualization with Matplotlib and Seaborn

Exercise 1: Basic Line Plot

Create a simple line plot using Matplotlib to visualize the function \( y = x^2 \) for \( x \) ranging from 0 to 10.

Solution:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x ** 2

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.show()

Exercise 2: Bar Chart with Seaborn

Create a bar chart using Seaborn to visualize the average petal length for each species in the Iris dataset.

Solution:

import seaborn as sns

df = sns.load_dataset('iris')
sns.barplot(x='species', y='petal_length', data=df)
plt.show()

Exercise 3: Scatter Plot Matrix

Use Seaborn to create a scatter plot matrix of the Iris dataset focusing on the variables 'sepal_length', 'sepal_width', 'petal_length', and 'petal_width'.

Solution:

sns.pairplot(df, vars=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], hue='species')
plt.show()

Exercise 4: Advanced Plot - Heatmap

Visualize a correlation matrix of the Iris dataset using a heatmap in Seaborn.

Solution:

correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.show()

Exercise 5: Customize Your Plot

Take the line plot you created in Exercise 1 and customize it by adding grid lines, changing line style, and adding markers.

Solution:

plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.grid(True)
plt.show()

We hope you find these exercises beneficial. Remember, the more you practice, the more proficient you'll become. Enjoy plotting!

7.4 Practical Exercises - Chapter 7: Data Visualization with Matplotlib and Seaborn

Exercise 1: Basic Line Plot

Create a simple line plot using Matplotlib to visualize the function \( y = x^2 \) for \( x \) ranging from 0 to 10.

Solution:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x ** 2

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.show()

Exercise 2: Bar Chart with Seaborn

Create a bar chart using Seaborn to visualize the average petal length for each species in the Iris dataset.

Solution:

import seaborn as sns

df = sns.load_dataset('iris')
sns.barplot(x='species', y='petal_length', data=df)
plt.show()

Exercise 3: Scatter Plot Matrix

Use Seaborn to create a scatter plot matrix of the Iris dataset focusing on the variables 'sepal_length', 'sepal_width', 'petal_length', and 'petal_width'.

Solution:

sns.pairplot(df, vars=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], hue='species')
plt.show()

Exercise 4: Advanced Plot - Heatmap

Visualize a correlation matrix of the Iris dataset using a heatmap in Seaborn.

Solution:

correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.show()

Exercise 5: Customize Your Plot

Take the line plot you created in Exercise 1 and customize it by adding grid lines, changing line style, and adding markers.

Solution:

plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = x^2')
plt.grid(True)
plt.show()

We hope you find these exercises beneficial. Remember, the more you practice, the more proficient you'll become. Enjoy plotting!