반응형

/*********************************************************************************************************
-- Title : [Py2.7] POLT, BAR, HISTOGRAM Chart w/ Matplotlib.pyplot
-- Reference : pythonprogramming.net
-- Key word : plot bar histogram hist 그래프 히스토그램 막대 그래프 선그래프 matplotlib pyplot graph

                  chart 차트
*********************************************************************************************************/


-- Figure



-- Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
# -*- coding: utf-8 -*-
 
import matplotlib.pyplot as plt
 
# ********************************************
# -- PLOT Chart : 출력
# ********************************************
plt.plot([1,2,3], [5,7,4])
plt.show()
 
 
# ********************************************
# -- PLOT Chart : 라벨 및 타이틀 출력
# ********************************************
= [1,2,4]
= [3,7,8]
 
plt.plot(x,y)
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Adding\nLebel and Title")
 
plt.show()
 
 
# ********************************************
# -- PLOT Chart : 범례 출력
# ********************************************
x1 = [1,2,4]
y1 = [3,7,8]
x2 = [1,2,3]
y2 = [10,14,12]
 
plt.plot(x1, y1, label = 'first line')
plt.plot(x2, y2, label = 'second line')
 
plt.title("Adding Legend")
plt.legend()
plt.show()
 
 
# ********************************************
# -- BAR Chart : 출력
# ********************************************
= [2,4,6,8,10]
= [6,7,8,2,4]
 
plt.bar(x,y, label="bar label")
plt.show()
 
 
# ********************************************
# -- BAR Chart : 색상 출력
# ********************************************
= [2,4,6,8,10]
= [6,7,8,2,4]
 
x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]
 
plt.bar(x,y, label="bars1 label", color="blue")
plt.bar(x2,y2, label="bars2 label", color="#FF6A00")
 
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Bar Graph Title")
plt.legend()
plt.show()
 
 
# ********************************************
# -- BAR Chart : X축 생성
# ********************************************
population_ages=[22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48]
 
ids = [x for x in range(len(population_ages))]   # X축 자동 생성
plt.bar(ids, population_ages)
 
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Bar Graph Title")
plt.show()
 
 
# ********************************************
# -- HISTOGRAM Chart : 출력
# ********************************************
population_ages=[22,55,62,45,21,22,34,42,42,4,99,102,110,15,121,25,135,90,115,50,80,75,65,54,44,43,42,48]
 
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
 
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)    # histtype : bar 그래프 출력
#plt.hist(population_ages, bins, histtype='step', rwidth=0.8)   # histtype : 계단형 그래프 출력
#plt.hist(population_ages, bins, histtype='stepfilled', rwidth=0.8)   # histtype : 채워진 계단형 그래프 출력
 
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Histogram Graph Title")
plt.show()
 
 
# ********************************************
# -- HISTOGRAM Chart : 옵션 조정
# ********************************************
population_ages=[22,55,62,45,21,22,34,42,42,4,99,102,110,15,121,25,135,90,115,50,80,75,65,54,44,43,42,48]
 
#plt.hist(population_ages, bins=10, histtype='bar', rwidth=0.8)   # 10단계로 구분
#plt.hist(population_ages, bins=10, histtype='bar', rwidth=0.8, normed=True)  # y축을 비율로?
plt.hist(population_ages, bins=10, histtype='bar', rwidth=0.8, normed=True, cumulative=True)  # y축 누적 비율?
 
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Histogram Graph Title")
plt.show()
 
 
# ********************************************
# -- HISTOGRAM Chart : 옵션 조정
# ********************************************
 
ages1=[22,55,62,45,21,22,34,42,42,4,99,102,110,15,121,25,135,90,115,50,80,75,65,54,44,43,42,48]
ages2=[23,57,23,78,90,23,45,78,23,7893]
 
plt.hist(ages1, bins=10, histtype='stepfilled', rwidth=1, color="red", label="label1")
plt.hist(ages2, bins=10, histtype='bar', rwidth=1, color="blue", label="label2")
 
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.title("Histogram Graph Title")
plt.legend()
plt.show()
 

cs

반응형

+ Recent posts