机器学习-matplotlib的小尝试

前言:在anacondas环境下,导入matplotlib工具包,用matplotlib画出简单的折线图和散点图

First:折线图源码

from matplotlib import pyplot as plt
from matplotlib import font_manager

# 设置中文的方式,引入windows系统里面的字体,默认不支持中文
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/AdobeHeitiStd-Regular.otf")

# range函数传递的参数 start end step 真正执行结束的值是end的前一个值
x = range(2, 26, 2)
y = [17, 12, 10, 19, 18, 17, 14, 6, 20, 10, 11, 12]

# 指定画出来的图的大小
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)

# 设置x的刻度,第三个参数不传,就表示不指定步长,间隔为1,指定y轴刻度
plt.xticks(range(2, 25))
plt.yticks(range(min(y), max(y)+1))

# 添加描述信息,添加x和y轴描述信息
plt.xlabel("X轴", fontproperties=my_font)
plt.ylabel("y轴", fontproperties=my_font)
plt.title("test-matplotlib表", fontproperties=my_font)

# 保存图片位置
plt.savefig("./01_test_折线图.png")
plt.show()

在这里插入图片描述

Second:散点图源码

#  coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
# 引入系统字体,解决显示中文的问题
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/AdobeHeitiStd-Regular.otf")
y_3 = [14, 15, 16, 17, 16, 18, 13, 14, 15, 16, 14, 16, 17, 18, 18,
       19, 15, 17, 19, 10, 14, 15, 16, 13, 14, 17, 18, 19, 10, 20, 22]
y_10 = [14, 15, 16, 17, 16, 18, 13, 14, 15, 16, 14, 16, 17, 18, 18,
        19, 15, 17, 19, 10, 14, 15, 16, 13, 14, 17, 18, 19, 10, 20, 10]
# 指定画出来的图的大小
plt.figure(figsize=(20, 8), dpi=80)

x_1 = range(1, 32)
x_2 = range(51, 82)

plt.scatter(x_1, y_3, label="3月份")
plt.scatter(x_2, y_10, label="10月份")

# 调整x轴的刻度,旋转45度
_x = list(x_1)+list(x_2)
_x_labels = ["3月{}日".format(i) for i in x_1]
_x_labels += ["10月{}日".format(i-50) for i in x_2]
plt.xticks(_x[::3], _x_labels[::3], fontproperties=my_font, rotation=45)

# 添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.title("3月份和10月份温度表", fontproperties=my_font)

# 添加图例
plt.legend(loc="upper left", prop=my_font)

plt.savefig("./01_test_散点图.png")
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值