2022年 11月 7日

Python画图库Matplotlib教程(一)

基础教程

  • Matplotlib基础使用
  • 点图
  • 坐标轴
  • 图例
  • Annotation标注
  • tick可见度

首先定义头文件

import matplotlib.pyplot as plt
import numpy as np
  • 1
  • 2

Matplotlib基础使用

#从-1 到 1 生成 50 个点
x = np.linspace(-1, 1, 50)
y1 = x ** 2
y2 = x ** 3

# 生成画布
plt.figure()
#画出两条线
plt.plot(x, y1)
plt.plot(x, y2, color='red', linewidth=2.0, linestyle='--')
#显示画布
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

点图

n = 3
x = np.linspace(-1, 1, n)
y = np.linspace(-1, 1, n)

plt.plot(x, y,
         color='red',  # 设置颜色为limegreen
         marker='.',  # 设置点类型为圆点
         linestyle='')  # 设置线型为空,也即没有线连接点

plt.grid(True)      #显示网格
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

坐标轴

x = np.linspace(-1, 10, 50)
y1 = x ** 2
y2 = x ** 3

plt.figure()
plt.plot(x, y1)
plt.plot(x, y2, color='red', linewidth=2.0, linestyle='--')

# 设置X、Y范围
plt.xlim((-1, 2))
plt.ylim((-2, 3))

# lable说明
plt.xlabel('x-axis')
plt.ylabel('y-axis')

# 从 -1 到 2 生成 5 个点
new_ticks = np.linspace(-1, 2, 5)

plt.xticks(new_ticks)
plt.yticks([-2, -1.5, -1, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])


# 获取当前图的轴
ax = plt.gca()

# 隐藏右边和上面的轴
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 设置X、Y轴为左边和下面的轴
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

# 设置X、Y轴的原点
ax.spines['bottom'].set_position(('data', 0)) # outward,axes
ax.spines['left'].set_position(('data', 0))

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述

图例

x = np.linspace(-1, 10, 50)
y1 = x ** 2
y2 = x + 1

plt.figure()
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xlabel('x-axis')
plt.ylabel('y-axis')
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2, -1.5, -1, 1.22, 3], [r'$really\ bad$', r'$\alpha$', 'normal', 'good', 'really good'])

# 设置label
l1, = plt.plot(x, y1, label='blue')
l2, = plt.plot(x, y2, color='red', linewidth=2.0, linestyle='--', label='red')
# 添加图例
# plt.legend()
plt.legend(handles=[l1,l2], labels=['aa', 'bb'], loc='best')

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

Annotation标注

x = np.linspace(-10, 10, 50)
y = 2*x + 1

plt.figure()
plt.xlim((-3, 3))
plt.ylim((-6, 8))
plt.plot(x, y)

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0)) # outward,axes
ax.spines['left'].set_position(('data', 0))

x0 = 1
y0 = 2*x0 + 1
# 画点
plt.scatter(x0, y0, s=50, color='b')

# 画线
plt.plot([x0,x0], [y0,0], 'k--', lw=2.5)

# 箭头注释
# xy=(x0, y0)为箭头指向位置,xytext为注释偏移位置,connectionstyle为箭头的弧度角度
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
             textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle='<->', connectionstyle="arc3,rad=.2"))

# 文本注释
# -3,6为注释所在坐标
plt.text(-3, 6, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述

tick可见度

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 3, 50)
y = 0.1*x

plt.figure()
plt.plot(x, y, linewidth=2)
plt.ylim(-2, 2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 设置XY轴的刻度
for label in ax.get_xticklabels() + ax.get_yticklabels():
    # 字号大小
    label.set_fontsize(12)
    # 背景 facecolor:轴上端点的背景色 edgecolor :边框 alpha:透明度
    label.set_bbox(dict(facecolor='blue', edgecolor='black', alpha=0.5))

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

在这里插入图片描述