常用配置: matplotlib.rcParams
本文首发于本csdn博主私人博客:Timing is Fun
- A dictionary object including validation
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
mpl.rcParams['font.sans-serifs'] = ['SimHei'] # 中文支持
mpl.rcParams['axes.unicode_minus'] = False # 正常显示负号
mpl.rcParams['lines.linewidth'] = 5 # 线条宽度
mpl.rcParams['lines.color'] = 'red' # 线条颜色
mpl.rcParams['lines.linestyle'] = '-' # 线条样式
基本图形
直方图
- 查看分布规律
- plt.hist(x, bins)
- x - array or sequence of (n,) arrays,数据data
- bins - int or sequence or str, optional, 横坐标区间
import matplotlib.pyplot as plt
%matplotlib inline
height = [168, 155, 182, 170, 173, 161]
bins = range(150,191,5)
plt.hist(height, bins=bins)
plt.show()
条形图
- 同类数据进行对比
- plt.bar(x, y)
import matplotlib.pyplot as plt
%matplotlib inline
classes = ['class 1', 'class 2', 'class 3']
scores = [70, 80, 60]
plt.bar(classes, scores)
plt.show()
折线图
- 展示数据随某一指标变化的关系
- plt.plot(x, y)
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(year, height)
plt.title('身高随时间变化图')
plt.show()
饼图
- 展示不同类在整体中所占的比重
- plt.pie(data, labels, autopct)
- data - array or sequence of (n,) arrays,数据data
- labels - list, optional, default: None,数据标签
- autopct - None (default), string, or function, optional,占比计算
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
labels = ['衣', '食', '住', '行' ]
data &#