目录:
缺省样式
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
#生成曲线上各点的水平坐标
x=np.linspace(-np.pi,np.pi,1000)
cos_y=np.cos(x)/2
sin_y=np.sin(x)
h_y=x/2
#用直线链接曲线上的各点
mp.plot(x,cos_y)
mp.plot(x,sin_y)
mp.plot(x,h_y)
#显示图形
mp.show()
设置线型、线宽和颜色
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 生成曲线上各点的水平坐标
x = np.linspace(-np.pi, np.pi, 1000)
# 根据曲线函数计算其上各点的垂直坐标
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
# 用直线连接曲线上各点
mp.plot(x, cos_y, linestyle='-', linewidth=1,
color='dodgerblue')
mp.plot(x, sin_y, linestyle='-', linewidth=1,
color='orangered')
# 显示图形
mp.show()
设置坐标范围
代码:
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
# plotting 测绘 library
import numpy as np
#生成曲线上各点的水平坐标
x=np.linspace(-np.pi,np.pi,2000)
#根据曲线函数计算其上各点的垂直坐标
cos_y=np.cos(x)/2
sin_y=np.sin(x)
#设置坐标范围
mp.xlim(x.min()*1.1,x.max()*1.1)
mp.ylim(min(cos_y.min(),sin_y.min())*1.1,
max(cos_y.max(),sin_y.max())*1.1)
#用直线连接曲线上各点
mp.plot(x,cos_y,linestyle='-',linewidth=1,
color='dodgerblue')
mp.plot(x,sin_y,linestyle='-',linewidth=1,
color='orangered')
#显示图形
mp.show()
设置坐标轴刻度标签
代码:
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
# plotting 测绘 library
import numpy as np
#生成曲线上各点的水平坐标
x=np.linspace(-np.pi,np.pi,2000)
#根据曲线函数计算其上各点的垂直坐标
cos_y=np.cos(x)/2
sin_y=np.sin(x)
#设置坐标范围
mp.xlim(x.min()*1.1,x.max()*1.1)
mp.ylim(min(cos_y.min(),sin_y.min())*1.1,
max(cos_y.max(),sin_y.max())*1.1)
mp.xticks([-np.pi,-np.pi/2,np.pi/2,np.pi*3/4,np.pi],
[r'$-\pi$',r'$-\frac{\pi}{2}$',r'$0$',
r'$\frac{\pi}{2}$',r'$\frac{3\pi}{4}$',r'$\pi$'])
mp.yticks([-1,-0.5,0.5,1])
#用直线连接曲线上各点
mp.plot(x,cos_y,linestyle='-',linewidth=1,
color='dodgerblue')
mp.plot(x,sin_y,linestyle='-',linewidth=1,
color='orangered')
#显示图形
mp.show()
将矩形坐标轴改成十字坐标轴
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 生成曲线上各点的水平坐标
x = np.linspace(-np.pi, np.pi, 1000)
# 根据曲线函数计算其上各点的垂直坐标
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
# 设置坐标范围
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(min(cos_y.min(), sin_y.min()) * 1.1,
max(cos_y.max(), sin_y.max()) * 1.1)
# 设置坐标轴刻度标签
mp.xticks([
-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4, np.pi], [
r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$', r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
# 将矩形坐标轴改成十字坐标轴
# 获取当前坐标轴对象
ax = mp.gca()
# 将垂直坐标刻度置于左边框
ax.yaxis.set_ticks_position('left')
# 将左边框置于数据坐标原点
ax.spines['left'].set_position(('data', 0))
# 将水平坐标刻度置于底边框
ax.xaxis.set_ticks_position('bottom')
# 将底边框置于数据坐标原点
ax.spines['bottom'].set_position(('data', 0))
# 将右边框和顶边框设置成无色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 用直线连接曲线上各点
mp.plot(x, cos_y, linestyle='-', linewidth=1,
color='dodgerblue')
mp.plot(x, sin_y, linestyle='-', linewidth=1,
color='orangered')
# 显示图形
mp.show()
显示图例
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 生成曲线上各点的水平坐标
x = np.linspace(-np.pi, np.pi, 1000)
# 根据曲线函数计算其上各点的垂直坐标
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
# 设置坐标范围
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(min(cos_y.min(), sin_y.min()) * 1.1,
max(cos_y.max(), sin_y.max()) * 1.1)
# 设置坐标轴刻度标签
mp.xticks([
-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4, np.pi], [
r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$', r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
# 将矩形坐标轴改成十字坐标轴
# 获取当前坐标轴对象
ax = mp.gca()
# 将垂直坐标刻度置于左边框
ax.yaxis.set_ticks_position('left')
# 将左边框置于数据坐标原点
ax.spines['left'].set_position(('data', 0))
# 将水平坐标刻度置于底边框
ax.xaxis.set_ticks_position('bottom')
# 将底边框置于数据坐标原点
ax.spines['bottom'].set_position(('data', 0))
# 将右边框和顶边框设置成无色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 用直线连接曲线上各点
mp.plot(x, cos_y, linestyle='-', linewidth=1,
color='dodgerblue', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle='-', linewidth=1,
color='orangered', label=r'$y=sin(x)$')
mp.legend(loc='upper left')
# 显示图形
mp.show()
添加特殊点
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 生成曲线上各点的水平坐标
x = np.linspace(-np.pi, np.pi, 1000)
# 根据曲线函数计算其上各点的垂直坐标
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
# 计算特殊点的坐标
xo = np.pi * 3 / 4
yo_cos = np.cos(xo) / 2
yo_sin = np.sin(xo)
# 设置坐标范围
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(min(cos_y.min(), sin_y.min()) * 1.1,
max(cos_y.max(), sin_y.max()) * 1.1)
# 设置坐标轴刻度标签
mp.xticks([
-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4, np.pi], [
r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$', r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
# 将矩形坐标轴改成十字坐标轴
# 获取当前坐标轴对象
ax = mp.gca()
# 将垂直坐标刻度置于左边框
ax.yaxis.set_ticks_position('left')
# 将左边框置于数据坐标原点
ax.spines['left'].set_position(('data', 0))
# 将水平坐标刻度置于底边框
ax.xaxis.set_ticks_position('bottom')
# 将底边框置于数据坐标原点
ax.spines['bottom'].set_position(('data', 0))
# 将右边框和顶边框设置成无色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 用直线连接曲线上各点
mp.plot(x, cos_y, linestyle='-', linewidth=1,
color='dodgerblue', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle='-', linewidth=1,
color='orangered', label=r'$y=sin(x)$')
# 绘制特殊点
mp.plot([xo, xo], [yo_cos, yo_sin], linestyle='--',
linewidth=1, color='limegreen')
mp.scatter([xo, xo], [yo_cos, yo_sin], s=60,
edgecolor='limegreen', facecolor='white',
zorder=3)
mp.legend(loc='upper left')
# 显示图形
mp.show()
添加注释
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 生成曲线上各点的水平坐标
x = np.linspace(-np.pi, np.pi, 1000)
# 根据曲线函数计算其上各点的垂直坐标
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
# 计算特殊点的坐标
xo = np.pi * 3 / 4
yo_cos = np.cos(xo) / 2
yo_sin = np.sin(xo)
# 设置坐标范围
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(min(cos_y.min(), sin_y.min()) * 1.1,
max(cos_y.max(), sin_y.max()) * 1.1)
# 设置坐标轴刻度标签
mp.xticks([
-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4, np.pi], [
r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$', r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
# 将矩形坐标轴改成十字坐标轴
# 获取当前坐标轴对象
ax = mp.gca()
# 将垂直坐标刻度置于左边框
ax.yaxis.set_ticks_position('left')
# 将左边框置于数据坐标原点
ax.spines['left'].set_position(('data', 0))
# 将水平坐标刻度置于底边框
ax.xaxis.set_ticks_position('bottom')
# 将底边框置于数据坐标原点
ax.spines['bottom'].set_position(('data', 0))
# 将右边框和顶边框设置成无色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 用直线连接曲线上各点
mp.plot(x, cos_y, linestyle='-', linewidth=1,
color='dodgerblue', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle='-', linewidth=1,
color='orangered', label=r'$y=sin(x)$')
# 绘制特殊点
mp.plot([xo, xo], [yo_cos, yo_sin], linestyle='--',
linewidth=1, color='limegreen')
mp.scatter([xo, xo], [yo_cos, yo_sin], s=60,
edgecolor='limegreen', facecolor='white',
zorder=3)
# 添加注释
mp.annotate(
r'$\frac{1}{2}cos(\frac{3\pi}{4})=-\frac{\sqrt{2}}{4}$',
xy=(xo, yo_cos), xycoords='data',
xytext=(-90, -40), textcoords='offset points',
fontsize=14, arrowprops=dict(
arrowstyle='->', connectionstyle='arc3, rad=0.2'))
mp.annotate(
r'$sin(\frac{3\pi}{4})=\frac{\sqrt{2}}{2}$',
xy=(xo, yo_sin), xycoords='data',
xytext=(20, 20), textcoords='offset points',
fontsize=14, arrowprops=dict(
arrowstyle='->', connectionstyle='arc3, rad=0.2'))
# 显示图例
mp.legend(loc='upper left')
# 显示图形
mp.show()
图形对象
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.figure('Figure Object 1', figsize=(8, 6), dpi=60,
facecolor='lightgray')#打开窗口,设置分辨率
mp.title('Figure Object 1', fontsize=20)#设置标题
mp.xlabel('x', fontsize=14)#水平标签文本,fontsize=字体大小
mp.ylabel('y', fontsize=14)#垂直标签文件,fontsize字体大小
mp.tick_params(labelsize=10)#labelsize=刻度标签字体大小
mp.grid(linestyle=':')#linestyle=网格线风格
mp.figure('Figure Object 2', figsize=(8, 6), dpi=60,
facecolor='lightgray')
mp.title('Figure Object 2', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.figure('Figure Object 1')
mp.plot(x, cos_y, color='dodgerblue',
label=r'$y=\frac{1}{2}cos(x)$')
mp.figure('Figure Object 2')
mp.plot(x, sin_y, color='orangered', label=r'$y=sin(x)$')
mp.legend()
mp.figure('Figure Object 1')
mp.legend()
mp.show()
子坐标图
代码:sub1.py、sub2.py、sub3.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
mp.figure(facecolor='lightgray')
mp.subplot(221)
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(222)
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(223)
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '3', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(224)
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '4', ha='center', va='center', size=36,
alpha=0.5)
mp.tight_layout()
mp.show()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
import matplotlib.gridspec as mg
mp.figure(facecolor='lightgray')
gs = mg.GridSpec(3, 3)
mp.subplot(gs[0, :2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(gs[1:, 0])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(gs[2, 1:])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '3', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(gs[:2, 2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '4', ha='center', va='center', size=36,
alpha=0.5)
mp.subplot(gs[1, 1])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '5', ha='center', va='center', size=36,
alpha=0.5)
mp.tight_layout()
mp.show()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
mp.figure(facecolor='lightgray')
mp.axes([0.03, 0.038, 0.94, 0.924])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36,
alpha=0.5)
mp.axes([0.63, 0.076, 0.31, 0.308])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36,
alpha=0.5)
mp.show()
设置坐标轴刻度定位器
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
mp.figure()
locators = [
'mp.NullLocator()',
'mp.MaxNLocator(nbins=3, steps=[1, 3, 5, 7, 9])',
'mp.FixedLocator(locs=[0, 2.5, 7.5, 10])',
'mp.AutoLocator()',
'mp.IndexLocator(offset=0.5, base=1.5)',
'mp.MultipleLocator()',
'mp.LinearLocator(numticks=21)',
'mp.LogLocator(base=2, subs=[1.0])']
n_locators = len(locators)
for i, locator in enumerate(locators):
mp.subplot(n_locators, 1, i + 1)
mp.xlim(0, 10)
mp.ylim(-1, 1)
mp.yticks(())
ax = mp.gca()
ax.spines['left'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data', 0))
ax.xaxis.set_major_locator(eval(locator))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
mp.plot(np.arange(11), np.zeros(11), color='none')
mp.text(5, 0.3, locator[3:], ha='center', size=12)
mp.tight_layout()
mp.show()
散点图
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
d = np.sqrt(x ** 2 + y ** 2)
mp.figure('Scatter', facecolor='lightgray')
mp.title('Scatter', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.scatter(x, y, s=6, c=d, cmap='jet_r', alpha=0.5)
mp.show()
填充
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000
x = np.linspace(0, 8 * np.pi, n)
sin_y = np.sin(x)
cos_y = np.cos(x / 2) / 2
mp.figure('Fill', facecolor='lightgray')
mp.title('Fill', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.plot(x, sin_y, c='dodgerblue', label=r'$y=sin(x)$')
mp.plot(x, cos_y, c='orangered',
label=r'$y=\frac{1}{2}cos(\frac{x}{2})$')
mp.fill_between(x, cos_y, sin_y, cos_y < sin_y,
color='dodgerblue', alpha=0.5)
mp.fill_between(x, cos_y, sin_y, cos_y > sin_y,
color='orangered', alpha=0.5)
mp.legend()
mp.show()
条形图
代码:bar.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 12
x = np.arange(n)
y1 = np.random.uniform(0.5, 1.0, n) * (1 - x / n)
y2 = np.random.uniform(0.5, 1.0, n) * (1 - x / n)
mp.figure('Bar', facecolor='lightgray')
mp.title('Bar', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.xticks(x, x + 1)
mp.ylim(-1.25, 1.25)
mp.tick_params(labelsize=10)
mp.grid(axis='y', linestyle=':')
mp.bar(x, y1, ec='white', fc='dodgerblue',
label='Sample 1')
for _x, _y in zip(x, y1):
mp.text(_x, _y, '%.2f' % _y, ha='center',
va='bottom', size=8)
mp.bar(x, -y2, ec='white', fc='dodgerblue', alpha=0.5,
label='Sample 2')
for _x, _y in zip(x, y2):
mp.text(_x, -_y - 0.015, '%.2f' % _y, ha='center',
va='top', size=8)
mp.legend()
mp.show()
等高线图
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
mp.figure('Contour', facecolor='lightgray')
mp.title('Contour', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.contourf(x, y, z, 8, cmap='jet')
cntr = mp.contour(x, y, z, 8, colors='black', linewidths=0.5)
mp.clabel(cntr, inline_spacing=1, fmt='%.1f', fontsize=8)
mp.show()
三维曲面/线框图
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
from mpl_toolkits.mplot3d import axes3d
n = 1000
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
mp.figure('3D Surface')
ax = mp.gca(projection='3d')
mp.title('3D Surface', fontsize=20)
ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_zlabel('z', fontsize=14)
mp.tick_params(labelsize=10)
ax.plot_surface(x, y, z, rstride=10, cstride=10, cmap='jet')
mp.figure('3D Wireframe')
ax = mp.gca(projection='3d')
mp.title('3D Wireframe', fontsize=20)
ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_zlabel('z', fontsize=14)
mp.tick_params(labelsize=10)
ax.plot_wireframe(x, y, z, rstride=20, cstride=20,
linewidth=0.5, color='orangered')
mp.show()
饼图
代码:pie.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as mp
values = [26, 17, 21, 29, 11]
spaces = [0.05, 0.01, 0.01, 0.01, 0.01]
labels = ['Python', 'JavaScript', 'C++', 'C', 'PHP']
colors = ['dodgerblue', 'orangered', 'limegreen', 'violet',
'gold']
mp.figure('Pie', facecolor='lightgray')
mp.title('Pie', fontsize=20)
mp.pie(values, spaces, labels, colors, '%d%%', shadow=True,
startangle=90)
mp.axis('equal')
mp.show()
坐标格线
代码:grid.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-5, 5, 1000)
y = 8 * np.sinc(x)
mp.figure('Grid', facecolor='lightgray')
mp.title('Grid', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(mp.MultipleLocator())
ax.xaxis.set_minor_locator(mp.MultipleLocator(.1))
ax.yaxis.set_major_locator(mp.MultipleLocator())
ax.yaxis.set_minor_locator(mp.MultipleLocator(.1))
mp.tick_params(labelsize=10)
ax.grid(which='major', axis='both', linewidth=0.75,
linestyle='-', color='lightgray')
ax.grid(which='minor', axis='both', linewidth=0.25,
linestyle='-', color='lightgray')
mp.plot(x, y, c='dodgerblue', label=r'$y=8sinc(x)$')
mp.legend()
mp.show()