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