Python Matplotlib Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. How can you customize the color and style of a plot in Matplotlib?
You can use the `color` and `linestyle` parameters in the `plt.plot()` function to customize color and style.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='green', linestyle='--')
plt.show()
Ques 2. How can you create a 3D plot using Matplotlib?
Use the `mplot3d` toolkit in Matplotlib and functions like `ax.plot3D()` to create 3D plots.
Example:
from mpl_toolkits import mplot3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D([1, 2, 3, 4], [10, 20, 25, 30], [5, 10, 15, 20])
plt.show()
Ques 3. What is the purpose of the `plt.fill_between()` function in Matplotlib?
`plt.fill_between()` is used to fill the area between two horizontal curves on the plot.
Example:
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [5, 15, 20, 25]
plt.fill_between(x, y1, y2, color='gray', alpha=0.5)
plt.show()
Ques 4. What is the purpose of the `plt.subplot2grid()` function in Matplotlib?
`plt.subplot2grid()` is used to create a subplot with a grid-like placement in the figure.
Example:
plt.subplot2grid((2, 2), (0, 0))
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Ques 5. What is the purpose of the `plt.subplot2grid()` function in Matplotlib?
`plt.subplot2grid()` is used to create a subplot with a grid-like placement in the figure.
Example:
plt.subplot2grid((2, 2), (0, 0))
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Ques 6. How can you create a streamplot in Matplotlib?
Use the `plt.streamplot()` function to create a streamplot, which visualizes a 2D vector field.
Example:
import numpy as np
x, y = np.meshgrid(np.linspace(-2, 2, 20), np.linspace(-2, 2, 20))
u = np.cos(x)
v = np.sin(y)
plt.streamplot(x, y, u, v)
plt.show()
Ques 7. What is the purpose of the `plt.subplot2grid()` function in Matplotlib?
`plt.subplot2grid()` is used to create a subplot with a grid-like placement in the figure.
Example:
plt.subplot2grid((2, 2), (0, 0))
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Ques 8. What is the purpose of the `plt.annotate()` function in Matplotlib?
`plt.annotate()` is used to add annotations with arrows to points on the plot.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.annotate('Max Value', xy=(3, 30), xytext=(2.5, 28), arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
Most helpful rated by users:
- What is Matplotlib and what is its primary use?
- How can you add labels to the x-axis and y-axis in a Matplotlib plot?
- Explain the purpose of the `plt.grid()` function in Matplotlib.
- How to create a scatter plot in Matplotlib?
Related interview subjects
Python Matplotlib interview questions and answers - Total 30 questions |
Django interview questions and answers - Total 50 questions |
Pandas interview questions and answers - Total 30 questions |
Deep Learning interview questions and answers - Total 29 questions |
PySpark interview questions and answers - Total 30 questions |
Flask interview questions and answers - Total 40 questions |
PyTorch interview questions and answers - Total 25 questions |
Data Science interview questions and answers - Total 23 questions |
SciPy interview questions and answers - Total 30 questions |
Generative AI interview questions and answers - Total 30 questions |
NumPy interview questions and answers - Total 30 questions |
Python interview questions and answers - Total 106 questions |
Python Pandas interview questions and answers - Total 48 questions |