plot_find_center
时间: 2023-10-25 08:04:57 浏览: 94
plot_find_center是一个用于找到图形中心的函数。这个函数可以用不同的方法找到图形的中心点。首先,可以使用几何学方法来计算图形的重心。重心是图形的质心,它是所有点的平均位置。计算重心可以通过先计算图形的面积,然后计算每个点的相对重心位置来实现。另一种方法是计算图形的外接圆或内切圆,然后找到圆心。这些圆可以通过图形的边界点来计算。最后,还可以使用图像处理技术来找到图形的中心。这可以通过在图像中检测图形的轮廓,然后计算轮廓的重心来实现。这种方法通常在计算机视觉领域中使用。无论使用哪种方法,plot_find_center函数可以根据给定的图形找到其中心,并返回中心点的坐标。
相关问题
import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt import pandas as pd PLAYERS_LIMIT = 25 TABLE_CLASS_NAME = "players_table" plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False def get_top_players_scores(limit=PLAYERS_LIMIT, table_class_name=TABLE_CLASS_NAME): url = "https://nba.hupu.com/stats/players" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") players = [] scores = [] table = soup.find("table", class_=table_class_name) rows = table.find_all("tr") for row in rows[1:limit+1]: cols = row.find_all("td") player = cols[1].text.strip() score_range = cols[4].text.strip() score_parts = score_range.split("-") min_score = float(score_parts[0]) max_score = float(score_parts[1]) score = int((min_score + max_score) / 2) players.append(player) scores.append(score) return players, scores def plot_top_players_scores(players, scores): data = {"Player": players, "Score": scores} df = pd.DataFrame(data) fig, ax = plt.subplots(figsize=(12, 6)) ax.bar(players, scores, color='green', alpha=0.6) ax.set_xlabel('球员', fontsize=12) ax.set_ylabel('得分', fontsize=12) ax.set_title('NBA球员得分', fontsize=14) plt.xticks(rotation=45, ha='right', fontsize=8) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) for i, score in enumerate(scores): ax.text(i, score+0.5, str(score), ha='center', va='bottom') writer = pd.ExcelWriter('plot_top_players_scores.xlsx') df.to_excel(writer, index=False) writer.save() fig.tight_layout() plt.show() if __name__ == "__main__": players, scores = get_top_players_scores() plot_top_players_scores(players, scores)这段代码生成的excel损坏
这可能是因为在代码中将Excel文件的写操作放在了`for`循环中,每次迭代都会打开并写入Excel文件,导致Excel文件损坏。应该将Excel文件的写操作放在循环外面,只在循环结束后执行一次即可。修改后的代码如下:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import pandas as pd
PLAYERS_LIMIT = 25
TABLE_CLASS_NAME = "players_table"
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def get_top_players_scores(limit=PLAYERS_LIMIT, table_class_name=TABLE_CLASS_NAME):
url = "https://nba.hupu.com/stats/players"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
players = []
scores = []
table = soup.find("table", class_=table_class_name)
rows = table.find_all("tr")
for row in rows[1:limit+1]:
cols = row.find_all("td")
player = cols[1].text.strip()
score_range = cols[4].text.strip()
score_parts = score_range.split("-")
min_score = float(score_parts[0])
max_score = float(score_parts[1])
score = int((min_score + max_score) / 2)
players.append(player)
scores.append(score)
return players, scores
def plot_top_players_scores(players, scores):
data = {"Player": players, "Score": scores}
df = pd.DataFrame(data)
fig, ax = plt.subplots(figsize=(12, 6))
ax.bar(players, scores, color='green', alpha=0.6)
ax.set_xlabel('球员', fontsize=12)
ax.set_ylabel('得分', fontsize=12)
ax.set_title('NBA球员得分', fontsize=14)
plt.xticks(rotation=45, ha='right', fontsize=8)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
for i, score in enumerate(scores):
ax.text(i, score+0.5, str(score), ha='center', va='bottom')
writer = pd.ExcelWriter('plot_top_players_scores.xlsx')
df.to_excel(writer, index=False)
writer.save()
fig.tight_layout()
plt.show()
if __name__ == "__main__":
players, scores = get_top_players_scores()
plot_top_players_scores(players, scores)
```
这样修改后,Excel文件的写操作只会在循环结束后执行一次,不会出现损坏的问题。
%% MATLAB自带kmeans函数 subplot(2,1,2) %画子图,在这里是一图上可画两张子图 cluster_num=4; %自定义分类数 [index_km,center_km]=kmeans(data,cluster_num) ;%MATLAB自带kmeans函数 a=unique(index_km); %找出分类出的个数 C=cell(1,length(a)); for i=1:length(a) C(1,i)={find(index_km==a(i))}; end for j=1:cluster_num data_get=data(C{1,j},:); scatter(data_get(:,1),data_get(:,2),100,'filled','MarkerFaceAlpha',.6,'MarkerEdgeAlpha',.9); hold on end plot(center_km(:,1),center_km(:,2),'kd','LineWidth',2); hold on sc_k=mean(silhouette(data,index_km)); title_str1=['MATLAB自带kmeans函数',' 聚类数为:',num2str(cluster_num),' SC轮廓系数:',num2str(sc_k)]; title(title_str1) [index_km, center_km] = kmeans(data, cluster_num);disp('最终聚类中心:');disp(center_km);在这个代码中将聚类中心输出为一个数据矩阵X
在这个代码中,聚类中心已经被存储在了变量 center_km 中,它是一个 cluster_num 行,每行包含两个元素的矩阵,其中每个元素表示一个聚类中心在二维平面上的坐标。所以,如果你想将聚类中心输出为一个数据矩阵 X,可以使用下面的代码:
```
X = center_km;
```
执行这行代码后,变量 X 就包含了聚类中心的坐标信息。
阅读全文