Python 可视化

Python - 可视化

知识点

  1. 颜色
缩写含义
bblue
ggreen
rred
ccyan
mmagenta
yyellow
kblack
wwhite

其他颜色表示方法:灰色阴影,html十六进制,RGB元组

  1. pyplot:经典高层封装。pylab:将maplotlib和numpy合并的模块,模拟matlab编程环境。面向对象的方式:matplotlib的精髓,更基础和底层的方式。
    • pyplot 简单易用,交互使用方便,底层定制能力buzu
    • pylab 不推荐使用
      -面向对象 难度大,定制能力强,是matplotlib的精髓。
  2. 常用。
import matplotlib.pyplot as plt
fig,ax = plt.subplots(nrow,ncol,figsize=(m,n))
plt.legend(loc=0,ncol=3)
#array axis
plt.xlim(xmin=x,xmax=y)
plt.axis([x1,x2,x3,x4..])
#date axis
date_format = mpl.dates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(date_format)
#annotate/text
plt.annotate('here',xy=(0,1),xytext = (0,20),arrowprops = ..)
plt.text(x,y,'put your name here')
#fill color
plt.fill(x,y,color='b',alpha=0.5)
plt.fill_between(x,y1,y2,color='r',where=y1>y2,alpha=0.5)
#default color pad
plt.rcParams['axes.color_cycle']
#styles
plt.style.available -> ggplot, fivethirtyeight
plt.style.use('ggplot')
#font
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r'\users\usuus\Fonts\simsun.ttf',size=12)

Matplotlib自带mathtext引擎,不需要安装Tex系统。

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([1,7])
ax.set_ylim([1,5])
ax.text(2,4,r'$\alpha_{i}\beta_{j}\Pi\lambda\Omega$')
ax.text(4,4,r'$\sin(0)=\cos(\frac{\pi}{2})$')
ax.text(2,2,r'$lim_{x->y}\frac{1}{x^3}$')
ax.text(4,2,r'$\sqrt[4]{x}=\sqrt{y}$')
  1. 图形
import matplotlib.patches as mpatches
fig,ax=plt.subplots()
xy1 = np.array([0,2,0,2])
xy2 = np.array([0.2,0.8])
xy3 = np.array([0.8,0.2])
xy4 = np.array([0.8,0.8])

circle = mpatches.Circle(xy1,0.05)
ax.add_patch(circle)
rect = mpatches.Rectangle(xy2,0.2,0.1,color='r')
ax.add_patch(rect)
polygon = mpatches.RegularPolygon(xy3,5,0.1,color='g')
ax.add_patch(polygon)
ellipse = mpatches.Ellipse(xy4,0.4,0.2,color='y')
ax.add_patch(ellipse)

plt.axis('equal')
plt.grid()
plt.show()
  1. 极坐标
r = np.arange(1,6,1)
theta = [0,np.pi/4,np.pi/2,np.pi*3/4,np.pi]
ax = plt.subplot(111,projection='polar')
ax.plot(theta,r,color='r',linewidth=3)
ax.grid(True)
plt.show()
  1. 函数积分图
def fun(x):
    return -(x-2)*(x-8)+40

x = np.linspace(0,10)
y = fun(x)
fig,ax = plt.subplots()
plt.plot(x,y,'r',linewidth=2)
a,b=2,9
ax.set_xticks([a,b])
ax.set_xticklabels(['$a$','$b$'])
ax.set_yticks([])
plt.figtext(0.9,0.05,'$x$')
plt.figtext(0.1,0.9,'$y$')
ix = np.linspace(a,b)
iy = fun(ix)

ixy = zip(ix,iy)
verts = [(a,0)]+list(ixy)+[(b,0)]
poly = mpatches.Polygon(verts,facecolor='0.8',edgecolor='0.1')
ax.add_patch(poly)

x_math,y_math = (a+b)/2,35

plt.text(x_math,y_math,r'$\int_{a}^{b}(-(x-2)*(x-8)+40)dx$',horizontalalignment='center')
plt.show()
  1. 散点条形图。
plt.style.use('ggplot')
x = np.random.randn(200)
y = x+np.random.randn(200)*0.5

margin_boarder = 0.1
width,height = 0.6,0.2
margin_between = 0.02

left_s = margin_boarder
bottom_s = margin_boarder
height_s = width
width_s = width

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

left_y = bottom_x
bottom_y = margin_boarder
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]

binwidth = 0.25
xymax = np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
lim = int(xymax/binwidth+1)*binwidth

axScatter = plt.axes(rect_s)
axScatter.scatter(x,y,color='b')
axScatter.set_xlim(-lim,lim)
axScatter.set_ylim(-lim,lim)

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

axHisX = plt.axes(rect_x)
axHisX.hist(x,bins=bins)
axHisX.set_xlim(axScatter.get_xlim())
axHisY.set_ylim(axScatter.get_ylim())
axHisY = plt.axes(rect_y)
axHisY.hist(y,bins=bins,orientation='horizontal')

axHisX.set_xticks([])
axHisY.set_yticks([])

plt.title('Scatter and Hist')
plt.show()
  1. 足球运动员
from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r'c:\windows\Fonts\simsun.ttc',size=12)

plt.style.use('ggplot')

ability_label = [u'进攻',u'防守',u'盘带',u'速度',u'体力',u'射术']
ability_size = 6

ax1 = plt.subplot(221,projection = 'polar')
ax2 = plt.subplot(222,projection = 'polar')
ax3 = plt.subplot(223,projection = 'polar')
ax4 = plt.subplot(224,projection = 'polar')

player = {
    'M':[99,70,100,90,90,99],
    'H':[90,80,90,85,90,90],
    'P':np.random.randint(size=ability_size,low=60,high=99),
    'Q':np.random.randint(size=ability_size,low=60,high=99)
}
theta = np.linspace(0,2*np.pi,6,endpoint=False)
theta = np.append(theta,theta[0])

player['M'] = np.append(player['M'],player['M'][0])
ax1.plot(theta,player['M'],'r')
ax1.fill(theta,player['M'],'r',alpha=0.3)
ax1.set_xticks(theta)
ax1.set_xticklabels(ability_label,fontproperties=font)
ax1.set_yticks([20,40,60,80,100])
ax1.set_title(u'梅西',fontproperties=font,size=20)

player['H'] = np.append(player['H'],player['H'][0])
ax2.plot(theta,player['H'],'g')
ax2.fill(theta,player['H'],'g',alpha=0.3)
ax2.set_xticks(theta)
ax2.set_xticklabels(ability_label,fontproperties=font)
ax2.set_yticks([20,40,60,80,100])
ax2.set_title(u'哈维',fontproperties=font,size=20)

player['P'] = np.append(player['P'],player['P'][0])
ax3.plot(theta,player['P'],'b')
ax3.fill(theta,player['P'],'b',alpha=0.3)
ax3.set_xticks(theta)
ax3.set_xticklabels(ability_label,fontproperties=font)
ax3.set_yticks([20,40,60,80,100])
ax3.set_title(u'皮克',fontproperties=font,size=20)

player['Q'] = np.append(player['Q'],player['Q'][0])
ax4.plot(theta,player['Q'],'y')
ax4.fill(theta,player['Q'],'y',alpha=0.3)
ax4.set_xticks(theta)
ax4.set_xticklabels(ability_label,fontproperties=font)
ax4.set_yticks([20,40,60,80,100])
ax4.set_title(u'切赫',fontproperties=font,size=20)

plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值