Matplotlib

Matplotlib是Python的一个绘图库。使用它可以方便的绘制各种图形,如直方图,散点图,3D图等。常用于数据可视化。

简介

时间轴

安装

Matplotlib官方在pypi上发行,所以可使用pip安装:

pip install -U matplotlib

一些科学计算包,已经包含了Matplotlib,可以不安装。如AnacondaCanopyActiveState等科学计算包。

了解更多 >> matplotlib:安装指南 PyPi:matplotlib matplotlib:从源代码安装


图表

绘图函数基本都在pyplot模块中,一般导入方式:import matplotlib.pyplot as plt,下面的plt指的是matplotlib.pyplot模块。

基本图表

名称 函数 描述
折线图 plt.plot()
散点图 plt.scatter()
条形图 plt.bar()
直方图 plt.hist()
饼图 plt.pie() 常用参数:
x 数据,一维结构。
labels 标签,列表类型。为每个扇形添加标签。
autopct 数值格式,字符串或函数类型。如'%1.1f%%'设置1位小数。
explode 突出显示,列表类型。将某些扇形偏移出来。
textprops 字体样式,字典类型。设置字体,字体大小等。
wedgeprops 扇形样式,字典。设置扇形间隔的宽度、颜色等。
import matplotlib.pyplot as plt

labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
explode = 

plt.pie(sizes, 
        explode=[0, 0.1, 0, 0],  #第二个
        labels=labels, 
        autopct='%1.1f%%')

颜色

设置

matplotlib显示中文

matplotlib有时中文不能显示,是由于matplotlib可用字体中没有中文字体,或matplotlib字体设置中没有中文字体,所以显示乱码。

matplotlib中matplotlib.font_manager模块用于跨平台查找、管理和使用字体的模块。可以先看matplotlib可用字体:

from matplotlib import font_manager

print({f.name for f in font_manager.fontManager.ttflist})

matplotlib使用的字体主要来自3个地方,操作系统自带、matplotlib自带和指定字体。

# 操作系统中有哪些中文字体
fc-list :lang=zh      # Linux系统,终端执行命令
C:\Windows\Fonts      # Windows系统浏览文件夹,有显示中文的字体

# 查看操作系统中有哪些字体
import matplotlib.pyplot as plt
matplotlib.font_manager.findSystemFonts()

# matplotlib自带字体
import matplotlib

matplotlib.get_data_path()  # 数据目录下的font目录

方法一,在文件开始处,使用rcParams动态修改matplotlibrc设置。

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['Source Han Sans SC']
#plt.rcParams['font.serif'] = ['Source Han Serif SC']

#import matplotlib 
#matplotlib.rcParams[‘font.sans-serif] = [‘SimHei’]

plt.plot((-1,2,3),(-2,-1,5))
plt.title("标题-简体中文")
plt.ylabel("x轴")
plt.xlabel("y轴") 
plt.show()


方法二,在使用处设置,在需要用到中文字体的地方,都加上一个字体属性。

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

font_sh = FontProperties(family='SimHei', size=24, weight='bold')   # 选择一个matplotlib可用字体
font_notos = FontProperties(fname='/path/to/NotoSansCJKsc-Regular.otf', size=16)   # 指定一个字体,可以是相对路径或绝对路径

plt.plot([0, 1], [-1, 2])
plt.title('中文标题', fontproperties=font_sh )
plt.ylabel("x轴", fontproperties=font_notos )
plt.xlabel("y轴", fontproperties='SimHei') 
plt.show()


方法三、修改配置文件matplotlibrc

import matplotlib

# 查看matplotlibrc位置
print( matplotlib.matplotlib_fname() )

选择一个matplotlib可用字体,或将字体文件放入matplotlib自带字体文件夹。修改matplotlibrc文件中的 font.family 和 font.sans-serif。

  • font.family设置字体家族,可选serif(衬线字体)、sans-serif(无衬线字体)、monospace、cursive和fantasy。
  • font.sans-serif设置无衬线字体的默认顺序。
font.family:  sans-serif
font.sans-serif:  Source Han Sans SC, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

设置之后,重启没有生效,因为matplotlib缓存目录下fontlist-v330.json文件缓存了可用字体,需要删除该缓存文件。

import matplotlib

matplotlib.get_cachedir()     # 查看缓存目录
# matplotlib.get_configdir()  # 查看配置目录

了解更多 >> matplotlib API:matplotlib.font_manager


annotate()文本注释

资源

官网

教程

书籍

  • 《Python编程从入门到实践》

相关文章