绘制散点图时出现问题

时间:2011-11-12 16:50:23

标签: python matplotlib

我正在使用matplotlib.pyplot.scatter来绘制一些简单的散点图。但是,出现了一些错误,我无法找到解决方案。以下是绘制此散点图的代码:

 # xActA, yActA, xActQ, yActQ are all lists with same dimensions.
ax1 = scatter(xActA, yActA, color = 'blue',s = 20, label = 'Answers', linestyle = 'o')
ax2 = scatter(xActQ, yActQ, color = 'black', s = 20, label = 'Questions', linestyle = 'o')
ax1.set_label('Answers')
ax2.set_label('Questions')
xscale('log')
yscale('log')
title('User activity')
xlabel('Number of posts')
ylabel('Number of users')
legend()
f1.show()
f1.savefig('figure7_test.png')

并且没有错误,但情节不包含任何点。

enter image description here

以下是数据:

xActA = [0, 1, 2, 3, 4, 5, 6, 129, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
             18, 147, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
             35, 36, 39, 40, 7, 45, 46, 49, 50, 52, 53, 183, 59, 63, 65, 69,
             70, 72, 73, 55, 77, 78, 84, 85, 43, 215, 88, 100, 94, 131, 167,
             19, 375, 122, 125, 149]

  len(xActA) = 70

  yActA = [1212, 822, 194, 94, 61, 44, 24, 1, 26, 20, 11, 16, 10, 8, 5, 8,
             5, 5, 3, 1, 4, 4, 5, 3, 2, 3, 4, 3, 1, 2, 2, 3, 2, 1, 2, 2, 2, 2,
             31, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1]

  len(yActA) = 70

  xActQ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23,    24, 25, 29, 36, 40, 45, 48, 50, 55, 67, 124]
  len(xActQ) = 34

  yActQ [204, 242, 150, 50, 49, 27, 5, 9, 4, 2, 6, 3, 2, 8, 4, 5, 1, 3, 3, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  len(yActQ) = 24

使用其他数据集引发第二个错误。我正在检查数据...对于之前不清楚的描述感到抱歉。

2 个答案:

答案 0 :(得分:3)

问题在于linestyle='o''o'指令是marker,而非linestyle,因此请在前两行代码中将linestyle替换为marker

您可以查看scatter here的完整标记列表。

答案 1 :(得分:2)

如果仔细观察数据,您会注意到大多数数据点都落在绘图区域之外(x = [1e2:1e3]; y = [1e2:1e4])。如果使用线性刻度而不是对数刻度,则会看到一些散点,但不是以非常易读的方式。但是,如果你将你的音阶改为'symlog',然后使用'xlim'和'ylim'设置x和y限制,那就完成了。当然,在运行'scatter'时,您必须确保标记设置为'o'。检查完整代码。

from pylab import *

# The data
xActA = array([0, 1, 2, 3, 4, 5, 6, 129, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
    18, 147, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 
    39, 40, 7, 45, 46, 49, 50, 52, 53, 183, 59, 63, 65, 69, 70, 72, 73, 55, 77,
    78, 84, 85, 43, 215, 88, 100, 94, 131, 167, 19, 375, 122, 125, 149])
yActA = array([1212, 822, 194, 94, 61, 44, 24, 1, 26, 20, 11, 16, 10, 8, 5, 8, 
    5, 5, 3, 1, 4, 4, 5, 3, 2, 3, 4, 3, 1, 2, 2, 3, 2, 1, 2, 2, 2, 2, 31, 2, 1,
    1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2,
    1, 1, 1, 1])

xActQ = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 
    19, 20, 21, 22, 23, 24, 25, 29, 36, 40, 45, 48, 50, 55, 67, 124])
yActQ = array([204, 242, 150, 50, 49, 27, 5, 9, 4, 2, 6, 3, 2, 8, 4, 5, 1, 3, 
    3, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

# The plots
close('all')
f1 = figure()
ax1 = scatter(xActA, yActA, color='blue', s=20, label='Answers', marker='o')
ax2 = scatter(xActQ, yActQ, color='black', s=20, label='Questions', marker='o')
xscale('symlog')
yscale('symlog')
xlim([0, 1e3])
ylim([0, 1.5e3])
title('User activity')
xlabel('Number of posts')
ylabel('Number of users')
legend()
f1.show()
f1.savefig('figure7_test.png')

这几行代码为您提供了这个简洁的数字:Results