通过单独的颜色绘制循环

时间:2019-09-18 11:15:21

标签: python pandas plot

我有一个像这样的数据框:

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绘制为灰色

类似

this

我的尝试:

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)

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

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()