FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vcs/_4.python/matplotlib/matplotlib1_plot6_yy.py at master · bunshue/vcs · GitHub
bunshue
/
vcs
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vcs
/
_4.python
/
matplotlib
/
matplotlib1_plot6_yy.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
343 lines (274 loc) · 6.86 KB
Breadcrumbs
vcs
/
_4.python
/
matplotlib
/
matplotlib1_plot6_yy.py
Copy path
File metadata and controls
343 lines (274 loc) · 6.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
雙y軸
"""
print
(
"------------------------------------------------------------"
)
# 60個
# 共同
import
os
import
sys
import
time
import
math
import
random
import
numpy
as
np
import
pandas
as
pd
import
matplotlib
.
pyplot
as
plt
font_filename
=
"D:/_git/vcs/_1.data/______test_files1/_font/msch.ttf"
# 設定中文字型及負號正確顯示
# 設定中文字型檔
plt
.
rcParams
[
"font.sans-serif"
]
=
"Microsoft JhengHei"
# 將字體換成 Microsoft JhengHei
# 設定負號
plt
.
rcParams
[
"axes.unicode_minus"
]
=
False
# 讓負號可正常顯示
plt
.
rcParams
[
"font.size"
]
=
12
# 設定字型大小
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"使用 plt.subplots() 做一圖"
)
x
=
np
.
linspace
(
0
,
10
,
50
)
y
=
np
.
sin
(
x
)
# fig, ax = plt.subplots() same
fig
,
ax
=
plt
.
subplots
(
1
,
1
)
ax
.
plot
(
x
,
y
,
"r-o"
,
label
=
"Sin(x)"
)
ax
.
set_xlabel
(
"弧度"
,
color
=
"green"
)
ax
.
set_ylabel
(
"Sin(x)"
,
color
=
"red"
)
ax
.
set_title
(
"sin(x)"
,
size
=
20
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
# 共同參數
x
=
np
.
linspace
(
0
,
2
*
np
.
pi
,
30
)
y
=
np
.
sin
(
x
)
y0
=
np
.
sin
(
x
)
y1
=
np
.
cos
(
x
)
y2
=
np
.
tan
(
x
)
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"兩Y軸不同刻度, plot + plot"
)
x
=
np
.
linspace
(
0
,
10
,
50
)
sinus
=
np
.
sin
(
x
)
sinhs
=
np
.
sinh
(
x
)
fig
,
ax
=
plt
.
subplots
()
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
# 第1軸
ax
.
plot
(
x
,
sinus
,
"r-o"
,
label
=
"Sin(x)"
)
ax
.
set_xlabel
(
"弧度"
,
color
=
"green"
)
ax
.
set_ylabel
(
"Sin(x)"
,
color
=
"red"
)
ax
.
legend
(
loc
=
"best"
)
# 第2軸
ax2
.
plot
(
x
,
sinhs
,
"g--"
,
label
=
"Sinh(x)"
)
ax2
.
set_ylabel
(
"Sinh(x)"
,
color
=
"blue"
)
ax2
.
legend
(
loc
=
"best"
)
ax
.
set_title
(
"兩Y軸不同刻度 plot + plot"
,
size
=
20
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
x
=
np
.
linspace
(
0
,
10
,
50
)
sinus
=
np
.
sin
(
x
)
sinhs
=
np
.
sinh
(
x
)
fig
,
ax
=
plt
.
subplots
()
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
lns1
=
ax
.
plot
(
x
,
sinus
,
"r-o"
,
label
=
"Sin(x)"
)
ax
.
set_xlabel
(
"x"
,
color
=
"green"
)
ax
.
set_ylabel
(
"Sin(x)"
,
color
=
"red"
)
lns2
=
ax2
.
plot
(
x
,
sinhs
,
"g--"
,
label
=
"Sinh(x)"
)
ax2
.
set_ylabel
(
"Sinh(x)"
,
color
=
"blue"
)
# 自行建立圖例來顯示所有標籤
lns
=
lns1
+
lns2
labs
=
[
l
.
get_label
()
for
l
in
lns
]
ax
.
legend
(
lns
,
labs
,
loc
=
"best"
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
x
=
np
.
linspace
(
0
,
10
,
50
)
sinus
=
np
.
sin
(
x
)
sinhs
=
np
.
sinh
(
x
)
fig
,
ax
=
plt
.
subplots
()
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
ax
.
plot
(
x
,
sinus
,
"r-o"
)
ax
.
set_xlabel
(
"x"
,
color
=
"green"
)
ax
.
set_ylabel
(
"Sin(x)"
,
color
=
"red"
)
ax2
.
plot
(
x
,
sinhs
,
"g--"
)
ax2
.
set_ylabel
(
"Sinh(x)"
,
color
=
"blue"
)
# 指定圖表標題文字
ax
.
set_title
(
"Sin和Cos三角函數的波型"
,
fontsize
=
"large"
)
# 更改刻度的外觀
for
tick
in
ax
.
xaxis
.
get_ticklabels
():
tick
.
set_fontsize
(
"large"
)
tick
.
set_fontname
(
"Times New Roman"
)
tick
.
set_color
(
"blue"
)
tick
.
set_weight
(
"bold"
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
x
=
np
.
linspace
(
0
,
10
,
50
)
sinus
=
np
.
sin
(
x
)
sinhs
=
np
.
sinh
(
x
)
fig
,
ax
=
plt
.
subplots
()
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
ax
.
plot
(
x
,
sinus
,
"r-o"
)
ax
.
set_xlabel
(
"x"
,
color
=
"green"
)
ax
.
set_ylabel
(
"Sin(x)"
,
color
=
"red"
)
ax2
.
plot
(
x
,
sinhs
,
"g--"
)
ax2
.
set_ylabel
(
"Sinh(x)"
,
color
=
"blue"
)
plt
.
xticks
(
range
(
0
,
11
))
ax
.
set_yticks
(
np
.
linspace
(
-
1
,
1
,
10
))
ax2
.
set_yticks
(
np
.
linspace
(
0
,
12000
,
10
))
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"兩Y軸不同刻度, plot + bar"
)
import
matplotlib
.
gridspec
as
gridspec
x
=
np
.
linspace
(
0
,
6.28
,
10
)
y
=
np
.
sin
(
x
*
2
)
y2
=
np
.
sin
(
x
*
2
)
*
np
.
sin
(
x
*
2
)
*
10
fig
=
plt
.
figure
(
figsize
=
(
12
,
8
))
# 圖像大小[英吋]
gs
=
gridspec
.
GridSpec
(
4
,
1
,
figure
=
fig
)
ax
=
fig
.
add_subplot
()
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
ax
.
plot
(
x
,
y
,
marker
=
""
,
alpha
=
0.8
)
ax
.
grid
(
20
)
ax2
.
bar
(
x
,
y2
,
alpha
=
0.2
,
label
=
"hold_volume"
,
color
=
"pink"
,
)
ax
.
set_title
(
"兩Y軸不同刻度 plot + bar"
,
size
=
20
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
# 新竹市平均高溫 °C
temperature_high
=
[
19.1
,
19.4
,
21.6
,
25.6
,
28.9
,
31.5
,
33.2
,
32.8
,
31.2
,
28
,
25.1
,
21.1
,
26.5
,
]
# 新竹市日均氣溫 °C
temperature_average
=
[
15.7
,
16
,
18
,
21.9
,
25.2
,
27.9
,
29.3
,
28.9
,
27.3
,
24.4
,
21.5
,
17.7
,
22.8
,
]
# 新竹市平均低溫 °C
temperature_low
=
[
13.1
,
13.4
,
15.2
,
18.9
,
22.2
,
24.9
,
26
,
25.8
,
24.4
,
21.8
,
18.8
,
15.1
,
20
,
]
# 新竹市平均降雨量 mm
rainfall_average
=
[
75.7
,
123
,
159.8
,
161.9
,
249
,
252
,
120.2
,
197.1
,
174.5
,
53.6
,
51.1
,
57.7
,
1675.6
,
]
print
(
len
(
temperature_high
))
print
(
len
(
temperature_average
))
print
(
len
(
temperature_low
))
print
(
len
(
rainfall_average
))
x
=
np
.
arange
(
0
,
12
,
1
)
print
(
len
(
x
))
print
(
x
)
fig
,
ax1
=
plt
.
subplots
()
ax2
=
ax1
.
twinx
()
# 使用相同的 x 軸
ax1
.
plot
(
x
,
temperature_high
[:
12
],
"r-"
)
ax1
.
plot
(
x
,
temperature_average
[:
12
],
"g-"
)
ax1
.
plot
(
x
,
temperature_low
[:
12
],
"b-"
)
ax1
.
set_xlabel
(
"月份"
)
ax1
.
set_ylabel
(
"氣溫"
)
ax2
.
plot
(
x
,
rainfall_average
[:
12
],
"yellow"
)
ax2
.
set_ylabel
(
"雨量"
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
dists
=
{
"區名"
: [
"中正區"
,
"板橋區"
,
"桃園區"
,
"北屯區"
,
"安南區"
,
"三民區"
,
"大安區"
,
"永和區"
,
"八德區"
,
"前鎮區"
,
"鳳山區"
,
"信義區"
,
"新店區"
,
],
"人口"
: [
159598
,
551452
,
441287
,
275207
,
192327
,
343203
,
309835
,
222531
,
198473
,
189623
,
359125
,
225561
,
302070
,
],
"面積"
: [
7.6071
,
23.1373
,
34.8046
,
62.7034
,
107.2016
,
19.7866
,
11.3614
,
5.7138
,
33.7111
,
19.1207
,
26.7590
,
11.2077
,
120.2255
,
],
}
df
=
pd
.
DataFrame
(
dists
,
columns
=
[
"人口"
,
"面積"
],
index
=
dists
[
"區名"
])
print
(
df
)
fig
,
ax
=
plt
.
subplots
()
fig
.
suptitle
(
"分區統計"
)
ax
.
set_ylabel
(
"人口"
)
ax
.
set_xlabel
(
"分區"
)
ax2
=
ax
.
twinx
()
# 使用相同的 x 軸
ax2
.
set_ylabel
(
"面積"
)
df
[
"人口"
].
plot
(
ax
=
ax
,
style
=
"b--o"
,
use_index
=
True
,
rot
=
90
)
df
[
"面積"
].
plot
(
ax
=
ax2
,
style
=
"g-s"
,
use_index
=
True
,
rot
=
90
)
plt
.
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"作業完成"
)
print
(
"------------------------------------------------------------"
)
# 60個
Back
|
FazBrowse Home
|
New Git URL