python图表学习(一)
简单的绘制图表示例
《python编程 从入门到实践》:
import matplotlib.pyplot as plt
# 计算数值
x_values = list(range(1, 101))
y_values = [x**2 for x in x_values]
# 绘制图表
# plt.scatter(x_values, y_values, c='red', s=30)
# 绘制图表,并使用颜色映射 突出数据的规律
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=30)
# 修改图表标题,并给坐标轴加上标签
plt.title('Square Numbers', fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params('both', which='major', labelsize=14)
# 设置每个坐标轴的取值范围 X轴和Y轴
plt.axis([0, 110, 0, 11000])
# 显示图表
plt.show()
# 保存图表 bbox_inches='tight'表示删除图表多余的空白区域
plt.savefig('square_plot.png', bbox_inches='tight')
运行结果: