4.11Python数据处理篇之Matplotlib系列(十一)
2019-03-12 本文已影响0人
张一根
目录
[TOC]
前言
本章节将讲一下三个内容,图例,网格,背景
(一)图例legend
1.默认不带参数的图例
(1)说明:
默认情况下,是自适应的放在图例的位置,需要在绘图的时候,添加Label标识。
在使用plt.legend()显示图例
(2)源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y1 = x
y2 = x**2
y3 = x**(1/2)
# 绘图
plt.plot(x, y1, "--", label="y1")
plt.plot(x, y2, "o-", label="y2")
plt.plot(x, y3, "-.", label="y3")
# 展示图例
plt.legend()
# 展示
plt.show()
(3)展示效果

2.添加参数的图例
(1)可选参数
参数 | 说明 | 默认值 |
---|---|---|
loc=int\string | 图例所在的位置 | 0 |
fontsize=int\string | 图例的文字大小 | 当前文字的大小 |
facecolor='b' | 图例的背景色 | 'w' |
edgecolor='b' | 图例的边框颜色 | 'w' |
title=string | 图例的标题 | None |
shadow=bool | 是否显示图例的阴影 | False |
fancybox=bool | 是否显示圆角 | False |
framealpha=float | 图例的透明度 | None |
- 位置的参数值:可以使用字符串与数字

- 文字的大小值:'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large',或者直接用数字表示大小。
- 透明度的范围:0.0~1.0,1.0是不透明,0.0是全透明,从0.0到1.0越来越不透明。
(2)源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y1 = x
y2 = x**2
y3 = x**(1/2)
# 绘图
plt.plot(x, y1, "--", label="y1")
plt.plot(x, y2, "o-", label="y2")
plt.plot(x, y3, "-.", label="y3")
# 展示图例
plt.legend(loc='upper right',
fancybox=True,
title="l am legend",
edgecolor='r',
shadow=True,
fontsize='x-large',
facecolor='b',
framealpha=0.3)
# 展示
plt.show()
(3)输出效果:

3.将图例移动到框外
(1)说明:
bbox_to_anchor=(x, y) 可以移动到任意位置
x,y是float:0.0~1.0,表示是x,y轴的比例,x:左->右增大, y:下->上 增大
并且结合loc来确定位置。
(2)源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y1 = x
y2 = x**2
y3 = x**(1/2)
# 绘图
plt.plot(x, y1, "--", label="y1")
plt.plot(x, y2, "o-", label="y2")
plt.plot(x, y3, "-.", label="y3")
# 展示图例
plt.legend(bbox_to_anchor=(1, 0.8), loc=3)
# 展示
plt.show()
(3)输出效果:

(二)网格grid
1.说明
语法:plt.grid([可选参数])
可选参数 | 说明 | 默认值 |
---|---|---|
b=bool | 是否显示网格 | Fales |
color='r' | 网线的颜色 | 'w' |
linestyle='--' | 网格的线形 | ‘-’ |
linewidth=1 | 网格的线宽 | 1 |
alpha=0.3 | 网线的透明度0.0~1.0 | 1 |
axis={"both",'x',"y"} | 表示在哪些方向轴上画网线,x轴,y轴 | "both" |
which={"both","major","minor"} | 表示在哪些刻度上画网线,主刻度,还是小刻度 | ”both" |
2.源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y = x**2
# 绘图
plt.plot(x, y)
# 设置网格线
plt.grid(b=True,
color='r',
linestyle='--',
linewidth=1,
alpha=0.3,
axis='x',
which="major")
# 展示
plt.show()
3.输出效果

(三)背景axses
1.设置全局的背景色:
(1)说明:
- 设置figure窗体的颜色:plt.rcParams['figure.facecolor'] = 'color'
- 设置axes绘图区的颜色:plt.rcParams['axes.facecolor'] = 'color'
(2)源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y = x**2
# =====设置背景====
# 设置figure窗体的颜色
plt.rcParams['figure.facecolor'] = 'b'
# 设置axes绘图区的颜色
plt.rcParams['axes.facecolor'] = 'g'
# 绘图
plt.plot(x, y)
# 展示
plt.show()
(3)输出效果:

2.设置局部的背景色
(1)说明:
获取ax对象:ax = plt.gca()
设置ax区域背景颜色:ax.patch.set_facecolor("gray")
设置ax区域背景颜色透明度:ax.patch.set_alpha(0.5)
(2)源代码:
# 导入模块
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.linspace(-5, 5, 50)
y = x**2
# 绘图
plt.plot(x, y)
# =====设置背景====
# 得到ax对象
ax = plt.gca()
# 设置ax区域背景颜色
ax.patch.set_facecolor("gray")
# 设置ax区域背景颜色透明度
ax.patch.set_alpha(0.5)
# 展示
plt.show()
(3)输出效果:
