W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
# 三行配置使编译器支持绘图功能: import sys import matplotlib matplotlib.use('Agg') import numpy import matplotlib.pyplot as plt numpy.random.seed(2) # 生成随机数据: x = numpy.random.normal(3, 1, 100) # 均值=3,标准差=1 y = numpy.random.normal(150, 40, 100) / x # 均值=150,标准差=40 # 划分训练集和测试集: # 前 80 个样本作为训练集: train_x = x[:80] train_y = y[:80] # 后 20 个样本作为测试集: test_x = x[80:] test_y = y[80:] # 绘制训练集散点图: plt.scatter(train_x, train_y) plt.show() # 两行代码实现图形输出: plt.savefig(sys.stdout.buffer) sys.stdout.flush()