Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ASSIGNMENT – 5

1.Write a Python code to Create a figure with 2x2 subplots ?


Answer:

import matplotlib.pyplot as plt

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot in the first subplot (top left)


axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3])
axs[0, 0].set_title('Subplot 1')

# Plot in the second subplot (top right)


axs[0, 1].scatter([1, 2, 3, 4], [1, 4, 2, 3])
axs[0, 1].set_title('Subplot 2')

# Plot in the third subplot (bottom left)


axs[1, 0].bar([1, 2, 3, 4], [1, 4, 2, 3])
axs[1, 0].set_title('Subplot 3')

# Plot in the fourth subplot (bottom right)


axs[1, 1].plot([3, 2, 1, 4], [1, 2, 3, 4])
axs[1, 1].set_title('Subplot 4')

# Adjust the spacing between subplots


plt.tight_layout()

# Display the figure


plt.show()
2. In the first subplot (top-left), plot a bar chart using the following data:
Categories: ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
Values: [23, 56, 41, 62, 19] ?

Answer:

import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Subplot 1')

# Plot in the second subplot (top right)


axs[0, 1].scatter([1, 2, 3, 4], [1, 4, 2, 3])
axs[0, 1].set_title('Subplot 2')

# Plot in the third subplot (bottom left)


axs[1, 0].plot([1, 2, 3, 4], [1, 4, 2, 3])
axs[1, 0].set_title('Subplot 3')

# Plot in the fourth subplot (bottom right)


axs[1, 1].plot([3, 2, 1, 4], [1, 2, 3, 4])
axs[1, 1].set_title('Subplot 4')

# Adjust the spacing between subplots


plt.tight_layout()

# Display the figure


plt.show()
3. In the second subplot (top-right), plot a histogram using the following data:
Data: [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10] ?

Answer:

import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Data for the histogram


data = [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Subplot 1')

# Plot a histogram in the second subplot (top right)


axs[0, 1].hist(data)
axs[0, 1].set_title('Subplot 2')

# Plot in the third subplot (bottom left)


axs[1, 0].plot([1, 2, 3, 4], [1, 4, 2, 3])
axs[1, 0].set_title('Subplot 3')

# Plot in the fourth subplot (bottom right)


axs[1, 1].plot([3, 2, 1, 4], [1, 2, 3, 4])
axs[1, 1].set_title('Subplot 4')

# Adjust the spacing between subplots


plt.tight_layout()

# Display the figure


plt.show()
4. In the third subplot (bottom-left), plot a pie chart using the following data:
Labels: ['Apple', 'Banana', 'Orange', 'Mango']
Sizes: [30, 25, 15, 30] ?
Answer:
import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Data for the histogram


data = [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10]

# Data for the pie chart


labels = ['Apple', 'Banana', 'Orange', 'Mango']
sizes = [30, 25, 15, 30]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Subplot 1')

# Plot a histogram in the second subplot (top right)


axs[0, 1].hist(data)
axs[0, 1].set_title('Subplot 2')

# Plot a pie chart in the third subplot (bottom left)


axs[1, 0].pie(sizes, labels=labels, autopct='%1.1f%%')
axs[1, 0].set_title('Subplot 3')

# Plot in the fourth subplot (bottom right)


axs[1, 1].plot([3, 2, 1, 4], [1, 2, 3, 4])
axs[1, 1].set_title('Subplot 4')

# Adjust the spacing between subplots


plt.tight_layout()
# Display the figure
plt.show()

5. In the fourth subplot (bottom-right), plot a scatter plot using the following data:
X values: [1, 2, 3, 4, 5]
Y values: [2, 5, 3, 6, 4] ?

Answer:

import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Data for the histogram


data = [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10]

# Data for the pie chart


labels = ['Apple', 'Banana', 'Orange', 'Mango']
sizes = [30, 25, 15, 30]

# Data for the scatter plot


x_values = [1, 2, 3, 4, 5]
y_values = [2, 5, 3, 6, 4]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Subplot 1')

# Plot a histogram in the second subplot (top right)


axs[0, 1].hist(data)
axs[0, 1].set_title('Subplot 2')

# Plot a pie chart in the third subplot (bottom left)


axs[1, 0].pie(sizes, labels=labels, autopct='%1.1f%%')
axs[1, 0].set_title('Subplot 3')
# Plot a scatter plot in the fourth subplot (bottom right)
axs[1, 1].scatter(x_values, y_values)
axs[1, 1].set_title('Subplot 4')

# Adjust the spacing between subplots


plt.tight_layout()

# Display the figure


plt.show()

6. Add appropriate titles and labels to each subplot ?

Answer:

import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Data for the histogram


data = [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10]

# Data for the pie chart


labels = ['Apple', 'Banana', 'Orange', 'Mango']
sizes = [30, 25, 15, 30]

# Data for the scatter plot


x_values = [1, 2, 3, 4, 5]
y_values = [2, 5, 3, 6, 4]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Bar Chart')
axs[0, 0].set_xlabel('Categories')
axs[0, 0].set_ylabel('Values')
# Plot a histogram in the second subplot (top right)
axs[0, 1].hist(data)
axs[0, 1].set_title('Histogram')
axs[0, 1].set_xlabel('Data')
axs[0, 1].set_ylabel('Frequency')

# Plot a pie chart in the third subplot (bottom left)


axs[1, 0].pie(sizes, labels=labels, autopct='%1.1f%%')
axs[1, 0].set_title('Pie Chart')

# Plot a scatter plot in the fourth subplot (bottom right)


axs[1, 1].scatter(x_values, y_values)
axs[1, 1].set_title('Scatter Plot')
axs[1, 1].set_xlabel('X Values')
axs[1, 1].set_ylabel('Y Values')

# Adjust the spacing between subplots


fig.tight_layout()

# Display the figure


plt.show()

7. Adjust the spacing between subplots to avoid overlapping ?

Answer:

import matplotlib.pyplot as plt

# Data for the bar chart


categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 56, 41, 62, 19]

# Data for the histogram


data = [12, 17, 21, 18, 14, 13, 16, 9, 12, 15, 19, 11, 14, 16, 20, 18, 15, 13, 16, 11, 10]

# Data for the pie chart


labels = ['Apple', 'Banana', 'Orange', 'Mango']
sizes = [30, 25, 15, 30]
# Data for the scatter plot
x_values = [1, 2, 3, 4, 5]
y_values = [2, 5, 3, 6, 4]

# Create the figure and subplots


fig, axs = plt.subplots(2, 2)

# Plot a bar chart in the first subplot (top left)


axs[0, 0].bar(categories, values)
axs[0, 0].set_title('Bar Chart')
axs[0, 0].set_xlabel('Categories')
axs[0, 0].set_ylabel('Values')

# Plot a histogram in the second subplot (top right)


axs[0, 1].hist(data)
axs[0, 1].set_title('Histogram')
axs[0, 1].set_xlabel('Data')
axs[0, 1].set_ylabel('Frequency')

# Plot a pie chart in the third subplot (bottom left)


axs[1, 0].pie(sizes, labels=labels, autopct='%1.1f%%')
axs[1, 0].set_title('Pie Chart')

# Plot a scatter plot in the fourth subplot (bottom right)


axs[1, 1].scatter(x_values, y_values)
axs[1, 1].set_title('Scatter Plot')
axs[1, 1].set_xlabel('X Values')
axs[1, 1].set_ylabel('Y Values')

# Adjust the spacing between subplots


plt.tight_layout()

# Display the figure


plt.show()

You might also like