mpl基础

matplotlib 图表的常用设置

目录

  1. 基本绘图函数
  2. 设置画布
  3. 设置坐标轴
  4. 添加文本标签
  5. 设置标题和图例
  6. 添加注释
  7. 调整图表与画布边缘间距
  8. 其他设置

准备工作

1
2
3
4
5
# 导入绘图库
import matplotlib.pyplot as plt

# 设置中文字体
plt.rcParams['font.sans-serif']=['SimHei']

基本绘图函数

matplotlib.pyplot.plot(x, y, color='k', linestyle='-', marker=None, **kwargs)

x,y:为自变量和参数的数值集合或者表达式,或者所对应的series等。

color:线条颜色;

linestyle:线条样式;

marker:标记样式。

常用线条颜色

设置值 说明 设置值 说明
b 蓝色 m 洋红色
g 绿色 y 黄色
r 红色 k 黑色
c 蓝绿色 w 白色
#FFFF00 黄色,十六进制颜色值 0.5 灰度值字符串

常用线条样式

设置值 说明
‘-‘ 实线
‘–’ 双画线
‘-.’ 点画线
‘:’ 虚线

常用标记样式

标记 说明 标记 说明
‘.’ 点标记 ‘+’ 加号标记
‘,’ 像素标记 ‘*’ 星形标记
‘o’ 实心圆标记 ‘x’ 叉号标记
‘s’ 实心正方形标记 ‘p’ 实心五角星标记
1
2
3
4
5
6
7
8
9
10
11
12
x = ['1','2','3']
y = ['2','3','3']
plt.plot(x,y)

x_1 = range(1, 10, 1)
y_1 = range(1, 10, 1)
plt.plot(x_1, y_1)

x = range(1, 15, 1)
y = x

plt.plot(x,y, color='c', linestyle='-.', marker='*')

设置画布

