Prepare Interview

Mock Exams

Make Homepage

The Best LIVE Mock Interview - You should go through before Interview

## Intermediate / 1 to 5 years experienced level questions & answers

### Ques 1. 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 2. 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 3. 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 4. 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 5. 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 6. 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 7. 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 8. 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 9. 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 10. 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 11. 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 12. 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()`