Python Matplotlib perguntas e respostas de entrevista
Pergunta 16. 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()
Pergunta 17. 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()
Pergunta 18. 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()
Pergunta 19. 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()
Pergunta 20. 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()
Mais uteis segundo os usuarios:
- 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?
- What is the purpose of the `plt.tight_layout()` function in Matplotlib?
- How can you create error bars in a Matplotlib plot?