0% found this document useful (0 votes)
2 views3 pages

3

The document provides instructions on creating histograms, subplots, and 3D plots using Matplotlib. It includes code examples for each type of plot, demonstrating how to visualize data effectively. Key functions discussed are hist(), plt.subplots(), and the mpl_toolkits.mplot3d module.

Uploaded by

anuj rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

3

The document provides instructions on creating histograms, subplots, and 3D plots using Matplotlib. It includes code examples for each type of plot, demonstrating how to visualize data effectively. Key functions discussed are hist(), plt.subplots(), and the mpl_toolkits.mplot3d module.

Uploaded by

anuj rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. How can you create a histogram in Matplotlib?

ANS - To create a histogram in Matplotlib, you can use the hist()


function. Here's a simple example:
import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
plt.hist(data, bins=4, edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()
This code will create a histogram of the data list with 4 bins. The
edgecolor='black' argument is used to add a black border around each
bin.

2. What is the purpose of the plt.subplots() function in Matplotlib?


ANS - The plt.subplots() function in Matplotlib is used to create a figure
and a set of subplots. It returns a figure and an array of axes objects,
which you can use to create multiple plots in a single figure. It's
especially useful for creating complex visualizations with multiple plots.
Here's an example:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 0].set_title('Plot 1')
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
axs[0, 1].set_title('Plot 2')
axs[1, 0].plot([1, 2, 3], [1, 0.5, 0.2])
axs[1, 0].set_title('Plot 3')
axs[1, 1].plot([1, 2, 3], [2, 2, 2])
axs[1, 1].set_title('Plot 4')

for ax in axs.flat:
ax.set(xlabel='x-label', ylabel='y-label')

plt.tight_layout()
plt.show()
This example creates a 2x2 grid of subplots, each with its own data and
title.

3. How can you create a 3D plot in Matplotlib?


ANS - To create a 3D plot in Matplotlib, you need to use the
mpl_toolkits.mplot3d module. Here's an example of how to create a 3D
scatter plot:
python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

ax.scatter(x, y, z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.title('3D Scatter Plot Example')
plt.show()
In this example:
• fig.add_subplot(111, projection='3d') adds a 3D subplot to the

figure.
• ax.scatter(x, y, z) creates a 3D scatter plot with random data.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy