重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

gca函数python Gcd函数

如何使用 Python 创建一个 NBA 得分图

首先,我们需要获得每个球员的投篮数据。利用 Savvas Tjortjoglou 贴出的代码,笔者从 NBA.com 网站 API 上获取了数据。在此不会贴出这个函数的结果。如果你感兴趣,推荐你去看看 Savvas Tjortjoglou 的博客。

创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站制作、成都网站设计、外贸营销网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元舒城做网站,已为上家服务,为舒城各地企业和个人服务,联系电话:028-86922220

def aqcuire_shootingData(PlayerID,Season):

import requests

shot_chart_url = ';CFPARAMS='+Season+'ContextFilter='\

'ContextMeasure=FGADateFrom=DateTo=GameID=GameSegment=LastNGames=0LeagueID='\

'00Location=MeasureType=BaseMonth=0OpponentTeamID=0Outcome=PaceAdjust='\

'NPerMode=PerGamePeriod=0PlayerID='+PlayerID+'PlusMinus=NPosition=Rank='\

'NRookieYear=Season='+Season+'SeasonSegment=SeasonType=Regular+SeasonTeamID='\

'0VsConference=VsDivision=mode=AdvancedshowDetails=0showShots=1showZones=0'

response = requests.get(shot_chart_url)

headers = response.json()['resultSets'][0]['headers']

shots = response.json()['resultSets'][0]['rowSet']

shot_df = pd.DataFrame(shots, columns=headers)

return shot_df

接下来,我们需要绘制一个包含得分图的篮球场图。该篮球场图例必须使用与NBA.com API 相同的坐标系统。例如,3分位置的投篮距篮筐必须为 X 单位,上篮距离篮筐则是 Y 单位。同样,笔者再次使用了 Savvas Tjortjoglou 的代码(哈哈,否则的话,搞明白 NBA.com 网站的坐标系统肯定会耗费不少的时间)。

def draw_court(ax=None, color='black', lw=2, outer_lines=False):

from matplotlib.patches import Circle, Rectangle, Arc

if ax is None:

ax = plt.gca()

hoop = Circle((0, 0), radius=7.5, linewidth=lw, color=color, fill=False)

backboard = Rectangle((-30, -7.5), 60, -1, linewidth=lw, color=color)

outer_box = Rectangle((-80, -47.5), 160, 190, linewidth=lw, color=color,

fill=False)

inner_box = Rectangle((-60, -47.5), 120, 190, linewidth=lw, color=color,

fill=False)

top_free_throw = Arc((0, 142.5), 120, 120, theta1=0, theta2=180,

linewidth=lw, color=color, fill=False)

bottom_free_throw = Arc((0, 142.5), 120, 120, theta1=180, theta2=0,

linewidth=lw, color=color, linestyle='dashed')

restricted = Arc((0, 0), 80, 80, theta1=0, theta2=180, linewidth=lw,

color=color)

corner_three_a = Rectangle((-220, -47.5), 0, 140, linewidth=lw,

color=color)

corner_three_b = Rectangle((220, -47.5), 0, 140, linewidth=lw, color=color)

three_arc = Arc((0, 0), 475, 475, theta1=22, theta2=158, linewidth=lw,

color=color)

center_outer_arc = Arc((0, 422.5), 120, 120, theta1=180, theta2=0,

linewidth=lw, color=color)

center_inner_arc = Arc((0, 422.5), 40, 40, theta1=180, theta2=0,

linewidth=lw, color=color)

court_elements = [hoop, backboard, outer_box, inner_box, top_free_throw,

bottom_free_throw, restricted, corner_three_a,

corner_three_b, three_arc, center_outer_arc,

center_inner_arc]

if outer_lines:

outer_lines = Rectangle((-250, -47.5), 500, 470, linewidth=lw,

color=color, fill=False)

court_elements.append(outer_lines)

for element in court_elements:

ax.add_patch(element)

ax.set_xticklabels([])

