1.机器学习-matplotlib

折线图

一个坐标系

import random
import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams['font.sans-serif'] = ["SimHei"]
# 设置正常显示符号
mpl.rcParams['axes.unicode_minus'] = False

# 创建画布——图像长宽比,清晰度
plt.figure(figsize=(20, 8), dpi=80)
# 绘制图像,x轴,y轴列表,颜色,线段类型,图例
x = range(60)
y = [random.uniform(15, 18) for i in x]
plt.plot(x, y, label='上海')
##绘制多条图像
x_other = range(60)
y_other = [random.uniform(9, 14) for i in x_other]
plt.plot(x_other, y_other, color="r", linestyle="--", label="北京")
"""
color       linestyle
r   红色     -    实线 
g   绿色     --   虚线
b   蓝色     -.   点划线
w   白色     :    点虚线
c   青色     ''   留空、空格
m   洋红
y   黄色
k   黑色
"""
#显示图例
plt.legend(loc=0)
"""
'best'          0
'upper right'   1
'upper left'    2
'lower left'    3
'lower right'   4
'right'         5
'center left'   6
'center right'  7
'lower center'  8
'upper center'  9
'center'        10
"""
# 添加x,y轴刻度
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(40)
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])
# 添加网格显示,网格线类型,透明度
plt.grid(True, linestyle='--', alpha=0.8)
# 添加描述信息
plt.xlabel("时间", fontsize=15)
plt.ylabel("温度", fontsize=15)
plt.title("中午11点-12点某城市温度变化图", fontsize=20)
# 保存——资源释放前保存
plt.savefig("tableTest.png")

# 图像显示——资源释放
plt.show()

多个坐标系

import random
import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams['font.sans-serif'] = ["SimHei"]
# 设置正常显示符号
mpl.rcParams['axes.unicode_minus'] = False

# 创建画布——图像长宽比,清晰度
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
# 绘制图像,x轴,y轴列表,颜色,线段类型,图例
x = range(60)
y = [random.uniform(15, 18) for i in x]
y_other = [random.uniform(8, 15) for j in x]
axes[0].plot(x, y, label='上海')
axes[1].plot(x, y_other, label='北京', color='r', linestyle='--')

# 显示图例
axes[0].legend(loc=0)
axes[1].legend(loc=0)

# 添加x,y轴刻度
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(40)
axes[0].set_xticks(x[::5])
axes[1].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[1].set_yticks(y_ticks[::5])

axes[0].set_xticklabels(x_ticks_label[::5])
axes[1].set_xticklabels(x_ticks_label[::5])

# 添加网格显示,网格线类型,透明度
axes[0].grid(True, linestyle='--', alpha=0.8)
axes[1].grid(True, linestyle='--', alpha=0.8)
# 添加描述信息
axes[0].set_xlabel("时间", fontsize=15)
axes[1].set_xlabel("时间", fontsize=15)
axes[0].set_ylabel("温度", fontsize=15)
axes[1].set_ylabel("温度", fontsize=15)
axes[0].set_title("中午11点-12点上海市温度变化图", fontsize=20)
axes[1].set_title("中午11点-12点北京市温度变化图", fontsize=20)

# 保存——资源释放前保存
plt.savefig("tableTest2.png")
# 图像显示——资源释放
plt.show()

结合numpy 实现sin函数

import matplotlib.pyplot as plt

import numpy as np

# 准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制函数图像
plt.plot(x, y)
# 添加网格
plt.grid(True, linestyle='--', alpha=0.8)
#显示图像
plt.show()

散点图

import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams['font.sans-serif'] = ["SimHei"]
# 设置正常显示符号
mpl.rcParams['axes.unicode_minus'] = False

# 准备数据
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64, 163.56, 120.06, 207.83, 342.75, 147.9, 53.06,
     224.72, 29.51, 21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.75, 202.51, 247.61, 24.9, 239.34, 140.32, 104.15, 176.84, 288.23, 128.76, 49.64,
     191.75, 33.1, 30.75, 400.05, 205.35, 330.64, 283.15]

# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制图像
plt.scatter(x, y)
# 图像显示
plt.show()

柱状图

import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams['font.sans-serif'] = ["SimHei"]
# 设置正常显示符号
mpl.rcParams['axes.unicode_minus'] = False

# 准备数据
movie_name = ['雷神1', "雷神2", "雷神3", "雷神4", "雷神5", "雷神6", "雷神7", "雷神8", "雷神9", "雷神10", "雷神11"]
x = range(len(movie_name))
y = [73853, 57767, 22354, 15969, 14839, 8425, 8716, 8318, 7916, 6764, 52222]
# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制图像
plt.bar(x, y, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'], width=0.7)
# 修改x轴显示
plt.xticks(x, movie_name)
# 添加网格
plt.grid(linestyle='--', alpha=0.8)
# 添加标题
plt.title("电影收入")
# 图像显示
plt.show()

其他图像

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值