Python画正弦图并填充颜色
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo01_fill.py 填充
"""
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(0, 8 * np.pi, 1000)
sinx = np.sin(x)
cosx = np.cos(x / 2) / 2
mp.figure('Fill', facecolor='lightgray')
mp.title('Fill', fontsize=18)
mp.grid(linestyle=':')
mp.plot(x, sinx, color='dodgerblue', label='sin(x)')
mp.plot(x, cosx, color='orangered', label='cos(x)')
# 绘制填充
mp.fill_between(x, sinx, cosx, sinx > cosx,
color='dodgerblue', alpha=0.5)
mp.fill_between(x, sinx, cosx, sinx < cosx,
color='orangered', alpha=0.5)
mp.legend()
mp.show()
图形如下所示:
Python画条形图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo02_bar.py 柱状图
"""
import numpy as np
import matplotlib.pyplot as mp
apples = np.array([
87, 23, 45, 68, 23, 52, 38, 45, 62, 39, 23, 42])
oranges = np.array([
47, 56, 37, 84, 59, 23, 84, 56, 29, 43, 85, 35])
mp.figure('Bar Chart', facecolor='lightgray')
mp.title('Bar Chart', fontsize=18)
mp.grid(linestyle=':', axis='y')
mp.xlabel('Date', fontsize=14)
mp.ylabel('Volume', fontsize=14)
x = np.arange(apples.size)
mp.bar(x - 0.2, apples, 0.4, label='Apples',
color='dodgerblue')
mp.bar(x + 0.2, oranges, 0.4, label='Oranges',
color='orangered')
# 优化x轴刻度文本
mp.xticks(x, [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
mp.legend()
mp.show()
Python画饼图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo03_pie.py 饼状图
"""
import numpy as np
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++', 'Java', 'PHP']
colors = ['dodgerblue', 'orangered',
'limegreen', 'violet', 'gold']
mp.figure('Pie Chart', facecolor='lightgray')
mp.axis('equal') # 设置等轴比例绘制饼状图
mp.pie(values, spaces, labels, colors,
'%.1f%%', shadow=True)
mp.legend()
mp.show()
Python画等高线热图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo04_contour.py 等高线图
"""
import numpy as np
import matplotlib.pyplot as mp
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# 通过x与y 得到每个坐标点的高度
z = (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 画图
mp.figure('Contour', facecolor='lightgray')
mp.title('Contour', fontsize=18)
mp.grid(linestyle=':')
cntr = mp.contour(x, y, z, 8, colors='black',
linewidths=0.5)
mp.clabel(cntr, inline_spacing=2, fmt='%.1f',
fontsize=10)
# 填充等高线图
mp.contourf(x, y, z, 8, cmap='jet')
mp.show()
Python画热成像图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo05_imshow.py 热成像图
"""
import numpy as np
import matplotlib.pyplot as mp
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# 通过x与y 得到每个坐标点的高度
z = (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 画图
mp.figure('Imshow', facecolor='lightgray')
mp.title('Imshow', fontsize=18)
mp.imshow(z, cmap='jet', origin='lower')
mp.colorbar()
mp.show()
Python画三维散点图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo06_3dscatter.py 三维散点图
"""
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib.pyplot as mp
n = 300
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
z = np.random.normal(0, 1, n)
mp.figure('3D Scatter', facecolor='lightgray')
ax3d = mp.gca(projection='3d')
ax3d.set_xlabel('x', fontsize=14)
ax3d.set_ylabel('y', fontsize=14)
ax3d.set_zlabel('z', fontsize=14)
ax3d.scatter(x, y, z, marker='o', s=100,
color='dodgerblue', alpha=0.5)
mp.tight_layout()
mp.show()
Python画三维表面热力图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo07_3dsurface.py
"""
import numpy as np
import matplotlib.pyplot as mp
from mpl_toolkits.mplot3d import axes3d
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# 通过x与y 得到每个坐标点的高度
z = (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 画图
mp.figure('Imshow', facecolor='lightgray')
ax3d = mp.gca(projection='3d')
ax3d.set_xlabel('x')
ax3d.set_ylabel('y')
ax3d.set_zlabel('z')
ax3d.plot_surface(x, y, z, rstride=30,
cstride=30, cmap='jet')
mp.tight_layout()
mp.show()
Python画等高线图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
demo08_wireframe.py
"""
import numpy as np
import matplotlib.pyplot as mp
from mpl_toolkits.mplot3d import axes3d
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# 通过x与y 得到每个坐标点的高度
z = (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 画图
mp.figure('Imshow', facecolor='lightgray')
ax3d = mp.gca(projection='3d')
ax3d.set_xlabel('x')
ax3d.set_ylabel('y')
ax3d.set_zlabel('z')
ax3d.plot_wireframe(
x, y, z, rstride=30, cstride=30,
linewidth=0.5, color='dodgerblue')
mp.tight_layout()
mp.show()
Python画动态泡泡图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
import matplotlib.animation as ma
n = 100
balls = np.zeros(n, dtype=[
('position', 'f8', 2),
('size', 'f8', 1),
('growth', 'f8', 1),
('color', 'f8', 4)])
# 初始化balls的字段值
# print(balls['position'].shape)
balls['position'] = np.random.uniform(0, 1, (n, 2))
balls['size'] = np.random.uniform(40, 80, n)
balls['growth'] = np.random.uniform(10, 30, n)
balls['color'] = np.random.uniform(0, 1, (n, 4))
# 画图
mp.figure('Points', facecolor='lightgray')
mp.xticks([])
mp.yticks([])
sc = mp.scatter(balls['position'][:, 0],
balls['position'][:, 1],
s=balls['size'],
color=balls['color'])
def update(number):
# 让每个球size自增,重新绘制界面
balls['size'] += balls['growth']
index = number % n
balls['size'][index] = 60
balls['position'][index] = \
np.random.uniform(0, 1, (1, 2))
sc.set_sizes(balls['size'])
sc.set_offsets(balls['position'])
# 执行动画
a = ma.FuncAnimation(mp.gcf(), update, interval=33)
mp.show()
效果如下所示(真的会动哟)
Python画动态弦图
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
import matplotlib.animation as ma
mp.figure("Signal", facecolor='lightgray')
mp.title("Signal", fontsize=14)
mp.xlim(0, 10)
mp.ylim(-3, 3)
mp.grid(linestyle='--', color='lightgray', alpha=0.5)
pl = mp.plot([], [], color='dodgerblue', label='Signal')[0]
pl.set_data([], [])
x = 0
def update(data):
t, v = data
x, y = pl.get_data()
x.append(t)
y.append(v)
# 重新设置数据源
pl.set_data(x, y)
# 移动坐标轴
if(x[-1] > 10):
mp.xlim(x[-1] - 10, x[-1])
def y_generator():
global x
y = np.sin(2 * np.pi * x) * np.exp(np.sin(0.2 * np.pi * x))
yield (x, y)
x += 0.05
anim = ma.FuncAnimation(mp.gcf(), update, y_generator, interval=20)
mp.tight_layout()
mp.show()