ax.set_yticklabels([])

ax.set_xticks([])

ax.set_yticks([])

return ax

我想创造一个不同位置的投篮百分比数组,因此决定利用 matplot 的 Hexbin 函数 将投篮位置均匀地分组到六边形中。该函数会对每个六边形中每一个位置的投篮次数进行计数。

六边形是均匀的分布在 XY 网格中。「gridsize」变量控制六边形的数目。「extent」变量控制第一个和最后一个六边形的绘制位置(一般来说第一个六边形的位置基于第一个投篮的位置)。

计算命中率则需要对每个六边形中投篮的次数和投篮得分次数进行计数,因此笔者对同一位置的投篮和得分数分别运行 hexbin 函数。然后,只需用每个位置的进球数除以投篮数。

def find_shootingPcts(shot_df, gridNum):

x = shot_df.LOC_X[shot_df['LOC_Y']425.1] #i want to make sure to only include shots I can draw

y = shot_df.LOC_Y[shot_df['LOC_Y']425.1]

x_made = shot_df.LOC_X[(shot_df['SHOT_MADE_FLAG']==1) (shot_df['LOC_Y']425.1)]

y_made = shot_df.LOC_Y[(shot_df['SHOT_MADE_FLAG']==1) (shot_df['LOC_Y']425.1)]

#compute number of shots made and taken from each hexbin location

hb_shot = plt.hexbin(x, y, gridsize=gridNum, extent=(-250,250,425,-50));

plt.close() #don't want to show this figure!

hb_made = plt.hexbin(x_made, y_made, gridsize=gridNum, extent=(-250,250,425,-50),cmap=plt.cm.Reds);

plt.close()

#compute shooting percentage

ShootingPctLocs = hb_made.get_array() / hb_shot.get_array()

ShootingPctLocs[np.isnan(ShootingPctLocs)] = 0 #makes 0/0s=0

return (ShootingPctLocs, hb_shot)

笔者非常喜欢 Savvas Tjortjoglou 在他的得分图中加入了球员头像的做法,因此也顺道用了他的这部分代码。球员照片会出现在得分图的右下角。

def acquire_playerPic(PlayerID, zoom, offset=(250,400)):

from matplotlib import offsetbox as osb

import urllib

pic = urllib.urlretrieve(""+PlayerID+".png",PlayerID+".png")

player_pic = plt.imread(pic[0])

img = osb.OffsetImage(player_pic, zoom)

#img.set_offset(offset)

img = osb.AnnotationBbox(img, offset,xycoords='data',pad=0.0, box_alignment=(1,0), frameon=False)

return img

笔者想用连续的颜色图来描述投篮进球百分比,红圈越多代表着更高的进球百分比。虽然「红」颜色图示效果不错,但是它会将0%的投篮进球百分比显示为白色,而这样显示就会不明显,所以笔者用淡粉红色代表0%的命中率,因此对红颜色图做了下面的修改。

#cmap = plt.cm.Reds

#cdict = cmap._segmentdata

cdict = {

'blue': [(0.0, 0.6313725709915161, 0.6313725709915161), (0.25, 0.4470588266849518, 0.4470588266849518), (0.5, 0.29019609093666077, 0.29019609093666077), (0.75, 0.11372549086809158, 0.11372549086809158), (1.0, 0.05098039284348488, 0.05098039284348488)],

'green': [(0.0, 0.7333333492279053, 0.7333333492279053), (0.25, 0.572549045085907, 0.572549045085907), (0.5, 0.4156862795352936, 0.4156862795352936), (0.75, 0.0941176488995552, 0.0941176488995552), (1.0, 0.0, 0.0)],

'red': [(0.0, 0.9882352948188782, 0.9882352948188782), (0.25, 0.9882352948188782, 0.9882352948188782), (0.5, 0.9843137264251709, 0.9843137264251709), (0.75, 0.7960784435272217, 0.7960784435272217), (1.0, 0.40392157435417175, 0.40392157435417175)]

}