plt.figure(num=None, figsize-None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

参数 解析
num=None 图像编号或名称,数字为编号,字符串为名称
figsize-None 指定画布的宽和高,单位为英尺,如figsize=(5,3)
dpi=None 绘图对象分辨率默认值为80
facecolor=None 背景颜色
edgecolor=None 边框颜色
frameon=True 是否显示边框,布尔值
1
2
3
4
5
6
7
8
x = [1,2,3,4,5,6]
y = [1,5,3,6,9,2]

# 设置画布标题与大小
plt.figure(num='testing', figsize=(6, 4))

# 绘图函数
plt.plot(x, y, marker='s')

设置坐标轴

函数 含义
plt.xlabel(‘x_label’) 添加x轴标签
plt.ylabel(‘y_label’) 添加y轴标签
plt.xticks(locs, [labels], **kwargs) loc参数确定x轴刻度范围,[labels]确定各个刻度的标签
plt.yticks(locs, [labels], **kwargs) loc参数确定y轴刻度范围,[labels]确定各个刻度的标签
plt.xlim(a,b) 确定x轴取值范围,从a到b
plt.ylim(c,d) 确定y轴取值范围,从c到d
plt.grid() 显示网格线
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
x = [1, 2, 3, 4, 5, 6]
y = [1, 5, 3, 6, 9, 2]

# 设置画布标题与大小
plt.figure(num='testing', figsize=(6, 4))

# 绘图函数
plt.plot(x, y, marker='s')

# 设置坐标轴标签
plt.xlabel('时间')
plt.ylabel('销量')

# 设置坐标轴刻度
date = ['一月', '二月', '三月', '四月', '五月', '六月']
plt.xticks(range(1, 7, 1), date)

plt.show()

添加文本标签

plt.text(x,y,s,fontdict=None, withdash=False, **kwargs)

参数 解析
x,y 需要添加文本标签的位置
fontdict=None 字典,可选参数,不用管
withdash=False 默认创建TexWithDash实例
**kwargs 可选参数,如下面几个
fontsize=12 文本标签字体大小
horizontalalignment=’center’(ha=’center’) 垂直对齐方式
verticalalihnment=’center(va=’center’) 水平对齐方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
x = [1, 2, 3, 4, 5, 6]
y = [1, 5, 3, 6, 9, 2]

# 设置画布标题与大小
plt.figure(num='testing', figsize=(6, 4))

# 绘图函数
plt.plot(x, y, marker='s')

# 设置坐标轴标签
plt.xlabel('时间')
plt.ylabel('销量')

# 设置坐标轴刻度
date = ['一月', '二月', '三月', '四月', '五月', '六月']
plt.xticks(range(1, 7, 1), date)

# 添加文本标签
for a,b in zip(x,y):
plt.text(a,b+0.5, '%.1f'%b, ha='center', va='center', fontsize=8)

# 显示图像
plt.show()

设置标题和图例

  1. 设置标题

plt.title(label, fontdict=None, loc='center', pad=None, **kwargs)

一般而言,其他参数不用管,输入标题标签后,最多加上一个参数fontsize

  1. 设置图例

(1) 自动显示图例

plt.legend()

(2) 手动显示图例

plt.legend(('str1',), loc='upper right', fontsize=10)

图例位置参数设置值
| 位置 | 描述 | 位置 | 描述 |
| :———: | :—-: | :———-: | :———-: |
| best | 自适应 | center left | 左侧中间位置 |
| upper right | 右上方 | center right | 右侧中间位置 |
| upper left | 左上方 | upper center | 中上方 |
| lower left | 左下方 | lower center | 中下方 |
| lower right | 右下方 | center | 正中央 |
| right | 右侧 |

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
x = [1, 2, 3, 4, 5, 6]
y = [1, 5, 3, 6, 9, 2]

# 设置画布标题与大小
plt.figure(num='testing', figsize=(6, 4))

# 绘图函数
plt.plot(x, y, marker='s')

# 设置坐标轴标签
plt.xlabel('时间')
plt.ylabel('销量')

# 设置坐标轴刻度
date = ['一月', '二月', '三月', '四月', '五月', '六月']
plt.xticks(range(1, 7, 1), date)

# 添加文本标签
for a,b in zip(x,y):
plt.text(a,b+0.5, '%.1f'%b, ha='center', va='center', fontsize=8)

# 添加标题
plt.title('六日销量图')

# 添加图例
plt.legend(('销量',), loc='upper left', fontsize=8)

# 显示图像
plt.show()

添加注释

1
2
3
plt.annotate('tip', xy=(x_value,y_value), xytext=(xt_value,yt_value),
xycoords='data',
arrowprops=dict(facecolot='r',shrink=0.05))

‘tip’ : 注释内容

xy=(x_value,y_value) :被注释的坐标的

xytext=(xt_value,yt_value) : 注释文本的坐标点

arrowprops=dict(facecolot=’r’,shrink=0.05) : 箭头设置

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
x = [1, 2, 3, 4, 5, 6]
y = [1, 5, 3, 6, 9, 2]

# 设置画布标题与大小
plt.figure(num='testing', figsize=(6, 4))

# 绘图函数
plt.plot(x, y, marker='s')

# 设置坐标轴标签
plt.xlabel('时间')
plt.ylabel('销量')

# 设置坐标轴刻度
date = ['一月', '二月', '三月', '四月', '五月', '六月']
plt.xticks(range(1, 7, 1), date)

# 添加文本标签
for a, b in zip(x, y):
plt.text(a, b+0.5, '%.1f' % b, ha='center', va='center', fontsize=8)

# 添加标题
plt.title('六日销量图')

# 添加图例
plt.legend(('销量',), loc='upper left', fontsize=8)


# 添加注释
plt.annotate('tip', xy=(3, 3), xytext=(4, 3),
xycoords='data',
arrowprops=dict(facecolor='r', shrink=0.05))

# 显示图像
plt.show()

调整图表与画布边缘间距

plt.subplots_adjust(left=None, bottom=None, right=None,top=None, wspace=None, hspace=None)

left=None, bottom=None, right=None,top=None, 调整上下左右的空白;

wspace: 调整列间距

hspace:调整行间距

其他设置

  1. 坐标轴的刻度线

(1)设置是否显示四个方向的坐标轴

plt.tick_params(bottom=False, left=True, right=True, top=True)

(2)设置刻度线显示方向

plt.rcParams['xtick.direction'] = 'in'

plt.rcParams['ytick.direction'] = 'in'

in表示刻度线向内显示,默认为向外显示(out),inout 表示在中间

  1. 坐标轴相关属性
属性 含义
axis() 返回当前axes的范围
axis(v) 通过输入v=[xmin,xmax,ymin,ymax],设置x,y轴的取值范围
axis(‘off’) 关闭坐标轴轴线及坐标轴标签
axis(‘equal’) 使xy轴长度一致
axis(‘scaled’) 调整图框的尺寸,使x,y轴长度一致,而不改变坐标轴取值范围
axis(‘tight’) 改变x轴和y轴的限制,使所有数据被展示
axis(‘image’) 缩放axis范围
axis(‘auto’) 自动缩放
axis(‘normal’) 不推荐使用,恢复默认状态

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!