Beginner Level (Easy)
1. What is Matplotlib?
Matplotlib is a popular plotting library in Python used to create static, animated, and interactive
visualizations. It is highly customizable and provides multiple ways to represent data visually.
2. How do you create a simple line plot in Matplotlib?
You can create a simple line plot using the plot() function:
python
CopyEdit
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.show()
3. How do you set the title and labels for the axes in Matplotlib?
You can set the title and axis labels using title(), xlabel(), and ylabel():
python
CopyEdit
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
4. What is the difference between plt.plot() and plt.scatter()?
● plt.plot(): Used for creating line plots.
● plt.scatter(): Used for creating scatter plots, which display data points without
connecting lines.
5. How do you create a histogram in Matplotlib?
You can create a histogram using the hist() function:
python
CopyEdit
data = [1, 2, 2, 3, 4, 4, 4, 5, 6]
plt.hist(data, bins=5)
plt.show()
🟡 Intermediate Level
6. How do you create multiple subplots in Matplotlib?
You can use plt.subplots() to create multiple subplots:
python
CopyEdit
fig, axs = plt.subplots(2, 2) # 2x2 grid of subplots
axs[0, 0].plot(x, y)
axs[0, 1].scatter(x, y)
plt.show()
7. What are figsize and dpi in Matplotlib?
● figsize: Determines the size of the figure in inches (width, height).
● dpi: Stands for dots per inch, which defines the resolution of the plot.
Example:
python
CopyEdit
plt.figure(figsize=(10, 5), dpi=100)
8. How do you customize the color and style of a plot?
You can customize the color and style of a plot using the color and linestyle parameters:
python
CopyEdit
plt.plot(x, y, color='red', linestyle='--')
9. How do you add a legend to a plot?
You can add a legend using the legend() function:
python
CopyEdit
plt.plot(x, y, label="Line 1")
plt.legend()
10. What are axis() and xlim()/ylim() used for in Matplotlib?
● axis(): Used to set the limits of all axes in one call. Example: plt.axis([xmin,
xmax, ymin, ymax]).
● xlim() and ylim(): Used to set the limits of the x and y axes, respectively.
11. How do you create a bar chart in Matplotlib?
You can create a bar chart using the bar() function:
python
CopyEdit
x = ['A', 'B', 'C', 'D']
y = [3, 7, 5, 9]
plt.bar(x, y)
plt.show()
12. What is the purpose of tight_layout() in Matplotlib?
tight_layout() adjusts the subplots to make sure they fit in the figure area without
overlapping.
python
CopyEdit
plt.tight_layout()
🔵 Advanced Level
13. What is the difference between plt.plot() and plt.plot_date()?
● plt.plot(): Used for general plotting of data points.
● plt.plot_date(): Specifically designed for plotting dates and times along the x-axis.
14. How do you plot a 3D graph in Matplotlib?
You can plot a 3D graph using Axes3D:
python
CopyEdit
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()
15. How do you save a plot as a file?
You can save a plot using savefig():
python
CopyEdit
plt.plot(x, y)
plt.savefig('plot.png') # Save as PNG file
16. How do you display a heatmap in Matplotlib?
You can display a heatmap using imshow():
python
CopyEdit
import numpy as np
data = np.random.rand(10, 10)
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()
17. What are contour and contourf plots in Matplotlib?
● contour(): Creates contour lines representing the levels of a 2D field.
● contourf(): Creates filled contour plots.
Example:
python
CopyEdit
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.contour(X, Y, Z)
plt.contourf(X, Y, Z)
plt.show()
18. What are scatter() and plot() in Matplotlib used for?
● scatter(): Used for creating scatter plots (points with no connecting lines).
● plot(): Used for creating line plots, which connect data points with lines.
19. How do you plot multiple datasets on the same graph with different styles?
You can plot multiple datasets using multiple plot() or scatter() functions:
python
CopyEdit
plt.plot(x1, y1, label='Dataset 1', linestyle='--')
plt.plot(x2, y2, label='Dataset 2', linestyle='-.')
plt.legend()
plt.show()
20. How do you control the transparency of a plot?
You can control the transparency using the alpha parameter:
python
CopyEdit
plt.plot(x, y, alpha=0.5) # 0 is completely transparent, 1 is opaque