基本函数

函数plot()

函数scatter()

函数xlim()

设置x轴的数值显示范围

函数xlabel()

设置x轴的标签文本

函数grid()

绘制刻度线的网格线

函数axhline()

绘制平行于x轴的水平参考线

函数axvspan()

绘制垂直于x轴的参考区域

函数annotate()

添加图形内容细节的指向型注释文本

函数text()

添加图形内容细节的无指向型注释文本

函数title()

添加图形内容的标题

函数legend()

标示不同图形的文本标签图例

统计函数

函数bar()

用于绘制柱状图

函数barh()

用于绘制条形图

函数hist()

用于绘制直方图

函数pie

用于绘制饼图

函数polar()

用于绘制极线图

函数scatter()

用于绘制气泡图

函数stem()

用于绘制棉棒图

函数boxplot()

用于绘制箱线图

函数errorbar()

用于绘制误差棒图

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.1,0.6,10)
y=np.exp(x)
error=0.05+0.15*x
lower_error=error
upper_error=0.3*error
error_limit=[lower_error,upper_error]
plt.errorbar(x,y,yerr=error_limit,fmt=":o",
             ecolor="y",elinewidth=4,
            ms=5,mfc="c",mec='r',
            capthick=1,capsize=2)
plt.xlim(0,0.7)
plt.show()

函数stackplot()

绘制堆积折线图

函数broken_barh()

绘制间断条形图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"]=["LiSu"]
mpl.rcParams["axes.unicode_minus"]=False
plt.broken_barh([(30,100),(180,50),(260,70)],(20,8),facecolors="#1f78b4")
plt.broken_barh([(60,90),(190,20),(230,30),(280,60)],(10,8),facecolors=("#7fc97f","#beaed4","#fdc086","#ffff99"))
plt.xlim(0,360)
plt.ylim(5,35)
plt.xlabel("演出时间")
plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],["歌剧院A","歌剧院B"])
plt.grid(ls='-',lw=1,color="gray")
plt.title("不同地区的歌剧院的演出时间比较")
plt.show()

函数step()

绘制阶梯图

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(1,10,10)
y=np.sin(x)
plt.step(x,y,color="#8dd3c7",where="pre",lw=2)
plt.xlim(0,11)
plt.xticks(np.arange(1,11,1))
plt.ylim(-1.2,1.2)
plt.show()

完善统计图形

添加图例和标题

legend()
title()

调整刻度范围和刻度标签

xlim()
xticks()

向统计图形添加表格

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
labels = "A难度水平","B难度水平","C难度水平","D难度水平"
students = [0.35,0.15,0.20,0.30]
explode = (0.1,0.1,0.1,0.1)
colors = ["#377eb8","#e41a1c","#4daf4a","#984ea3"]
plt.pie(students,
        explode=explode,
        labels=labels,
        autopct="%1.1f%%",
        startangle=45,
        shadow=True,
        colors=colors)
plt.title("选择不同难度测试试卷的学生百分比")
colLabels = ["A难度水平","B难度水平","C难度水平","D难度水平"]
rowLabels = ["学生选择试卷人数"]
studentValues = [[350,150,200,300]]
colColors = ["#377eb8","#e41a1c","#4daf4a","#984ea3"]
plt.table(cellText=studentValues,
          cellLoc="center",
          colWidths=[0.1]*4,
          colLabels=colLabels,
          colColours=colColors,
          rowLabels=rowLabels,
          rowLoc="center",
          loc="bottom")
plt.show()

图形样式

设置坐标轴的刻度样式

定位器(locator)——设置刻度线的位置
刻度格式器(formatter)——设置刻度标签的显示样式

添加有指示注解和无指示注解

annotate()——有指示注解
text()——无指示注解

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.0,10,40)
y = np.random.randn(40)
plt.plot(x,y,ls="-",lw=2,marker="o",ms=20,mfc="orange",alpha=0.6)
plt.grid(ls=":",color="gray",alpha=0.5)
plt.text(6,0, "Matplotlib", size=30,rotation=30.,bbox=dict(boxstyle="round",ec="#8968CD",fc="#FFE1FF"))
plt.show()
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.sankey import Sankey
mpl.rcParams["font.sans-serif"]=["FangSong"]
mpl.rcParams["axes.unicode_minus"]=False
flows=[0.2,0.1,0.4,0.3,-0.6,-0.05,-0.15,-0.2]
labels=["","","","","family","trip","education","sport"]
orientations=[1,1,0,-1,1,-1,1,0]
sankey = Sankey()
sankey.add(flows=flows,labels=labels,orientations=orientations,color="c",fc="lightgreen",patchlabel="Life Cost",alpha=0.7)
diagrams = sankey.finish()
diagrams[0].texts[4].set_color("r")
diagrams[0].texts[4].set_weight("bold")
diagrams[0].text.set_fontsize(20)
diagrams[0].text.set_fontweight("bold")
plt.title("日常生活中成本开支的流量图")
plt.show()

