零基礎學習Python數據分析:數據可視化(1)

大家好,我是就讀於西交利物浦商分專業的Dragee。

因為是零基礎學習數據分析,作為小白。Codecademy持續10周的Introduction to Data Analysis課程非常適合入門。還提供Python代碼的學習,因此我選擇了這個課程。

Introduction to Data Analysis | Codecademy

Introduction to Data Analysiswww.codecademy.com

課程主要分為五個部分,除此之外還有一個Project,售價是199美元。

對於我這樣的初學者還是很實用的。另外,Evan的這篇文章給提供了很多全面的資料和網站作為參考。

Evan:零基礎學習Python數據分析zhuanlan.zhihu.com圖標


Unit 1 用Matplotlib做數據可視化的Basic Command

一、使用Pyplot模塊畫圖

參考資料:matplotlib-user-guide-zh/3.1.md at master · wizardforcel/matplotlib-user-guide-zh · GitHub

wizardforcel/matplotlib-user-guide-zhgithub.com圖標

首先要把pyplot功能導入進來

from matplotlib import pyplot as pltn

用plt.plot()畫圖

用plt.show()語句可以顯示出點圖,如:

from matplotlib import pyplot as pltndays = range(7)nmoney_spent = [10, 12, 12, 10, 14, 22, 24]nplt.plot(days, money_spent)nplt.show()n

圖1

二、使用Python語句設置圖表

在同一組軸上顯示多個線圖用於比較多個Datasets時,Matpltlib會自動用不同顏色標記:

from matplotlib import pyplot as pltntime = [0, 1, 2, 3, 4]nrevenue = [200, 400, 650, 800, 850]ncosts = [150, 500, 550, 550, 560]nplt.plot(time,revenue)nplt.plot(time,costs)nplt.show()n

圖2

當然,我們也可以自己設置顏色(使用HTML color name or HEX code)_Key word:color ;設置是否虛線(dotted or dashed)_Key word:linestyle;設置標記(marker)_Key word:maker。例如:

from matplotlib import pyplot as pltnntime = [0, 1, 2, 3, 4]nrevenue = [200, 400, 650, 800, 850]ncosts = [150, 500, 550, 550, 560]nplt.plot(time,revenue,color=purple,linestyle=--)n#Dashed:linestylex=-- Dotted:linestylex=: No line:linestylex=nplt.plot(time,costs,color=#82edc9,marker=s)n#circle:marker=o square:marker=s star:marker=*nplt.show()n

圖3

就會出現上圖的效果了。

有時候,放大或縮小是很有幫助的,特別是如果有一些細節需要處理的話。放大,我們可以用plt.axis。例如:上面的圖3,我想查看下x從0到4,y從300到400的具體情況,結果如下圖

