簡體   English   中英

繪制散點圖時出現的問題

[英]Problems when drawing scatter plot

我正在使用 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')

並且沒有錯誤,但該圖不包含任何點。

在此處輸入圖片說明

這是數據:

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

第二個錯誤是通過使用另一個數據集引發的。 我正在檢查數據...抱歉之前描述的不清楚。

問題在於linestyle='o' 'o'指令是一個marker ,而不是一個linestyle ,所以在你的前兩行代碼中用marker替換linestyle

您可以在此處查看scatter標記的完整列表。

如果仔細查看數據,您會注意到大部分數據點都落在繪圖區域之外 (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')

這幾行代碼為您提供了這個簡潔的數字: 結果
(來源: nublia.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM