2016-08-25 70 views
0

我有一个x列表和y值列表。我想在下面的图片来构造Matplotlib散点图,并根据其x和y坐标划分点分为五大类,如:python matplotlib将scatterplot分成基于斜率的类别

enter image description here

angles = [0, 18, 36, 54, 72, 90] 
colors = ['r','g','b','c'] 
x = [....] 
y = [....] 
所有点的

在分类别将是相同的颜色。拥有类别的图例也很棒。我是Matplotlib和Python的新手,有谁知道我可以如何处理这个问题?

+0

要获取有关散点图多种颜色,你必须调用'plt.scatter()'为每个新的颜色。您的x-y值是否按特定顺序排列? –

+0

这些值未按任何特定顺序排列。我已经尝试过使用plt.scatter(),但我不确定如何划分图像中描绘的点。 –

+0

你可以发布x和y的值吗? –

回答

1

这里的工作的例子,这将给你一个小想法开始:

from matplotlib import pyplot as plt 
from matplotlib.lines import Line2D 

import math 
import random 

fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1) 

w, h = 7, 5 
a = [[random.random() * w, random.random() * h] for i in range(100)] 
plt.plot(*zip(*a), marker='o', color='r', ls='') 

for deg in [18, 36, 54]: 
    r = 10 
    line = Line2D([0, r * math.cos(math.radians(deg))], 
        [0, r * math.sin(math.radians(deg))], 
        linewidth=1, linestyle="-", color="green") 
    ax.add_line(line) 

ax.set_xlim(0, w) 
ax.set_ylim(0, h) 
plt.legend() 
plt.show() 
+0

谢谢。我如何补偿x和y坐标之间的比例差异?我的坐标大多是十进制值,而y是大数字。在这些情况下角度不匹配。 –