📜  pyplot python (1)

📅  最后修改于: 2023-12-03 15:03:55.787000             🧑  作者: Mango

Pyplot Python

Pyplot is a module in the Matplotlib library, which is used for creating static, animated, and interactive visualizations in Python. It is a collection of functions that helps in creating a variety of charts such as histograms, bar charts, scatterplots, etc.

Installing Pyplot
!pip install matplotlib
Importing Pyplot
import matplotlib.pyplot as plt
Creating a Basic Plot
import numpy as np

x = np.arange(0, 10, 0.1)
y = np.sin(x)

plt.plot(x, y)
plt.show()
Adding Labels, Title and Legend
plt.plot(x, y, label="sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Sine Function")
plt.legend()
plt.show()
Multiple Plots in a Figure
y1 = np.sin(x)
y2 = np.cos(x)

plt.subplot(2, 1, 1)
plt.plot(x, y1)
plt.title('Sine Function')

plt.subplot(2, 1, 2)
plt.plot(x, y2)
plt.title('Cosine Function')

plt.tight_layout()
plt.show()
Saving the Plot
plt.savefig('sine.png')
Conclusion

In this article, we have discussed the basics of Pyplot module in Python. We saw how to install it, import it and use it to create basic plots. We also added labels, title, and legends to the plots, created multiple plots in a single figure and saved the plots. Pyplot is a powerful tool for visualizing data and is widely used in scientific and data visualization applications.