Matplotlib 是一个 Python 2D 图形库,可以用于创建各种图表,如图表、散点图、直方图、极坐标图等。以下是一些 Matplotlib 代码库的资源:
快速入门
- Matplotlib 官方文档 - 提供了全面的文档和教程。
常见图表
- 折线图:用于展示数据随时间的变化趋势。
import matplotlib.pyplot as plt x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) plt.show()
- 散点图:用于展示两个变量之间的关系。
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.scatter(x, y) plt.show()
- 直方图:用于展示数据的分布情况。
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(1000) plt.hist(x, bins=30) plt.show()
- 折线图:用于展示数据随时间的变化趋势。
高级功能
- 动画:Matplotlib 也支持创建动画。
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) ax.set_xlim(0, 10) ax.set_ylim(-1, 1) def init(): line.set_data([], []) return line, def update(frame): xdata, ydata = line.get_data() xdata = np.append(xdata, frame) ydata = np.append(ydata, np.sin(frame)) line.set_data(xdata, ydata) return line, ani = FuncAnimation(fig, update, frames=np.linspace(0, 10, 100), init_func=init, blit=True) plt.show()
- 动画:Matplotlib 也支持创建动画。
希望这些资源能够帮助您更好地使用 Matplotlib。🌟
Matplotlib 示例图