DAY 20
1
Data Technology

## [Day20]Matplotlib資料視覺化進階！

``````import matplotlib.pyplot as plt
``````

# pie Chart

``````labels = 'A','B','C','D','E','F'
``````

``````size = [33,52,12,17,62,48]
``````

``````plt.pie(size , labels = labels,autopct='%1.1f%%')
``````

size與labels都是上面設定的資料，`autopct='%1.1f%%'`是用來顯示百分比。

``````plt.axis('equal')
``````

``````plt.show()
``````

## Separated

``````separated = (.1,0,0,0,0,0)
``````

``````plt.pie(size , labels = labels,autopct='%1.1f%%',explode=separated)
``````

## With pandas

``````import matplotlib.pyplot as plt
import pandas as pd
``````

``````data = {'names':['a','b','c','d','e'],
'jan':[133,122,101,104,320],
'feb':[122,132,144,98,62],
'march':[64,99,32,12,65] }
``````

``````df = pd.DataFrame(data,columns=['names','jan','feb','march'])
``````

``````df['total'] = df['jan']+ df['feb']+df['march']
``````

``````colors = [(1,.4,.4),(1,.6,1),(.5,.3,1),(.7,.7,.2),(.6,.2,.6)]
``````

``````plt.pie( df['total'] ,
labels = df['names'],
colors = colors,
autopct='%1.1f%%',
)
``````

``````plt.axis('equal')
plt.show()
``````

# Bar Chart

``````import matplotlib.pyplot as plt
``````

## With Numpy

``````import numpy as np
``````

``````col_count = 3
A_scores = (553,536,548)
B_scores = (518,523,523)
C_scores = (613,570,588)
D_scores = (475,505,499)
``````

``````index = np.arange(col_count)
``````

``````k = plt.bar(index,A_scores, .5)
plt.grid(True)
plt.show()
``````

``````col_count = 3
bar_width = 0.2
index = np.arange(col_count)
A_scores = (553,536,548)
B_scores = (518,523,523)
C_scores = (613,570,588)
D_scores = (475,505,499)
``````

``````A = plt.bar(index,
A_scores,
bar_width,
alpha=.4,
label="K")
B = plt.bar(index+0.2,
B_scores,
bar_width,
alpha=.4,
label="C")
C = plt.bar(index+0.4,
C_scores,
bar_width,
alpha=.4,
label="N") # x,y ,width
D = plt.bar(index+0.6,
D_scores,
bar_width,
alpha=.4,
label="F") # x,y ,width
``````

``````def createLabels(data):
for item in data:
height = item.get_height()
plt.text(
item.get_x()+item.get_width()/2.,
height*1.05,
'%d' % int(height),
ha = "center",
va = "bottom",
)
createLabels(A)
createLabels(B)
createLabels(C)
createLabels(D)
``````

How to write text above the bars on a bar plot (Python)?

``````plt.ylabel("Mean score")
plt.xlabel("Subject")
plt.title("Test Scores by Contry")
plt.legend()
plt.grid(True)
plt.show()
``````

`plt.legend()`為右上角的圖。
`plt.xticks`為底下的文字（為了至中所以`+.3 / 2`

ok 這就是今天顯示圖表的部分了！