第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 python 中創(chuàng)建圖形后如何更改散點(diǎn)圖的軸限制

在 python 中創(chuàng)建圖形后如何更改散點(diǎn)圖的軸限制

慕無忌1623718 2023-03-01 15:55:20
我想創(chuàng)建一個 NFL 場圖,然后在其上放置一個散點(diǎn)圖。這是我的字段代碼(只需復(fù)制粘貼所有這些。這不是我的問題所在):import matplotlib.pyplot as pltimport pylab as pl# Create figurefig, ax = pl.subplots(figsize=(15,10))# Set field dimensionsplt.xlim(0, 120)  # Field length including endzonesplt.ylim(0, 53.3)  # field width# Set field color greenax.set_facecolor('#79af75')ax.set_alpha(0.5)# Print linesfor i in range(0, 120, 10):    plt.axvline(i, color='white', linewidth=3, alpha=0.4, zorder=1)    if i == 10 or i == 110:  # Make endzone lines        plt.axvline(i, color='white', linewidth=5, alpha=0.4, zorder=1)# Paint numbersyds_from_sideline = 12for i in range(10, 50, 10):    plt.text(i+10, 53.3-yds_from_sideline, str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)    plt.text(110-i, 53.3-yds_from_sideline,  str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)    plt.text(i+10, yds_from_sideline, str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')    plt.text(110-i, yds_from_sideline, str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')# Paint 50 yard line numbersplt.text(60, 53.3-yds_from_sideline, str(50), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)plt.text(60, yds_from_sideline, str(50), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')# Print something in the endzonesplt.text(5, 26.5, 'Vikings', color='#4F2683', fontsize=30, verticalalignment='center', horizontalalignment='center', rotation=90)plt.text(115, 26.5, 'Opponent', color='black', fontsize=30, verticalalignment='center', horizontalalignment='center', rotation=270)# Fix the aspect ratio (optional)plt.gca().set_aspect(1)# Display the figureplt.show()我試圖將以下代碼添加到現(xiàn)場圖,但沒有成功。
查看完整描述

1 回答

?
萬千封印

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個贊

不確定這是否是最好的方法,但我首先縮放ylim到您想要的范圍 (-5,5),然后將碼值plt.text()從范圍 (0,53.3) 縮放到范圍 (-5,5) ,相應(yīng)地調(diào)整頂部/底部值plt.text()(從 53.3 到 5,從 0 到 -5)。最后更改ax.scatter()為直接引用您的數(shù)據(jù)列,因此要在 x 軸上偏移只需添加一個值 10。此外,刪除set_aspect(1)。


這樣你就不會在一開始就創(chuàng)建一個過大的圖形,然后你必須爭先恐后地提交。


代碼(更改前面的注釋# CHANGES HERE !!!:


import matplotlib.pyplot as plt

import pylab as pl

import numpy as np


# Create figure

fig, ax = pl.subplots(figsize=(15,10))


# Set field dimensions

plt.xlim(0, 120)  # Field length including endzones


# CHANGES HERE !!!

# set ylim respective to your data

plt.ylim(-5, 5)  # field width


# Set field color green

ax.set_facecolor('#79af75')

ax.set_alpha(0.5)


# Print lines

for i in range(0, 120, 10):

    plt.axvline(i, color='white', linewidth=3, alpha=0.4, zorder=1)

    if i == 10 or i == 110:  # Make endzone lines

        plt.axvline(i, color='white', linewidth=5, alpha=0.4, zorder=1)


# Paint numbers

yds_from_sideline = 12

for i in range(10, 50, 10):

#   CHANGES HERE !!!

#   change y values because ylim has changed (top-ylim - yds_from_sideline translated to scale of ylim)

    plt.text(i+10, 5-10*(yds_from_sideline/53.3), str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)

    plt.text(110-i, 5-10*(yds_from_sideline/53.3),  str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)


    plt.text(i+10, -5 + 10*(yds_from_sideline/53.3), str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')

    plt.text(110-i, -5 + 10*(yds_from_sideline/53.3), str(i), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')


# Paint 50 yard line numbers

# CHANGES HERE !!!

# change y values here as well

plt.text(60, 5-10*(yds_from_sideline/53.3), str(50), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center', rotation=180)

plt.text(60, -5+10*(yds_from_sideline/53.3), str(50), color='white', fontsize=20, verticalalignment='bottom', horizontalalignment='center')


# Print something in the endzones


plt.text(5, 0, 'Vikings', color='#4F2683', fontsize=30, verticalalignment='center', horizontalalignment='center', rotation=90)

plt.text(115, 0, 'Opponent', color='black', fontsize=30, verticalalignment='center', horizontalalignment='center', rotation=270)


# Just showing how to set titles and labels

plt.title('The gridiron', fontsize=14)

plt.ylabel('EPA', fontsize=12)

plt.xlabel('Yardline', fontsize=12)


plt.title('The gridiron', fontsize=14)

plt.ylabel('EPA', fontsize=12)

plt.xlabel('Yardline', fontsize=12)


ax.set_yticks(np.arange(-5, 6,1))


# CHANGES HERE !!!

# increase x by 10 to start at x = 10

ax.scatter(x=data['yardline_100']+10,y=data['epa'])




# # Fix the aspect ratio (optional)

# plt.gca().set_aspect(1)


# Display the figure

plt.show()

輸出(使用您的示例數(shù)據(jù)):

http://img1.sycdn.imooc.com//63ff052c00018bfc08970604.jpg

查看完整回答
反對 回復(fù) 2023-03-01
  • 1 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號