1 Matplotlib

1 实现一个简单的Matplotlib

import matplotlib.pyplot as plt

# 1.创建画布
# dpi有关图像清晰度
plt.figure(figsize=(20, 8), dpi=100)

# 2.绘制图像
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)

# 3.显示图像
plt.show()

在这里插入图片描述

2 折线图绘制与显示

import matplotlib.pyplot as plt

# 1.创建画布
plt.figure(figsize=(10, 10))

# 2.绘制折线图
plt.plot([1,2,3,4,5,6,7], [17,17,18,15,11,11,13])

# 3.显示图像
plt.show()

在这里插入图片描述

3 图片的保存

import matplotlib.pyplot as plt

# 1.创建画布
plt.figure(figsize=(10, 10))

# 2.绘制折线图
plt.plot([1,2,3,4,5,6,7], [17,17,18,15,11,11,13])

# 3.图片的保存,注意不能在show之后保存,不然只能保存空图片
plt.savefig('test.png')
plt.show()

在这里插入图片描述

4 初始折线图

import matplotlib.pyplot as plt
import random

# 1.准备x,y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制折线图
plt.plot(x, y_shanghai)

# 4.显示图像
plt.show()

在这里插入图片描述

5 添加自定义x,y刻度

import matplotlib.pyplot as plt
import random

# 1.准备x,y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制折线图
plt.plot(x, y_shanghai)

# 4.构造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])

# 5.显示图像
plt.show()

在这里插入图片描述

6 中文乱码的解决

6.1 首先找到对应的环境路径,将simhei.ttf复制进去

lib/python3.x/site-packages/matplotlib/mpl-data/fonts/ttf

6.2 打开mpl-data文件夹下的matplotlibrc 在最后加上

font.family         : sans-serif
font.sans-serif         : SimHei
axes.unicode_minus  : False

6.3 删除缓存

# 一般在~/.matplotlib,把里面的内容删了即可
# 若找不到则在,~/.cache/matplotlib中

6.4 最后重启jupyter即可

7 添加网格显示

import matplotlib.pyplot as plt
import random

# 1.准备x,y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制折线图
plt.plot(x, y_shanghai)

# 4.构造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])

# 5.添加网格显示
plt.grid(True, linestyle='--', alpha=1)

# 6.显示图像
plt.show()

在这里插入图片描述

8 添加描述信息

import matplotlib.pyplot as plt
import random

# 1.准备x,y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制折线图
plt.plot(x, y_shanghai)

# 4.构造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])

# 5.添加网格显示
plt.grid(True, linestyle='--', alpha=0.5)

# 6.添加描述信息
plt.xlabel('时间')
plt.ylabel('温度')
plt.title('中午11:00-12:00温度变化图示')

# 7.显示图像
plt.show()

在这里插入图片描述

9 多次plot

import matplotlib.pyplot as plt
import random


x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]

plt.figure(figsize=(20, 8), dpi=80)

plt.plot(x, y_shanghai, label='上海')
plt.plot(x, y_beijing, color='r', linestyle='--', label='北京')

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.5)

plt.xlabel('时间')
plt.ylabel('温度')
plt.title('中午11:00-12:00温度变化图示')

plt.legend(loc='best')
<matplotlib.legend.Legend at 0x114359490>

在这里插入图片描述

  • 图形风格设置
    • color
      • r 红色
      • g 绿色
      • b 蓝色
      • w 白色
      • c 青色
      • m 洋红
      • y 黄色
      • k 黑色
    • 风格字符
      • - 实线
      • -- 虚线
      • -. 点划线
      • : 点虚线
      • ‘’ 留空、空格

10 多个坐标系显示

import matplotlib.pyplot as plt
import random

# 1.构造数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 14) for i in x]

# 2.创建画布
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)

# 3.图像绘制
axes[0].plot(x, y_shanghai, label='上海')
axes[1].plot(x, y_beijing, label='北京', linestyle='--', color='r')

# 4.添加x,y刻度
x_ticks_label = ['11点{}分'.format(i) for i in x]
y_ticks = range(40)
y_ticks_label = ['{}^C'.format(i) for i in y_ticks]
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_label[::5])
axes[0].set_yticklabels(y_ticks_label[::5])
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_label[::5])
axes[1].set_yticklabels(y_ticks_label[::5])

# 5.添加网格
axes[0].grid(True, linestyle='--', alpha=0.5)
axes[1].grid(True, linestyle='--', alpha=0.5)

# 6.添加x,y轴描述
axes[0].set_xlabel('时间')
axes[0].set_ylabel('温度')
axes[0].set_title('上海温度变化图')
axes[1].set_xlabel('时间')
axes[1].set_ylabel('温度')
axes[1].set_title('北京温度变化图')

# 7.显示
axes[0].legend(loc=0)
axes[1].legend(loc=0)
plt.show()

在这里插入图片描述

11 折线图的应用场景

import numpy as np
import matplotlib.pyplot as plt

# 1.准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

# 2.创建画布
plt.plot(x, y)

# 3.添加网格
plt.grid()

# 4.显示图像
plt.show()

在这里插入图片描述

12 散点图绘制

import matplotlib.pyplot as plt

# 1.准备数据
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.74, 202.41, 247.61,  24.9 , 239.34,
       140.32, 104.15, 176.84, 288.23, 128.79,  49.64, 191.74,  33.1 ,
        30.74, 400.02, 205.35, 330.64, 283.45]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3.绘制散点图
plt.scatter(x, y)

plt.show()

在这里插入图片描述

13 柱状图绘制

import matplotlib.pyplot as plt

movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
x = range(len(movie_name))
y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]

plt.figure(figsize=(20, 8), dpi=100)

# 绘制柱状图
plt.bar(x, y, width=0.5, color=['r','g','b','y'])

plt.xticks(x, movie_name)

plt.grid(linestyle='--', alpha=0.5)

plt.title('电影票房收入对比')

plt.show()

在这里插入图片描述

14 饼图

import matplotlib.pyplot as plt
# 1. 销售部: 50
# 2. 电商: 60
# 3. 推荐: 20

# 1. 准备数据
deparment_names = ['电商', '销售部', '推荐']
x = [60, 50, 20]

# 2. 创建画布
plt.figure(figsize=(4, 4), dpi=100)

# 3. 绘图(饼状图)
plt.pie(x, labels=deparment_names, autopct='%.2f%%', colors=['r','g','b'], explode=[0,0.2,0.1])


# 4. 展示
plt.show()

在这里插入图片描述

15 直方图

import matplotlib.pyplot as plt
import random

x1 = [random.randint(0, 100) for i in range(1000)]
plt.figure()
plt.hist(x=x1, bins=100)
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值