Sun.csim.scu.edu.tw



Data Visualization 教材江清水編2017/4/10參考教材: from webGenerating Arrays (Lists)from numpy import *1.1 linspace# x = linspace(low, high, n) # 由low到high等分成n份n+1個點。回傳# arrayx = linspace(0, 10, 11)arangex = arange(7)x = arange(7.0)x = arange(3, 7, 1.3)Import Data using pandas (from excel)2.1 read_excelfrom pandas import read_excelexcel_data = read_excel(‘path’)excel_values = excel_data.valuesexcel_vales[:4, :2]open = excel_values[:, 1]2.2 整理資料(將所有與工研院相關的整理出來,只要年度、排名、與發明數)rL = []for i in excel_values: if i[3]=='財團法人工業技術研究院': rL.append([i[0], i[1], i[4]])print(rL)2.3 找出要製作bar chart的兩軸資料xL = []yL = []for i in excel_values: if i[3]=='財團法人工業技術研究院': xL.append(i[0]) yL.append(i[4])for i in range(len(xL)): xL[i]=int(xL[i])xL.reverse()yL.reverse()print(xL)print(yL)2.3 製作bar chartimport matplotlib.pyplot as plt; #plt.rcdefaults()import numpy as np# Example datayear =xLy_pos = np.arange(len(year))np.random.rand(len(year))performance = yL#5.1.1. vertical bar chartplt.bar(y_pos, performance, align='center', alpha=0.4)plt.ylim(0, 950)plt.xticks(y_pos, year)xlrd 來read excelwrite to excel openyxlexporting data to text filesExample:2.5.1 下載資料(經濟部智財局)2.5.2 整理資料(將工研院的資料收集整理)2.5.3 整理後放回excel檔案2.5.4 將資料以圖檔印出來Numeric Limits (from numpy import *)3.1 inf(; exp(710))x = inftry: 1+x; 1-x; x-1; 1*x; 0*x; 1/x; x/x; x/-x;3.2 nan: not a numbertry: 1+x; 1-x; 1*x; 0*x; 1/x;3.3 floating point and precision (python)upper limits: 1.7976e(308), finfo(float).max, next number is inf.x = finfo(float).maxx*2 # inflower limit: -1.7976e(308), finfo(float).minx = finfo(float).minx/2**52; x/2**53smallest positive number: 2.2250e(-308)Relative Precision:finfo(float).eps=2.2204e(-16)eps = finfo(float).epsx = 1.0x = x + eps/2print(x)x = x – 1print(x)x = 1 + 2*eps (or x = 1+*eps)print(x==1)x = 10x = 10 + 2* epsprint(x)(1e120-1e103) == 1e1201e103/1e120eps = finfo(float).epsprint(eps/10==0)print(1+eps-1 == 0)print(1 – eps/10 – 1 == 1 – 1 – eps/10)print(0.1 == 0.1 + eps/10)Numerical Errortotal = 0for i in range(10):total = total + 0.1print(total == 1.0)5 Datas and Times5.1 設定import datetime as dtyr, mo, dd = 2017, 4, 11dt.date(yr, mo, dd)hr, mm, ss, ms = 16, 30, 30, 50dt.time(hr, mm, ss, ms)dt.datetime(yr, mo, dd, hr, mm, ss, ms)5.2 datas MathematicsD1 = dt.datetime(yr, mo, dd, hr, mm, ss, ms)D2 = dt.datetime(yr, mo+2, dd, hr, mm, ss, ms)D2-D1 # return (days, seconds, and microsecond)5.3 current timed.datetime.now().date() # return (yr, mo, dd)d.datetime.now().time() # return(hr, mm, ss, ms)5.4 How long your program takesfrom time import *start = time()elapsed = time() – startprint(elapsed)# Test datadef fib(n): if n == 0: return 0 elif: return 1 else: return f(n-1)+f(n-2)for i in range(20, 30):start = time()fib(i)elapsed = time() – startprint(‘fib(‘,I,’) takes’, elapsed, ‘seconds’)Chart5.1 bar chartimport matplotlib.pyplot as plt; #plt.rcdefaults()import numpy as np# Example datayear = ('2010','2011', '2012', '2013', '2014')y_pos = np.arange(len(year))np.random.rand(len(year))performance = (79.18, 79.15, 79.51, 80.02, 79.84)#5.1.1. vertical bar chartplt.bar(y_pos, performance, align='center', alpha=0.4)plt.ylim(78, 81)plt.xticks(y_pos, year)#5.1.2 horizontal bar chartplt.barh(y_pos, performance, align='center', alpha=0.4)plt.xlim(78, 81)plt.yticks(y_pos, year)#5.1.3 horizontal bar chart with error bounderror = np.random.rand(len(year)) * 0.1plt.barh(y_pos, performance, xerr = error, align='center', alpha=0.4)plt.xlim(78, 81)plt.yticks(y_pos, year)plt.xlabel('Year')plt.title('Expected Life period for the baby born in specified year')plt.show()#5.1.4 stacked bar chartimport numpy as npimport matplotlib.pyplot as pltN = 5menMeans = (20, 35, 30, 35, 27)womenMeans = (25, 32, 34, 20, 25)menStd = (2, 3, 4, 1, 2)womenStd = (3, 5, 2, 3, 3)ind = np.arange(N) # the x locations for the groupswidth = 0.35 # the width of the bars: can also be len(x) sequencep1 = plt.bar(ind, menMeans, width, color='r', yerr=menStd)p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=womenStd)plt.ylabel('Scores')plt.title('Scores by group and gender')plt.xticks(ind + width/2., ('G1', 'G2', 'G3', 'G4', 'G5'))plt.yticks(np.arange(0, 81, 10))plt.legend((p1[0], p2[0]), ('Men', 'Women'))plt.show()#5.1.5 group bar chartimport numpy as npimport matplotlib.pyplot as pltN = 5menMeans = (20, 35, 30, 35, 27)menStd = (2, 3, 4, 1, 2)ind = np.arange(N) # the x locations for the groupswidth = 0.35 # the width of the barsfig, ax = plt.subplots()rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)womenMeans = (25, 32, 34, 20, 25)womenStd = (3, 5, 2, 3, 3)rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd)# add some text for labels, title and axes ticksax.set_ylabel('Scores')ax.set_title('Scores by group and gender')ax.set_xticks(ind + width)ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))def autolabel(rects): # attach some text labels for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, '%d' % int(height), ha='center', va='bottom')autolabel(rects1)autolabel(rects2)plt.show()pie chartimport matplotlib.pyplot as plt# The slices will be ordered and plotted counter-clockwise.labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'sizes = [15, 30, 45, 10]colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%.1f%%', shadow=True, startangle=90)# Set aspect ratio to be equal so that pie is drawn as a circle.plt.axis('equal')plt.show() ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download