Python Matplotlib Interview Questions and Answers
Ques 6. What is the purpose of `plt.legend()` in Matplotlib?
`plt.legend()` is used to add a legend to the plot, providing information about the plotted data series.
Example:
plt.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Line 1')
plt.legend()
plt.show()
Is it helpful?
Add Comment
View Comments
Ques 7. 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()
Is it helpful?
Add Comment
View Comments
Ques 8. What is a subplot in Matplotlib?
A subplot is a way to organize multiple plots within a single figure. It is created using `plt.subplot()`.
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.show()
Is it helpful?
Add Comment
View Comments
Ques 9. 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()
Is it helpful?
Add Comment
View Comments
Ques 10. 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()
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?
- How to create a scatter plot in Matplotlib?
- How can you create error bars in a Matplotlib plot?
- Explain the purpose of the `plt.grid()` function in Matplotlib.