Python Matplotlib Interview Questions and Answers
Freshers / Beginner level questions & 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()
Ques 2. 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()
Ques 3. 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()
Ques 4. How can you set the limits of the x-axis and y-axis in Matplotlib?
Use `plt.xlim()` and `plt.ylim()` functions to set the limits of the x-axis and y-axis, respectively.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.xlim(0, 5)
plt.ylim(0, 35)
plt.show()
Ques 5. Explain the purpose of the `plt.grid()` function in Matplotlib.
`plt.grid()` adds a grid to the plot, making it easier to read and interpret.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.grid(True)
plt.show()
Ques 6. How can you create a bar chart in Matplotlib?
Use the `plt.bar()` function to create a bar chart in Matplotlib.
Example:
plt.bar([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
Ques 7. Explain the purpose of the `plt.title()` function in Matplotlib.
`plt.title()` is used to add a title to the plot, providing context or information about the data being visualized.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.title('Line Chart')
plt.show()
Ques 8. How can you create a histogram in Matplotlib?
Use the `plt.hist()` function to create a histogram in Matplotlib.
Example:
data = [1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
plt.hist(data, bins=5, edgecolor='black')
plt.show()
Ques 9. What is the purpose of the `plt.tight_layout()` function in Matplotlib?
`plt.tight_layout()` automatically adjusts subplot parameters to ensure that subplots fit into the figure area.
Example:
plt.subplot(2, 1, 1)
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.subplot(2, 1, 2)
plt.scatter([1, 2, 3, 4], [10, 20, 25, 30])
plt.tight_layout()
plt.show()
Ques 10. How can you create a barh (horizontal bar) chart in Matplotlib?
Use the `plt.barh()` function to create a horizontal bar chart in Matplotlib.
Example:
plt.barh(['A', 'B', 'C', 'D'], [10, 20, 25, 30])
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?
- How to create a scatter plot in Matplotlib?
- Explain the purpose of the `plt.grid()` function in Matplotlib.
- How can you set the limits of the x-axis and y-axis in Matplotlib?
Related interview subjects
Deep Learning interview questions and answers - Total 29 questions |
Flask interview questions and answers - Total 40 questions |
PySpark interview questions and answers - Total 30 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 |
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 |