Plotting with Pyplot - I

1 What is ‘Pyplot’ in Matplotlib?
Ans. Pyplot` is a module in the Matplotlib library used for creating static, interactive, and animated visualizations in Python.
2. How do you import the Matplotlib library in Python?**
Ans. import matplotlib.pyplot as plt.
3. Which function is used to create a line graph in Pyplot?
Ans. The plot() function is used.
4. What is the purpose of the `show()` function in Matplotlib?
Ans. It displays the graph or plot on the screen.
5. How do you set labels for the X-axis and Y-axis in a graph?**
Ans.
plt.xlabel(“X-axis Label”)
plt.ylabel(“Y-axis Label”)
6. How can you add a title to a graph?
Ans. plt.title(“Graph Title”)

7. What does the `legend()` function do in Pyplot?
Ans. It adds a legend to the graph to identify different lines, bars, or data points.
8. Which function is used to create a bar graph in Pyplot?

Ans.The `bar()` function is used.
9. How do you change the color of a line in a line graph?
Ans. Use the `color` parameter in the `plot()` function.
plt.plot(x, y, color=’red’)

10. What is the default style of a line in Matplotlib?
Ans. A solid line.
11. What is the syntax to plot a line graph using Pyplot?
Ans. plt.plot(x, y)

12.How can you plot multiple lines on the same graph?
Ans. Call the `plot()` function multiple times before `show()`.
plt.plot(x1, y1, label=”Line 1″)
plt.plot(x2, y2, label=”Line 2″)
plt.legend()
13. How do you change the style of a line in a line graph (e.g., dashed or dotted)?
Ans . Use the ‘linestyle’ parameter.
plt.plot(x, y, linestyle=’–‘) # Dashed line

14. What is the role of the ‘marker’ parameter in a line graph?**
Ans . It specifies the style of markers for individual data points.
plt.plot(x, y, marker=’o’) # Circle markers

15. How can you adjust the width of a line in a line graph?
Ans. Use the `linewidth` parameter.
plt.plot(x, y, linewidth=2)
16. What is the difference between `plot()` and `scatter()` in Pyplot?**
Ans. ‘plot()’ creates a continuous line graph, while `scatter()` creates a scatter plot with individual points.
17. What is the syntax to create a bar graph in Pyplot?**
Ans. plt.bar(x, y)

18. How do you create a horizontal bar graph?**
Ans. Use the ‘barh()’ function.
plt.barh(x, y)

19. How can you change the width of bars in a bar graph?
Ans. Use the ‘width’ parameter.
plt.bar(x, y, width=0.5)

20. What is the role of the ‘color’ parameter in a bar graph?
Ans. It specifies the color of the bars.
plt.bar(x, y, color=’green’)

21. How do you add data labels to the bars in a bar graph?
Ans. Use the ‘text()’ function.
for i, v in enumerate(y):
plt.text(x[i], v + 0.5, str(v))

22. How can you create a grouped bar graph?
Ans. Use multiple `bar()` calls with adjusted positions.
plt.bar(x – 0.2, y1, width=0.4, label=”Group 1″)
plt.bar(x + 0.2, y2, width=0.4, label=”Group 2″)

23. What is the purpose of the `edgecolor` parameter in a bar graph?
Ans. It specifies the color of the edges of the bars.
plt.bar(x, y, edgecolor=’black’)

24. What is a histogram used for?
Ans. It represents the frequency distribution of a dataset.
25. How do you create a histogram in Pyplot?
Ans. plt.hist(data)

26. What is the purpose of the `bins` parameter in a histogram?
Ans. It specifies the number of intervals (bins) for grouping data.
27. How can you change the color of the bars in a histogram?
Ans. Use the `color` parameter.
plt.hist(data, color=’blue’)

28. What is the difference between a bar graph and a histogram?
Ans . A bar graph compares categories, while a histogram represents a frequency distribution.
29. How do you set the transparency of bars in a histogram?
Ans. Use the `alpha` parameter.
plt.hist(data, alpha=0.7)

30. What does the ‘density’ parameter in a histogram do?
Ans. It normalizes the data to represent probabilities instead of counts.
31. How can you save a graph as an image file in Pyplot?
Ans. plt.savefig(“graph.png”)

32. What is the role of the `grid()` function in Matplotlib?
Ans. It adds a grid to the graph for better readability.
33. How do you set limits for the X-axis and Y-axis in a graph?
Ans.
plt.xlim(min_x, max_x)
plt.ylim(min_y, max_y)

34. What is the purpose of the `xticks()` and `yticks()` functions?
Ans.They customize the ticks and labels on the X-axis and Y-axis.
35. How can you display multiple plots in the same figure using Pyplot?
Ans. Use the ‘subplot()’ function.
plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.subplot(1, 2, 2)
plt.plot(x, y2)

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.