2011-11-12 122 views
1

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

和第二誤差是通過使用另一數據集凸起。我正在檢查數據...對於之前不清楚的描述感到抱歉。

+0

對於第二個問題(一個導致'設置有sequence'數組),它會幫助你編輯這個問題顯示了xRa和yRa的內容或內容的一些子集。 –

+1

你爲什麼使用'scatter'?散點圖旨在繪製3維或4維的二維數據(通過改變顏色和/或大小)。你只需要'plot'。除此之外,看起來您會遺漏大量代碼來生成您在圖像中顯示的圖形。您所使用的代碼片段(除了@David的'linestyle ='o''部分已經指出)。你正在拋出導致問題的部分代碼。 –

+0

謝謝你的觀點。其實我正在意識到我看到的情節,我只是猜測它正在使用分散。是的,你是對的,情節可以顯示完全一樣後,我嘗試,因爲傳說出現兩個點,而不是三個使用分散。然而,我仍然好奇爲什麼在我的散點圖中沒有任何東西...... – AnneS

回答

2

如果仔細觀察數據,您會發現大部分數據點都不在繪圖區域內(x = [1e2:1e3]; y = [1e2:1e4])。如果您使用線性比例而不是對數比例,則會看到一些散點,但不是非常可讀的方式。但是,如果您將比例更改爲「symlog」,然後使用「xlim」和「ylim」設置x和y限制,則完成。當然,運行'散射'時,你必須確保標記設置爲'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

3

問題出在linestyle='o''o'指令是,而不是linestyle,因此在前兩行代碼中將linestyle替換爲。

您可以看到scatterhere的標記完整列表。

+0

感謝您的回答。我試了一下,然後又轉向第二個錯誤。我已經發布了xRa和yRa的內容。你能幫我找到錯誤嗎? :) – AnneS

+1

@AnneS使用您添加到問題中的數據,我無法重現您爲問題的第二部分獲得的異常。在你的問題中對'scatter'的調用會傳遞'xRa'和'yRa',但你編輯時包含'xActA'和'yActA'。這兩組變量是相同的嗎? –

+0

對不起,我使用了不同的名字。是的,這兩組變量是相同的。 – AnneS