import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.xlabel('X 轴') plt.ylabel('Y 轴') plt.title('简单折线图') plt.show()
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [1, 3, 5, 7, 9] plt.plot(x, y1, label='折线 1') plt.plot(x, y2, label='折线 2') plt.xlabel('X 轴') plt.ylabel('Y 轴') plt.title('多条折线图') plt.legend() plt.show()
import matplotlib.pyplot as plt x = ['A', 'B', 'C', 'D', 'E'] y = [10, 20, 30, 40, 50] plt.bar(x, y) plt.xlabel('X 轴') plt.ylabel('Y 轴') plt.title('柱状图') plt.show()
import matplotlib.pyplot as plt labels = ['苹果', '香蕉', '橙子', '其他'] sizes = [30, 25, 20, 25] plt.pie(sizes, labels=labels, autopct='%1.1f%%') plt.axis('equal') plt.title('饼图') plt.show()
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.scatter(x, y) plt.xlabel('X 轴') plt.ylabel('Y 轴') plt.title('散点图') plt.show()
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='viridis') ax.set_xlabel('X 轴') ax.set_ylabel('Y 轴') ax.set_zlabel('Z 轴') ax.set_title('3D 图') plt.show()
这些只是 Python 绘图的一些基本示例,通过 matplotlib
库还可以实现更多复杂和高级的绘图功能,例如自定义颜色、标记、线条样式,以及添加图例、注释等。