Python HW13 – DS

1265-张同学

发表文章数:58

首页 » 数据科学库 » 正文

知识点

  1. 颜色
缩写 含义
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white

其他颜色表示方法:灰色阴影,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()

拜师教育学员文章:作者:1265-张同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《Python HW13 – DS》 发布于2020-09-08

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录