Python做统计图之美
import numpy as np
import matplotlib.pyplot as plt
# 生成模拟数据
x = np.linspace(0, 10, 1000)
y = np.sin(x)
z = np.cos(x)
# 创建图像
fig = plt.figure()
ax = fig.add_subplot(111)
# 绘制曲线
ax.plot(x, y, label='$\sin x$', color='blue', linewidth=2)
ax.plot(x, z, label='$\cos x$', color='red', linewidth=2)
# 设置坐标轴标签
plt.xlabel('x')
plt.ylabel('y')
# 设置标题
plt.title('$\sin x$ and $\cos x$')
# 显示图例
plt.legend()
# 显示网格
plt.grid()
# 保存图像
plt.savefig('sin_cos_plot.png')
# 显示图像
plt.show()
这段代码使用了Matplotlib库来创建一个包含正弦和余弦函数图像的统计图,并保存为PNG格式。代码展示了如何设置标签、标题、线条样式以及如何添加图例和网格。最后,图像被保存并展示出来。
评论已关闭