Python資料視覺效果——散佈圖

來源:互聯網
上載者:User

標籤:python   scatter   legend   數組串連   

PS: 翻了翻草稿箱, 發現居然存了一篇去年2月的文章。。。雖然naive,還是發出來吧。。。


本文記錄了python中的資料視覺效果——散佈圖scatter,

令x作為資料(50個點,每個30維),我們僅可視化前兩維。labels為其類別(假設有三類)。

這裡的x就用random來了,具體資料具體分析。

label設定為[1:20]->1, [21:35]->2, [36:50]->3,(python中數組串連方法:先強制轉為list,用+,再轉回array)

用matplotlib的scatter繪製散佈圖,legend和matlab中稍有不同,詳見代碼。


x = rand(50,30)from numpy import *import matplotlibimport matplotlib.pyplot as plt#basicf1 = plt.figure(1)plt.subplot(211)plt.scatter(x[:,1],x[:,0])# with labelplt.subplot(212)label = list(ones(20))+list(2*ones(15))+list(3*ones(15))label = array(label)plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)# with legendf2 = plt.figure(2)idx_1 = find(label==1)p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = ‘x‘, color = ‘m‘, label=‘1‘, s = 30)idx_2 = find(label==2)p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = ‘+‘, color = ‘c‘, label=‘2‘, s = 50)idx_3 = find(label==3)p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = ‘o‘, color = ‘r‘, label=‘3‘, s = 15)plt.legend(loc = ‘upper right‘)


result:

figure(1):


figure(2):






Python資料視覺效果——散佈圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.