Prepare Interview

Mock Exams

Make Homepage

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 pltplt.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()`

Intermediate / 1 to 5 years experienced level questions & answers

Ques 11. 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')`

Ques 12. 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()`

Ques 13. 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()`

Ques 14. 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()`

Ques 15. What is the role of the `plt.xticks()` and `plt.yticks()` functions in Matplotlib?

`plt.xticks()` and `plt.yticks()` are used to customize the tick positions and labels on the x-axis and y-axis, respectively.

Example:

`plt.plot([1, 2, 3, 4], [10, 20, 25, 30])plt.xticks([1, 2, 3, 4], ['A', 'B', 'C', 'D'])plt.show()`

Ques 16. What is the purpose of the `plt.imshow()` function in Matplotlib?

`plt.imshow()` is used to display images in Matplotlib plots.

Example:

`import matplotlib.image as mpimgimg = mpimg.imread('image.png')plt.imshow(img)plt.show()`

Ques 17. How can you add text annotations to a Matplotlib plot?

Use the `plt.text()` function to add text annotations at specified coordinates on the plot.

Example:

`plt.plot([1, 2, 3, 4], [10, 20, 25, 30])plt.text(2, 15, 'Annotation')plt.show()`

Ques 18. Explain the purpose of the `plt.pie()` function in Matplotlib.

`plt.pie()` is used to create a pie chart in Matplotlib, representing data in a circular statistical graphic.

Example:

`sizes = [20, 30, 40, 10]labels = ['A', 'B', 'C', 'D']plt.pie(sizes, labels=labels, autopct='%1.1f%%')plt.show()`

Ques 19. How can you create a logarithmic scale in Matplotlib?

Use the `plt.xscale()` and `plt.yscale()` functions with the argument 'log' to create a logarithmic scale on the x-axis and y-axis, respectively.

Example:

`plt.plot([1, 2, 3, 4], [10, 20, 30, 40])plt.xscale('log')plt.yscale('log')plt.show()`

Ques 20. How can you create error bars in a Matplotlib plot?

Use the `plt.errorbar()` function to create a plot with error bars.

Example:

`x = [1, 2, 3, 4]y = [10, 20, 25, 30]error = [1, 2, 1, 3]plt.errorbar(x, y, yerr=error, fmt='o', capsize=5)plt.show()`

Ques 21. How can you create a violin plot in Matplotlib?

Use the `plt.violinplot()` function to create a violin plot, which is used for visualizing the distribution of data.

Example:

`data = [np.random.normal(0, std, 100) for std in range(1, 4)]plt.violinplot(data, showmedians=True)plt.show()`

Ques 22. How can you create a polar plot in Matplotlib?

Use the `plt.polar()` function to create a polar plot, which is useful for visualizing data in circular coordinates.

Example:

`theta = np.linspace(0, 2*np.pi, 100)r = 1 + np.sin(3*theta)plt.polar(theta, r)plt.show()`

Experienced / Expert level questions & answers

Ques 23. 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 24. 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 mplot3dfig = 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 25. 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 26. 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 27. 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 28. 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 npx, 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 29. 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 30. 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()`