[英]Plot through the loop with separate color
我有一個像這樣的數據框:
clusters = pd.concat(l,keys=range(len(l)))
clusters =
latitude longitude
0 872 11.1248 2.5902
873 11.1246 2.5908
874 11.1230 2.5944
875 11.1228 2.5943
876 11.1157 2.5903
... ... ... ...
3 76 11.1610 2.7873
1226 11.1024 2.7498
1227 11.1027 2.7500
1228 11.1072 2.7568
1229 11.1076 2.7563
其中0、1、2、3(總共4個群集)顯示了我的群集。
我想按以下方式在一個循環(可能有一個循環)中繪制所有群集:
0
,以灰色繪制1,2,3
1
繪制為紅色,將群集0,2,3
為灰色 2
,用灰色繪制群集1,0,3
3
繪制為紅色,將群集1,2,0
繪制為灰色 就像是
x_test = ready_couples.loc[0].longitude
y_test = ready_couples.loc[0].latitude
x_all = ready_couples.longitude
y_all = ready_couples.latitude
plt.style.use('seaborn-darkgrid')
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
for k in ready_couples:
plt.scatter(x_all, y_all, marker='.', color='grey', linewidth=1, alpha=0.4)
plt.scatter(x_test, y_test, marker='.', color='orange', linewidth=4, alpha=0.7)
您可以嘗試如下操作:
import pandas as pd
import matplotlib.pyplot as plt
clusters = pd.DataFrame({
'mi': [0, 0, 0, 1, 1, 1],
'i': [1, 2, 3, 4, 5, 6],
'latitude': [1, 2, 4, 4, 5, 5],
'longitude': [1, 4, 5, 4, 5, 5]
})
# you can reset index if needed
# clusters = clusters.reset_index()
clusters = clusters.set_index(['mi'])
plt.plot(clusters.loc[index_in_red,'latitude'], clusters.loc[index_in_red,'longitude'], 'r')
plt.plot(clusters.loc[clusters.index != index_in_red,'latitude'], clusters.loc[clusters.index != index_in_red,'longitude'], 'grey')
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.