mymap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

好了,现在需要做的就是将它们合并到一块儿。下面所示的较大函数会利用上文描述的函数来创建一个描述投篮命中率的得分图,百分比由红圈表示(红色越深 = 更高的命中率),投篮次数则由圆圈的大小决定(圆圈越大 = 投篮次数越多)。需要注意的是,圆圈在交叠之前都能增大。一旦圆圈开始交叠,就无法继续增大。

在这个函数中,计算了每个位置的投篮进球百分比和投篮次数。然后画出在该位置投篮的次数(圆圈大小)和进球百分比(圆圈颜色深浅)。

def shooting_plot(shot_df, plot_size=(12,8),gridNum=30):

from matplotlib.patches import Circle

x = shot_df.LOC_X[shot_df['LOC_Y']425.1]

y = shot_df.LOC_Y[shot_df['LOC_Y']425.1]

#compute shooting percentage and # of shots

(ShootingPctLocs, shotNumber) = find_shootingPcts(shot_df, gridNum)

#draw figure and court

fig = plt.figure(figsize=plot_size)#(12,7)

cmap = mymap #my modified colormap

ax = plt.axes([0.1, 0.1, 0.8, 0.8]) #where to place the plot within the figure

draw_court(outer_lines=False)

plt.xlim(-250,250)

plt.ylim(400, -25)

#draw player image

zoom = np.float(plot_size[0])/(12.0*2) #how much to zoom the player's pic. I have this hackily dependent on figure size

img = acquire_playerPic(PlayerID, zoom)

ax.add_artist(img)

#draw circles

for i, shots in enumerate(ShootingPctLocs):

restricted = Circle(shotNumber.get_offsets()[i], radius=shotNumber.get_array()[i],

color=cmap(shots),alpha=0.8, fill=True)

if restricted.radius 240/gridNum: restricted.radius=240/gridNum

ax.add_patch(restricted)

#draw color bar

ax2 = fig.add_axes([0.92, 0.1, 0.02, 0.8])

cb = mpl.colorbar.ColorbarBase(ax2,cmap=cmap, orientation='vertical')

cb.set_label('Shooting %')

cb.set_ticks([0.0, 0.25, 0.5, 0.75, 1.0])

cb.set_ticklabels(['0%','25%', '50%','75%', '100%'])

plt.show()

return ax

好了,大功告成!因为笔者是森林狼队的粉丝,在下面用几分钟跑出了森林狼队前六甲的得分图。

PlayerID = '203952' #andrew wiggins

shot_df = aqcuire_shootingData(PlayerID,'2015-16')

ax = shooting_plot(shot_df, plot_size=(12,8));

python散点图横坐标文字倾斜

在 python 中使用 matplotlib 绘制散点图时,可以使用 xtick.set_rotation() 函数来设置 x 轴刻度标签的旋转角度。例如,要将 x 轴刻度标签倾斜 45 度,可以使用以下代码:

Copy code

import matplotlib.pyplot as plt

# 绘制散点图

plt.scatter(x, y)

# 获取 x 轴的刻度对象

xticks = plt.gca().get_xticks()

# 设置 x 轴刻度标签的旋转角度

plt.gca().set_xticklabels(xticks, rotation=45)

# 显示图形

plt.show()

在这段代码中,我们使用 plt.scatter() 函数绘制散点图,然后使用 plt.gca().get_xticks() 函数获取 x 轴的刻度对象。接着,我们使用 plt.gca().set_xticklabels() 函数设置 x 轴刻度标签的旋转角度,最后使用 plt.show() 函数显示图形。

注意:在调用 plt.scatter() 函数之前,需要先设置 x 和 y 轴的数据。

python绘图篇

1,xlable,ylable设置x,y轴的标题文字。

2,title设置标题。

3,xlim,ylim设置x,y轴显示范围。

