簡體   English   中英

Matplotlib散點圖現在需要三個參數?

[英]Matplotlib scatter now takes three arguments?

我寫了一個簡短的腳本來顯示一些圖形:

if __name__=='__main__':
    p1 = gen_spiral(label=0, dt=0,     n_samples=100, )
    p2 = gen_spiral(label=1, dt=np.pi, n_samples=100, )
    print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("p1",   type(p1),   str(np.shape(p1)),   len(p1),    str(p1)))
    print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("p2",   type(p2),   str(np.shape(p2)),   len(p2),    str(p2)))


    a = np.arange(1,20,1) 
    b = np.arange(1,20,1) 
    c = np.arange(0.0, 2.0, 0.01)
    d = np.sin(2*np.pi*c)

    fig1 = plt.figure()
    ax1 = fig1.add_subplot(121)
    ax1.scatter(a,b) 
    ax2 = fig1.add_subplot(122)
    ax2.scatter(c,d) 

而且效果很好。 但是,當我更改功能以顯示我的數據時:

fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax1.scatter(p1)

它給了我一個不應該存在的錯誤:

Traceback (most recent call last):
  File "Theano--PlotSet--ME01.py", line 53, in <module>
    ax1.scatter(p1) 
TypeError: scatter() takes at least 3 arguments (2 given)

這是不正確的:Scatter接受3個參數,而p1有兩個部分:

    jason@jason-HP-43299:~/Programs/MachineLearning/SectionOne$ python TheanoPS00.py
    Array:   p1
    Type:   <type 'numpy.ndarray'>
    Shape:   (100, 2)
    Length:   100
    Data:   [[-0.0617  0.0534]
     [ 0.0299  0.0913]
     [ 0.0094  0.157 ]
     [ 0.1057  0.1535]
     [ 0.1412  0.2741]
     [ 0.0851  0.1426]

這里到底發生了什么?

如果p1有兩個部分,則需要按照規范要求將每個部分作為單獨的位置參數傳遞

您看到的這個TypeError嚴格地檢查了源代碼中定義的參數數量,並將其與您對函數的使用進行了比較。

大概您想要shape的x和y,因此您可以執行以下操作:

ax1.scatter(p1.shape[0], p1.shape[1])

您在此處調用的scatter函數是軸的類方法。 它的簽名是
scatter(self, x, y, *args, **kwargs) ,具有3個位置參數,類參數self是第一個,您可以通過將其作為軸實例的方法來隱式提供。

知道這一點可以將錯誤消息轉換為“ scatter()接受至少2個用戶指定的參數(給定1個)” 這是有道理的,因為scatter(p1)使用1個參數。

您需要做的就是將數組分成xy數組,

ax1.scatter(p1[:,0], p1[:,1])

暫無
暫無

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

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