Python数据分析之matplotlib(应用篇)

时间:2022-07-22
本文章向大家介绍Python数据分析之matplotlib(应用篇),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

美化图形

# 采用ggplot绘画风格
plt.style.use('ggplot')

fig,axes = plt.subplots(ncols=2,nrows=2)
ax1,ax2,ax3,ax4 = axes.ravel()


x,y = np.random.normal(size=(2,100))
ax1.plot(x,y,'o')

x = np.arange(0,10)
y = np.arange(0,10)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0,10,ncolors)
for s in shift:
    ax2.plot(x,y+s,'-')

x = np.arange(5)
y1,y2,y3 = np.random.randint(1,25,size=(3,5))
width = 0.25

ax3.bar(x,y1,width)
ax3.bar(x+width,y2,width,color='r')
ax3.bar(x+2*width,y3,width,color='g')

for i,color in  enumerate(plt.rcParams['axes.color_cycle']):
    xy = np.random.normal(size=2)
    ax4.add_patch(plt.Circle(xy,radius=0.3,color=color))
ax4.axis('equal')

plt.show()

极坐标

plt.style.use('ggplot')

r = np.arange(1,6)
theta = [0,np.pi/2,np.pi,np.pi*3/2,2*np.pi]

ax = plt.subplot(111,projection='polar')

ax.plot(theta,r,color='r',linewidth=2)

ax.grid(True)

plt.show()

函数积分图

def func(x):
    return -(x-2)*(x-8)+40

x = np.linspace(0,10)
y = func(x)

fig,axes = plt.subplots()
axes.plot(x,y,'r',linewidth=2)
a = 2
b = 9
axes.set_xticks([a,b])
axes.set_yticks([])
axes.set_xticklabels([r'$a$','$b$'])

ix = np.linspace(a,b)
iy = func(ix)
ixy = zip(ix,iy)

verts = [(a,0)]+list(ixy)+[(b,0)]
poly = Polygon(verts,facecolor='0.8',edgecolor='0.5')
axes.add_patch(poly)

plt.figtext(0.9,0.07,r'$x$')
plt.figtext(0.1,0.9,r'$y$')

x_math = (a+b)*0.5
y_math = 35

plt.text(x_math,y_math,r'$int_a^b(-(x-2)*(x-8)+40)dx$',fontsize=10,horizontalalignment='center')

plt.show()

二维散点概率分布图

plt.style.use('ggplot')

x = np.random.randn(200)
y = x + np.random.randn(200)*0.5

margin_border = 0.1
width = 0.6
margin_between = 0.02
height = 0.2

left_s = margin_border
bottom_s = margin_border
height_s = width
width_s = width

left_x = margin_border
bottom_x = margin_border+width+margin_between
height_x = height
width_x = width

left_y = margin_border+width+margin_between
bottom_y = margin_border
height_y = width
width_y = height

plt.figure(1,figsize=(8,8))
rect_s = [left_s,bottom_s,width_s,height_s]
rect_x = [left_x,bottom_x,width_x,height_x]
rect_y = [left_y,bottom_y,width_y,height_y]

axScatter = plt.axes(rect_s)
axHisX = plt.axes(rect_x)
axHisY = plt.axes(rect_y)
axHisX.set_xticks([])
axHisY.set_yticks([])

axScatter.scatter(x,y)

bin_width = 0.25
xymax = np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])

lim =int(xymax/bin_width+1) * bin_width

axScatter.set_xlim(-lim,lim)
axScatter.set_ylim(-lim,lim)

bins = np.arange(-lim,lim+bin_width,bin_width)

axHisX.hist(x,bins=bins)
axHisY.hist(y,bins=bins,orientation='horizontal')

axHisX.set_xlim(axScatter.get_xlim())
axHisY.set_ylim(axScatter.get_ylim())

plt.title('Scatter and Hist')
plt.show()

完整的绘制程序综合

import numpy as np
import matplotlib.pyplot as plt
from pylab import *

# 定义数据部分
x = np.arange(0., 10, 0.2)
y1 = np.cos(x)
y2 = np.sin(x)
y3 = np.sqrt(x)

# 绘制 3 条函数曲线
plt.plot(x, y1, color='blue', linewidth=1.5, linestyle='-', marker='.', label=r'$y = cos{x}$')
plt.plot(x, y2, color='green', linewidth=1.5, linestyle='-', marker='*', label=r'$y = sin{x}$')
plt.plot(x, y3, color='m', linewidth=1.5, linestyle='-', marker='x', label=r'$y = sqrt{x}$')

# 坐标轴上移
ax = plt.subplot(111)
ax.spines['right'].set_color('none')     # 去掉右边的边框线
ax.spines['top'].set_color('none')       # 去掉上边的边框线

# 移动下边边框线,相当于移动 X 轴
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))

# 移动左边边框线,相当于移动 y 轴
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 设置 x, y 轴的取值范围
plt.xlim(x.min()*1.1, x.max()*1.1)
plt.ylim(-1.5, 4.0)

# 设置 x, y 轴的刻度值
plt.xticks([2, 4, 6, 8, 10], [r'2', r'4', r'6', r'8', r'10'])
plt.yticks([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0],
    [r'-1.0', r'0.0', r'1.0', r'2.0', r'3.0', r'4.0'])

# 添加文字
plt.text(4, 1.68, r'$x in [0.0,  10.0]$', color='k', fontsize=15)
plt.text(4, 1.38, r'$y in [-1.0,  4.0]$', color='k', fontsize=15)

# 特殊点添加注解
plt.scatter([8,],[np.sqrt(8),], 50, color ='m')  # 使用散点图放大当前点
plt.annotate(r'$2sqrt{2}$', xy=(8, np.sqrt(8)), xytext=(8.5, 2.2), fontsize=16, color='#090909', arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.1', color='#090909'))

# 设置标题、x轴、y轴
plt.title(r'$the  function  figure  of  cos(),  sin()  and  sqrt()$', fontsize=19)
plt.xlabel(r'$the  input  value  of  x$', fontsize=18, labelpad=88.8)
plt.ylabel(r'$y = f(x)$', fontsize=18, labelpad=12.5)

# 设置图例及位置
plt.legend(loc='up right')
# plt.legend(['cos(x)', 'sin(x)', 'sqrt(x)'], loc='up right')

# 显示网格线
plt.grid(True)

# 显示绘图
plt.show()

参考

matplotlib核心剖析(http://www.cnblogs.com/vamei/archive/2013/01/30/2879700.html#commentform)