導航:首頁 > 編程語言 > python繪圖三色六邊形

python繪圖三色六邊形

發布時間:2022-04-19 16:55:53

python怎麼畫六角形程序

python的turtle畫六邊形程序很簡單,如下4行代碼所示:
from turtle import *;
for i in range(6):(
left(60),fd(50));
done();

❷ 如何使用 Python 創建一個 NBA 得分圖

首先,我們需要獲得每個球員的投籃數據。利用 Savvas Tjortjoglou 貼出的代碼,筆者從 NBA.com 網站 API 上獲取了數據。在此不會貼出這個函數的結果。如果你感興趣,推薦你去看看 Savvas Tjortjoglou 的博客。

def aqcuire_shootingData(PlayerID,Season):
import requests
shot_chart_url = 'http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS='+Season+'&ContextFilter='\
'&ContextMeasure=FGA&DateFrom=&DateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID='\
'00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust='\
'N&PerMode=PerGame&Period=0&PlayerID='+PlayerID+'&PlusMinus=N&Position=&Rank='\
'N&RookieYear=&Season='+Season+'&SeasonSegment=&SeasonType=Regular+Season&TeamID='\
'0&VsConference=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&showZones=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 函數 http://matplotlib.org/api/pyplot_api.html 將投籃位置均勻地分組到六邊形中。該函數會對每個六邊形中每一個位置的投籃次數進行計數。

六邊形是均勻的分布在 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("http://stats.nba.com/media/players/230x185/"+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%的投籃進球百分比顯示為白色http://matplotlib.org/users/colormaps.html,而這樣顯示就會不明顯,所以筆者用淡粉紅色代表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實現六邊蜂窩型坐標

您好,對於正六邊形的構造您可以選用object-oriented programming,就是用class來做。在hexagon這個class裡面定義每個頂點的坐標,然後在主程序裡面創建每一個hexagon然後用set來儲存這些對象就可以了。

❹ Python畫一個圓外接正六邊形

具體方法如下:
1,先畫一條邊,旋轉60度,再畫一條邊,再旋轉60度,重復類似步驟,旋轉360度,就可畫完正六邊形。
2,導入turtle庫,設置畫布屏幕大小,設置筆粗細。
3,畫第一條邊,旋轉60度,畫第二條邊,旋轉60度,畫第三條邊。
4,畫第四條,第五條,第六條邊,不斷旋轉60度。

❺ python turtle模塊能畫什麼圖

我試過可以劃正六邊形圖,其它的圓形,正方形沒試過但help里有例子也可以畫

❻ 用Python生成了熱力圖,怎麼把每個格子邊框改成六邊形框

改不了吧。。。。或者說很難?你生成的熱力圖是用了什麼庫怎麼生成的?還是自己寫的?
要把矩形變成六邊形,我感覺只能自己寫了吧。
我有個想法,那個等邊六邊形可以拆成上下各三個等邊三角形吧。那 這些六邊形拼在一起,實際上就是這些等邊三角形拼在一起。那就先把各個矩形的值投射到三角形上,在把三角形組合成六邊形,求個均值,然後填色

❼ 求教python中的turtle

海龜庫(turtle)

❽ scratch對比python的代碼

1、Scratch中的每一個單獨的積木

8、自製積木就相當於python裡面的自定義方法/函數

9、列表就相當於python裡面的數組············


希望對您有用望採納

❾ python turtle作圖問題

簡介:turtle是一個簡單的繪圖工具。它提供了一個海龜,你可以把它理解為一個機器人,只聽得懂有限的指令。

1.在文件頭寫上如下行,這能讓我們在語句中插入中文
#-*- coding: utf-8 -*-

2.用import turtle導入turtle庫

3.繪圖窗口的原點(0,0)在正中間。默認情況下,海龜向正右方移動。

4.操縱海龜繪圖有著許多的命令,這些命令可以劃分為兩種:一種為運動命令,一種為畫筆控制命令
(1)運動命令:

forward(d)

向前移動距離d代表距離

backward(d)

向後移動距離d代表距離

right(degree)

向右轉動多少度

left(degree)

向左轉動多少度

goto(x,y)

將畫筆移動到坐標為(x,y)的位置

stamp()

繪制當前圖形

speed(speed)

畫筆繪制的速度范圍[0,10]整數

(2)畫筆控制命令:

down()

畫筆落下,移動時繪制圖形

up()

畫筆抬起,移動時不繪制圖形

setheading(degree)

海龜朝向,degree代表角度

reset()

恢復所有設置

pensize(width)

畫筆的寬度

pencolor(colorstring)

畫筆的顏色

fillcolor(colorstring)

繪制圖形的填充顏色

fill(Ture)

fill(False)

circle(radius, extent)

繪制一個圓形,其中radius為半徑,extent為度數,例如若extent為180,則畫一個半圓;如要畫一個圓形,可不必寫第二個參數

5.幾個例子
1)畫一個邊長為60的三角形
#-*- coding: utf-8 -*-
importturtle
a=60
turtle.forward(a)
turtle.left(120)
turtle.forward(a)
turtle.left(120)
turtle.forward(a)
turtle.left(120)

2)畫一個邊長為60的正方形,並填充為紅色,邊框為藍色
#-*- coding: utf-8 -*-
importturtle
turtle.reset()
a= 60
turtle.fillcolor("red")
turtle.pencolor("blue")
turtle.pensize(10)
turtle.fill(True)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.fill(False)

6.練習:
1)畫一個五邊形
2)畫一個六邊形
3)任意輸入一個正整數m(>=3),畫一個多邊形(m條邊)
4)畫一個五角星,如下所示,注意填充為紅色
5)畫一個中國象棋棋盤,如下圖所示,其中漢字不必顯示出來:
6)繪制奧運五環圖,其中五種顏色分別為藍色、黑色、紅色、黃色和綠色。注意根據實際效果調整圓形的大小和位置。

❿ 求大神用Python做一下這些題 身為小白的我實在不會 只做1,2,5題

from turtle import *

def fucn(n):

====pu()

====goto(0,250)

====pd()

====a=360/n

====for i in range(n):

========forward(200)

========right(a)

====done()

#fucn(5)

def wujiao():

====for i in range(5):

========forward(200)

========right(144)

====done()

#wujiao()

def sum(Str):

====l=list(Str)

====l=set(l)

====s=0

====for i in l:

========s=s+int(i)

====return s

#a=sum('7895442115456')

#print(a)

閱讀全文

與python繪圖三色六邊形相關的資料

熱點內容
程序員喜歡留指甲嗎 瀏覽:516
七牛雲伺服器收費標准 瀏覽:627
時光相冊加密空間密碼忘記 瀏覽:474
華為雲為用戶提供的服務雲伺服器 瀏覽:634
minecraftlinux伺服器搭建 瀏覽:376
linux命令新建文件 瀏覽:708
長線pdf 瀏覽:607
程序員電腦支持手寫 瀏覽:414
解壓頭戴式耳機推薦 瀏覽:344
紙條app上怎麼樣看對方主頁 瀏覽:883
編譯英語單詞怎麼寫 瀏覽:249
編譯原理和匯編原理的區別 瀏覽:864
如何給加密的pdf解密 瀏覽:770
華為盒子時間同步伺服器地址 瀏覽:95
python處理excel亂碼 瀏覽:391
mysql的命令行 瀏覽:822
jpeg採用什麼演算法 瀏覽:701
程序員紅軸薄膜 瀏覽:306
洗臉盆壓縮 瀏覽:780
dpd是什麼演算法 瀏覽:157