plt.show()显示绘图窗口,通常情况下,show()会阻碍程序运行,带-wthread等参数的环境下,窗口不会关闭。

plt.saveFig()保存图像。

面向对象绘图

1,当前图表和子图可以用gcf(),gca()获得。

subplot()绘制包含多个图表的子图。

configure subplots,可调节子图与图表边框距离。

可以通过修改配置文件更改对象属性。

图标显示中文

1,在程序中直接指定字体。

2, 在程序开始修改配置字典reParams.

3,修改配置文件。

Artist对象

1,图标的绘制领域。

2,如何在FigureCanvas对象上绘图。

3,如何使用Renderer在FigureCanvas对象上绘图。

FigureCanvas和Render处理底层图像操作,Artist处理高层结构。

分为简单对象和容器对象,简单的Aritist是标准的绘图元件,例如Line 2D,Rectangle,Text,AxesImage等,而容器类型包含许多简单的的 Aritist对象,使他们构成一个整体,例如Axis,Axes,Figure等。

直接创建Artist对象进项绘图操作步奏:

1,创建Figure对象(通过figure()函数,会进行许多初始化操作,不建议直接创建。)

2,为Figure对象创建一个或多个Axes对象。

3,调用Axes对象的方法创建各类简单的Artist对象。

Figure容器

如何找到指定的Artist对象。

1,可调用add_subplot()和add_axes()方法向图表添加子图。

2,可使用for循环添加栅格。

3,可通过transform修改坐标原点。

Axes容器

1,patch修改背景。

2,包含坐标轴,坐标网格,刻度标签,坐标轴标题等内容。

3,get_ticklabels(),,get-ticklines获得刻度标签和刻度线。

1,可对曲线进行插值。

2,fill_between()绘制交点。

3,坐标变换。

4,绘制阴影。

5,添加注释。

1,绘制直方图的函数是

2,箱线图(Boxplot)也称箱须图(Box-whisker Plot),是利用数据中的五个统计量:最小值、第一四分位

数、中位数、第三四分位数与最大值来描述数据的一种方法,它可以粗略地看出数据是否具有对称性以及分

布的分散程度等信息,特别可以用于对几个样本的比较。

3,饼图就是把一个圆盘按所需表达变量的观察数划分为若干份,每一份的角度(即面积)等价于每个观察

值的大小。

4,散点图

5,QQ图

低层绘图函数

类似于barplot(),dotchart()和plot()这样的函数采用低层的绘图函数来画线和点,来表达它们在页面上放置的位置以及其他各种特征。

在这一节中,我们会描述一些低层的绘图函数,用户也可以调用这些函数用于绘图。首先我们先讲一下R怎么描述一个页面;然后我们讲怎么在页面上添加点,线和文字;最后讲一下怎么修改一些基本的图形。

绘图区域与边界

R在绘图时,将显示区域划分为几个部分。绘制区域显示了根据数据描绘出来的图像,在此区域内R根据数据选择一个坐标系,通过显示出来的坐标轴可以看到R使用的坐标系。在绘制区域之外是边沿区,从底部开始按顺时针方向分别用数字1到4表示。文字和标签通常显示在边沿区域内,按照从内到外的行数先后显示。

添加对象

在绘制的图像上还可以继续添加若干对象,下面是几个有用的函数,以及对其功能的说明。

•points(x, y, ...),添加点

•lines(x, y, ...),添加线段

•text(x, y, labels, ...),添加文字

•abline(a, b, ...),添加直线y=a+bx

•abline(h=y, ...),添加水平线

•abline(v=x, ...),添加垂直线

•polygon(x, y, ...),添加一个闭合的多边形

•segments(x0, y0, x1, y1, ...),画线段

•arrows(x0, y0, x1, y1, ...),画箭头

•symbols(x, y, ...),添加各种符号

•legend(x, y, legend, ...),添加图列说明


本文题目:gca函数python Gcd函数
标题路径:http://cqcxhl.cn/article/hijhcp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP