2012-05-23 31 views

回答

0

如果您有不同類別的數據,可以使用classes

import matplotlib.pyplot as plt 

class DataSet(object): 
    def __init__(self, name, x_data, y_data, size, color): 
     self.name = name 
     self.x_data = x_data 
     self.y_data = y_data 
     self.size = size 
     self.color = color 

wood = DataSet('wood', [1,2,3,4], [5,7,4,1], 300, 'red') 
glass = DataSet('glass', [0,2,0,4], [2,7,4,1], 100, 'blue') 
paper = DataSet('paper', [1,7,3,6], [9,9,1,9], 500, '#a5a5a5') 
plastic = DataSet('plastic', [3,2,5,5], [1,5,1,8], 700, 'yellow') 

categories = [wood, glass, paper, plastic] 
#Here we plot all the data: 
for cat in categories: 
    plt.scatter(cat.x_data, cat.y_data, s=cat.size , color=cat.color, label=cat.name) 

plt.legend(loc='lower right', scatterpoints=1) 
plt.show() 

這給我們:

enter image description here

可能,也可能不會,優雅!