Python Matplotlib Interview Questions and Answers
Ques 1. What is Matplotlib and what is its primary use?
Matplotlib is a 2D plotting library for Python. Its primary use is to create static, animated, and interactive visualizations in Python.
Example:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Is it helpful?
Add Comment
View Comments
Ques 2. Explain the difference between `plt.show()` and `plt.savefig()` in Matplotlib.
`plt.show()` displays the plot interactively, while `plt.savefig()` saves the current figure to a file without displaying it.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.savefig('plot.png')
Is it helpful?
Add Comment
View Comments
Ques 3. How to create a scatter plot in Matplotlib?
Use the `plt.scatter()` function to create a scatter plot.
Example:
plt.scatter([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Is it helpful?
Add Comment
View Comments
Ques 4. Explain the difference between `plt.plot()` and `plt.scatter()`.
`plt.plot()` connects data points with lines, while `plt.scatter()` shows individual data points without connecting them.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.scatter([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Is it helpful?
Add Comment
View Comments
Ques 5. How can you add labels to the x-axis and y-axis in a Matplotlib plot?
Use the `plt.xlabel()` and `plt.ylabel()` functions to add labels to the x-axis and y-axis, respectively.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Is it helpful?
Add Comment
View Comments
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?