实现标题和坐标轴标签的投影效果

划分画布

函数subplot()

绘制网格区域中的几何形状相同的子区布局

函数subplot2grid()

让子区跨越固定的网格布局

函数suptitle()

绘制Figure画布标题的文本内容

函数subplots()

创建一张画布带有多个子区的绘图模式

函数subplots_adjust()

改变子区边缘相距画布边缘的距离和子区边缘之间的高度与宽度的距离

共享绘图区域的坐标轴

共享单一绘图区域的坐标轴

共享不同子区绘图区域的坐标轴

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.0, 10.0, 200)
y = np.cos(x)*np.sin(x)
y2 = np.exp(-x)*np.sin(x)
y3 = 3*np.sin(x)
y4 = np.power(x,0.5)
fig,(ax1,ax2,ax3,ax4) =plt.subplots(4,1,sharex="all")
fig.subplots_adjust(hspace=0)
ax1.plot(x,y,ls="-",lw=2)
ax1.set_yticks(np.arange(-0.6,0.7,0.2))
ax1.set_ylim(-0.7, 0.7)
ax2.plot(x,y2,ls="-",lw=2)
ax2.set_yticks(np.arange(-0.05,0.36,0.1))
ax2.set_ylim(-0.1, 0.4)
ax3.plot(x,y3,ls="-",lw=2)
ax3.set_yticks(np.arange(-3,4,1))
ax3.set_ylim(-3.5,3.5)
ax4.plot(x,y4,ls="-",lw=2)
ax4.set_yticks(np.arange(0.0,3.6,0.5))
ax4.set_ylim(0.0,4.0)
plt.show()

共享个别子区绘图区域的坐标轴

函数autoscale()

调整坐标轴范围

设置坐标轴的位置和展示形式

函数axis()

绘制坐标轴

使用两种方法控制坐标轴刻度的显示

调用matplotlib的面向对象的方法
使用模块pyplot的API方法

  • 使用Axes.set_xticks()和Axes.set_yticks(),不画坐标轴刻度
  • 使用函数step()设置刻度元素(ticklabel和tickline)

控制坐标轴的显示

控制轴脊和刻度线的显示

移动坐标轴的位置

移动坐标轴的载体(轴脊)的位置,进而设置刻度线的位置,从而完成移动坐标轴的位置的任务。

不同调用签名形式的字典使用方法

线条类型的显示样式设置方法

标记类型的显示样式设置方法

修改代码层面的matplotlib的配置

通过代码进行修改——局部调整

修改项目层面的matplotlib配置

通过修改配置文件matplotlibrc——全局修改

配置文件所在路径

设置字体属性和文本属性

改变配置文件matplotlibrc的字体属性值和文本属性值
通过属性字典rcParams调整字体属性值和文本属性值
通过设置函数的关键字参数

使用颜色参数和颜色映射表

函数pcolor()

生成二维的模拟颜色图

import matplotlib.pyplot as plt
import numpy as np
rd = np.random.rand(10,10)
#print(rd)
plt.pcolor(rd,cmap="BuPu")
plt.colorbar()
plt.show()
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
barSlices = 12
theta = np.linspace(0.0, 2*np.pi,barSlices,endpoint=False)
radii = 30*np.random.rand(barSlices)
width = np.pi/4*np.random.rand(barSlices)
fig = plt.figure()
ax = fig.add_subplot(111,polar=True)
bars = ax.bar(theta,radii,width=width,bottom=0.0)
for r,bar in zip(radii,bars):
    bar.set_facecolor(mpl.cm.Accent(r / 30.))
    bar.set_alpha(r/30.)
plt.show()

运行命令行展示输出图形

保存输出图形

plt.savefig("D:\\FIGURE_DEMO.png")