plt.axis([0,4,300,400]nplt.show()n

如果我們要展示圖表時,就需要給我們的圖表加上標題,給x軸和Y軸加上Label,這時要使用的語句分別是:plt.title和plt.xlabel&plt.ylabel。所有這些命令都需要一個字元串String,它是單引號或雙引號中的一組字元。例如:

"This is a string"

This is also a string

This is NOT a string (the quotes do not match)"

上例中,如果我們想給x添加標籤「Time」,給圖表添加標題「Revenue VS Cost」:

plt.xlabel("Time")nplt.title("Revenue VS Cost")n

那麼,在圖中有多條線的時候,如何標記不同的線,就需要用到plt.legend.例如:

from matplotlib import pyplot as pltnnmonths = range(12)nhyrule = [63, 65, 68, 70, 72, 72, 73, 74, 71, 70, 68, 64]nkakariko = [52, 52, 53, 68, 73, 74, 74, 76, 71, 62, 58, 54]ngerudo = [98, 99, 99, 100, 99, 100, 98, 101, 101, 97, 98, 99]nlegend_labels=[Hyrule,Kakariko,Gerudo Valley]nplt.plot(months, hyrule)nplt.plot(months, kakariko)nplt.plot(months, gerudo)nn#create your legend herenplt.legend(legend_labels,loc=9)nplt.show()n

三、多圖展示

有時候需要將一些相同值域的圖表並排顯示(如圖所示):

這時我們可以使用plt.subpot命令,語句中需包含3項,分別是

The number of rows of subplots,The number of columns of subplots,The index of the subplot we want to create

例如,plt.subplot(2,3,4)指的是上圖的Subplot4。創建每個圖表後我們都可以以此語句指定其位置。我們甚至可以設置圖與圖之間的間隙。此時要用到的語句是plt.subplot_adjust。此命令有一些關鍵參數,分別是:

  • left — the left-side margin, with a default of 0.125. You can increase this number to make room for a y-axis label(左側邊距,默認值為0.125。可以增加這個邊距為y軸標籤騰出空間)
  • right — the right-side margin, with a default of 0.9. You can increase this to make more room for the figure, or decrease it to make room for a legend(右側邊距,默認值為0.9)
  • bottom — the bottom margin, with a default of 0.1. You can increase this to make room for tick mark labels or an x-axis label(底部邊距,默認值為0.1。您可以增加這一邊距以騰出空間的刻度標記或X軸標籤)
  • top — the top margin, with a default of 0.9(頂部邊距,默認值為0.9)
  • wspace — the horizontal space between adjacent subplots, with a default of 0.2(橫向間距,默認值0.2)
  • hspace — the vertical space between adjacent subplots, with a default of 0.2(縱向間距,默認值0.2)

例如☆:

from matplotlib import pyplot as pltnnx = range(7)nstraight_line = [0, 1, 2, 3, 4, 5, 6]nparabola = [0, 1, 4, 9, 16, 25, 36]ncubic = [0, 1, 8, 27, 64, 125, 216]nn# Subplot 1nplt.subplot(2, 1, 1)nplt.plot(x, straight_line)nn# Subplot 2nplt.subplot(2, 2, 3)nplt.plot(x, parabola)nn# Subplot 3nplt.subplot(2, 2, 4)nplt.plot(x, cubic)nnplt.subplots_adjust(wspace=0.35, bottom=0.2)nnplt.show() n

四、標記

如果我們要標記其中的某一個subplot的話,就要用到軸對象axis object的概念,它讓我們修改屬於一個特定的subplot的axis。只有一個圖的情況也是如此,如:

ax = plt.subplot()nplt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])nplt.plot([0, 1, 2, 3, 4], [0, 1, 8, 27, 64])nax.set_xticks([1, 2, 4])n

from matplotlib import pyplot as pltnnmonth_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec"]nnmonths = range(12)nconversion = [0.05, 0.08, 0.18, 0.28, 0.4, 0.66, 0.74, 0.78, 0.8, 0.81, 0.85, 0.85]nnplt.xlabel("Months")nplt.ylabel("Conversion")nnplt.plot(months, conversion)nnax = plt.subplot()nax.set_xticks(months)nax.set_xticklabels(month_names)nax.set_yticks([0.10, 0.25, 0.5, 0.75])nax.set_yticklabels(["10%", "25%", "50%", "75%"])nnplt.show()n

五、清除和保存

清除:

plt.close(all)n

保存:我們可以使用命令plt.savefig儲存成許多不同的文件格式,如PNG,SVG,或PDF。

plt.savefig(name_of_graph.png)n

設置圖片大小:

plt.figure(figsize=(width,height))n

例如:

from matplotlib import pyplot as pltnnword_length = [8, 11, 12, 11, 13, 12, 9, 9, 7, 9]npower_generated = [753.9, 768.8, 780.1, 763.7, 788.5, 782, 787.2, 806.4, 806.2, 798.9]nyears = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]nplt.close(all)nplt.plot(years,word_length)nplt.savefig(winning_word_lengths.png)nnplt.figure(figsize=(7,3))nplt.plot(years,power_generated)nplt.savefig(power_generated.png)n


python是個很好的數據分析工具,不需要學習很多編程語言也可以操作。

因為自己也是在學習中,難免會有一些錯誤,歡迎討論指正

希望大家和我一起來交流學習


推薦閱讀:

如何看待類似《變形金剛》這樣的純以商業為目的的文化現象?
迪拜周專訪 | 阿聯酋落地簽只是一個開始
全面復盤興業太古匯試營業事件
投資網紅的邏輯是什麼?
對自由職業者來說,在自由職業者平台與在自建網站接觸客戶,有哪些利弊?

TAG:数据分析 | 商业分析 | 商业 |