iT邦幫忙

2024 iThome 鐵人賽

DAY 27
0
Python

Python和R入門語法比較系列 第 27

14 [Python] for迴圈 和 matplotlib.pyplot 畫線圖 [16th 鐵人 Day 27]

  • 分享至 

  • xImage
  •  

Windows作業系統

Mac作業系統

for 迴圈

假設我們要列出如下圖的樣子:

2024/09/01
2024/09/02
2024/09/03
2024/09/04
2024/09/05

使用print()

print('2024/09/0',1)
print('2024/09/0',2)
print('2024/09/0',3)
print('2024/09/0',4)
print('2024/09/0',5)
    2024/09/0 1
    2024/09/0 2
    2024/09/0 3
    2024/09/0 4
    2024/09/0 5

使用print(, sep=' ')

print('2024/09/0',1, sep='')
print('2024/09/0',2)
print('2024/09/0',3)
print('2024/09/0',4)
print('2024/09/0',5)
    2024/09/01
    2024/09/0 2
    2024/09/0 3
    2024/09/0 4
    2024/09/0 5
print('2024/09/0',1, sep='')
print('2024/09/0',2, sep='')
print('2024/09/0',3, sep='')
print('2024/09/0',4, sep='')
print('2024/09/0',5, sep='')
    2024/09/01
    2024/09/02
    2024/09/03
    2024/09/04
    2024/09/05

使用for 迴圈 way 1: for i in range( 次數 )

for i in range(5):
    print(i)
    0
    1
    2
    3
    4

使用for 迴圈 way 1: for i in range( 次數+1 )

for i in range(5+1):
    print(i)
    0
    1
    2
    3
    4
    5

使用for 迴圈 way 1: for i in range(起點, 次數+1 )

for i in range(1,5+1):
    print(i)
    1
    2
    3
    4
    5

使用for 迴圈 way 1: for i in range(起點, 次數+1 )

: print( )

for i in range(1, 5+1):
    print('2024/09/0',i, sep='')
    2024/09/01
    2024/09/02
    2024/09/03
    2024/09/04
    2024/09/05

datatime 格式

import datetime as df
d = df.datetime(2024,9,1)
d.strftime('%Y/%m/%d')
    '2024/09/01'

讀檔

'data/volcano.csv'

import pandas as pd
with open('data/volcano_utf8.csv', encoding='utf8') as f:
    p = pd.read_csv(f)
p

https://ithelp.ithome.com.tw/upload/images/20240923/20162398MYWej3tFAU.png

看有哪些欄位

p.columns
    Index(['日期', '數量'], dtype='object')

載入 matplotlib.pyplot

math plot library

import matplotlib.pyplot as plt

畫線圖

plt.plot(x, y)

plt.plot(p['日期'],p['數量'])
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240923/20162398Vrn2ygeuoH.png

看日期欄的型態

p['日期'].dtypes
    dtype('int64')

datetime 處理日期

import datetime as dt

使用 for 迴圈 way 2: for i in list/Series:

取出表格p裡面的日期欄

p['日期']
    0      20230101
    1      20230102
    2      20230103
    3      20230104
    4      20230105
             ...   
    299    20231027
    300    20231028
    301    20231029
    302    20231030
    303    20231031
    Name: 日期, Length: 304, dtype: int64

確定日期欄是一條Series

type(p['日期'])
    pandas.core.series.Series

strptime: string to datetime格式

df.datetime.strptime('2024/09/01', "%Y/%m/%d")
    datetime.datetime(2024, 9, 1, 0, 0)
d=[]
for i in p['日期']:
    i = str(i)
    d.append(dt.datetime.strptime(i, "%Y%m%d"))
d
    [datetime.datetime(2023, 1, 1, 0, 0),
     datetime.datetime(2023, 1, 2, 0, 0),
     datetime.datetime(2023, 1, 3, 0, 0),
     datetime.datetime(2023, 1, 4, 0, 0),
     datetime.datetime(2023, 1, 5, 0, 0),
     datetime.datetime(2023, 1, 6, 0, 0),
     datetime.datetime(2023, 1, 7, 0, 0),
     datetime.datetime(2023, 1, 8, 0, 0),
     datetime.datetime(2023, 1, 9, 0, 0),
     datetime.datetime(2023, 1, 10, 0, 0),
     datetime.datetime(2023, 1, 11, 0, 0),
     datetime.datetime(2023, 1, 12, 0, 0),
     datetime.datetime(2023, 1, 13, 0, 0),
     datetime.datetime(2023, 1, 14, 0, 0),
     datetime.datetime(2023, 1, 15, 0, 0),
     datetime.datetime(2023, 1, 16, 0, 0),
     datetime.datetime(2023, 1, 17, 0, 0),
     datetime.datetime(2023, 1, 18, 0, 0),
     datetime.datetime(2023, 1, 19, 0, 0),
     datetime.datetime(2023, 1, 20, 0, 0),
     datetime.datetime(2023, 1, 21, 0, 0),
     datetime.datetime(2023, 1, 22, 0, 0),
     datetime.datetime(2023, 1, 23, 0, 0),
     datetime.datetime(2023, 1, 24, 0, 0),
     datetime.datetime(2023, 1, 25, 0, 0),
     datetime.datetime(2023, 1, 26, 0, 0),
     datetime.datetime(2023, 1, 27, 0, 0),
     datetime.datetime(2023, 1, 28, 0, 0),
     datetime.datetime(2023, 1, 29, 0, 0),
     datetime.datetime(2023, 1, 30, 0, 0),
     datetime.datetime(2023, 1, 31, 0, 0),
     datetime.datetime(2023, 2, 1, 0, 0),
     datetime.datetime(2023, 2, 2, 0, 0),
     datetime.datetime(2023, 2, 3, 0, 0),
     datetime.datetime(2023, 2, 4, 0, 0),
     datetime.datetime(2023, 2, 5, 0, 0),
     datetime.datetime(2023, 2, 6, 0, 0),
     datetime.datetime(2023, 2, 7, 0, 0),
     datetime.datetime(2023, 2, 8, 0, 0),
     datetime.datetime(2023, 2, 9, 0, 0),
     datetime.datetime(2023, 2, 10, 0, 0),
     datetime.datetime(2023, 2, 11, 0, 0),
     datetime.datetime(2023, 2, 12, 0, 0),
     datetime.datetime(2023, 2, 13, 0, 0),
     datetime.datetime(2023, 2, 14, 0, 0),
     datetime.datetime(2023, 2, 15, 0, 0),
     datetime.datetime(2023, 2, 16, 0, 0),
     datetime.datetime(2023, 2, 17, 0, 0),
     datetime.datetime(2023, 2, 18, 0, 0),
     datetime.datetime(2023, 2, 19, 0, 0),
     datetime.datetime(2023, 2, 20, 0, 0),
     datetime.datetime(2023, 2, 21, 0, 0),
     datetime.datetime(2023, 2, 22, 0, 0),
     datetime.datetime(2023, 2, 23, 0, 0),
     datetime.datetime(2023, 2, 24, 0, 0),
     datetime.datetime(2023, 2, 25, 0, 0),
     datetime.datetime(2023, 2, 26, 0, 0),
     datetime.datetime(2023, 2, 27, 0, 0),
     datetime.datetime(2023, 2, 28, 0, 0),
     datetime.datetime(2023, 3, 1, 0, 0),
     datetime.datetime(2023, 3, 2, 0, 0),
     datetime.datetime(2023, 3, 3, 0, 0),
     datetime.datetime(2023, 3, 4, 0, 0),
     datetime.datetime(2023, 3, 5, 0, 0),
     datetime.datetime(2023, 3, 6, 0, 0),
     datetime.datetime(2023, 3, 7, 0, 0),
     datetime.datetime(2023, 3, 8, 0, 0),
     datetime.datetime(2023, 3, 9, 0, 0),
     datetime.datetime(2023, 3, 10, 0, 0),
     datetime.datetime(2023, 3, 11, 0, 0),
     datetime.datetime(2023, 3, 12, 0, 0),
     datetime.datetime(2023, 3, 13, 0, 0),
     datetime.datetime(2023, 3, 14, 0, 0),
     datetime.datetime(2023, 3, 15, 0, 0),
     datetime.datetime(2023, 3, 16, 0, 0),
     datetime.datetime(2023, 3, 17, 0, 0),
     datetime.datetime(2023, 3, 18, 0, 0),
     datetime.datetime(2023, 3, 19, 0, 0),
     datetime.datetime(2023, 3, 20, 0, 0),
     datetime.datetime(2023, 3, 21, 0, 0),
     datetime.datetime(2023, 3, 22, 0, 0),
     datetime.datetime(2023, 3, 23, 0, 0),
     datetime.datetime(2023, 3, 24, 0, 0),
     datetime.datetime(2023, 3, 25, 0, 0),
     datetime.datetime(2023, 3, 26, 0, 0),
     datetime.datetime(2023, 3, 27, 0, 0),
     datetime.datetime(2023, 3, 28, 0, 0),
     datetime.datetime(2023, 3, 29, 0, 0),
     datetime.datetime(2023, 3, 30, 0, 0),
     datetime.datetime(2023, 3, 31, 0, 0),
     datetime.datetime(2023, 4, 1, 0, 0),
     datetime.datetime(2023, 4, 2, 0, 0),
     datetime.datetime(2023, 4, 3, 0, 0),
     datetime.datetime(2023, 4, 4, 0, 0),
     datetime.datetime(2023, 4, 5, 0, 0),
     datetime.datetime(2023, 4, 6, 0, 0),
     datetime.datetime(2023, 4, 7, 0, 0),
     datetime.datetime(2023, 4, 8, 0, 0),
     datetime.datetime(2023, 4, 9, 0, 0),
     datetime.datetime(2023, 4, 10, 0, 0),
     datetime.datetime(2023, 4, 11, 0, 0),
     datetime.datetime(2023, 4, 12, 0, 0),
     datetime.datetime(2023, 4, 13, 0, 0),
     datetime.datetime(2023, 4, 14, 0, 0),
     datetime.datetime(2023, 4, 15, 0, 0),
     datetime.datetime(2023, 4, 16, 0, 0),
     datetime.datetime(2023, 4, 17, 0, 0),
     datetime.datetime(2023, 4, 18, 0, 0),
     datetime.datetime(2023, 4, 19, 0, 0),
     datetime.datetime(2023, 4, 20, 0, 0),
     datetime.datetime(2023, 4, 21, 0, 0),
     datetime.datetime(2023, 4, 22, 0, 0),
     datetime.datetime(2023, 4, 23, 0, 0),
     datetime.datetime(2023, 4, 24, 0, 0),
     datetime.datetime(2023, 4, 25, 0, 0),
     datetime.datetime(2023, 4, 26, 0, 0),
     datetime.datetime(2023, 4, 27, 0, 0),
     datetime.datetime(2023, 4, 28, 0, 0),
     datetime.datetime(2023, 4, 29, 0, 0),
     datetime.datetime(2023, 4, 30, 0, 0),
     datetime.datetime(2023, 5, 1, 0, 0),
     datetime.datetime(2023, 5, 2, 0, 0),
     datetime.datetime(2023, 5, 3, 0, 0),
     datetime.datetime(2023, 5, 4, 0, 0),
     datetime.datetime(2023, 5, 5, 0, 0),
     datetime.datetime(2023, 5, 6, 0, 0),
     datetime.datetime(2023, 5, 7, 0, 0),
     datetime.datetime(2023, 5, 8, 0, 0),
     datetime.datetime(2023, 5, 9, 0, 0),
     datetime.datetime(2023, 5, 10, 0, 0),
     datetime.datetime(2023, 5, 11, 0, 0),
     datetime.datetime(2023, 5, 12, 0, 0),
     datetime.datetime(2023, 5, 13, 0, 0),
     datetime.datetime(2023, 5, 14, 0, 0),
     datetime.datetime(2023, 5, 15, 0, 0),
     datetime.datetime(2023, 5, 16, 0, 0),
     datetime.datetime(2023, 5, 17, 0, 0),
     datetime.datetime(2023, 5, 18, 0, 0),
     datetime.datetime(2023, 5, 19, 0, 0),
     datetime.datetime(2023, 5, 20, 0, 0),
     datetime.datetime(2023, 5, 21, 0, 0),
     datetime.datetime(2023, 5, 22, 0, 0),
     datetime.datetime(2023, 5, 23, 0, 0),
     datetime.datetime(2023, 5, 24, 0, 0),
     datetime.datetime(2023, 5, 25, 0, 0),
     datetime.datetime(2023, 5, 26, 0, 0),
     datetime.datetime(2023, 5, 27, 0, 0),
     datetime.datetime(2023, 5, 28, 0, 0),
     datetime.datetime(2023, 5, 29, 0, 0),
     datetime.datetime(2023, 5, 30, 0, 0),
     datetime.datetime(2023, 5, 31, 0, 0),
     datetime.datetime(2023, 6, 1, 0, 0),
     datetime.datetime(2023, 6, 2, 0, 0),
     datetime.datetime(2023, 6, 3, 0, 0),
     datetime.datetime(2023, 6, 4, 0, 0),
     datetime.datetime(2023, 6, 5, 0, 0),
     datetime.datetime(2023, 6, 6, 0, 0),
     datetime.datetime(2023, 6, 7, 0, 0),
     datetime.datetime(2023, 6, 8, 0, 0),
     datetime.datetime(2023, 6, 9, 0, 0),
     datetime.datetime(2023, 6, 10, 0, 0),
     datetime.datetime(2023, 6, 11, 0, 0),
     datetime.datetime(2023, 6, 12, 0, 0),
     datetime.datetime(2023, 6, 13, 0, 0),
     datetime.datetime(2023, 6, 14, 0, 0),
     datetime.datetime(2023, 6, 15, 0, 0),
     datetime.datetime(2023, 6, 16, 0, 0),
     datetime.datetime(2023, 6, 17, 0, 0),
     datetime.datetime(2023, 6, 18, 0, 0),
     datetime.datetime(2023, 6, 19, 0, 0),
     datetime.datetime(2023, 6, 20, 0, 0),
     datetime.datetime(2023, 6, 21, 0, 0),
     datetime.datetime(2023, 6, 22, 0, 0),
     datetime.datetime(2023, 6, 23, 0, 0),
     datetime.datetime(2023, 6, 24, 0, 0),
     datetime.datetime(2023, 6, 25, 0, 0),
     datetime.datetime(2023, 6, 26, 0, 0),
     datetime.datetime(2023, 6, 27, 0, 0),
     datetime.datetime(2023, 6, 28, 0, 0),
     datetime.datetime(2023, 6, 29, 0, 0),
     datetime.datetime(2023, 6, 30, 0, 0),
     datetime.datetime(2023, 7, 1, 0, 0),
     datetime.datetime(2023, 7, 2, 0, 0),
     datetime.datetime(2023, 7, 3, 0, 0),
     datetime.datetime(2023, 7, 4, 0, 0),
     datetime.datetime(2023, 7, 5, 0, 0),
     datetime.datetime(2023, 7, 6, 0, 0),
     datetime.datetime(2023, 7, 7, 0, 0),
     datetime.datetime(2023, 7, 8, 0, 0),
     datetime.datetime(2023, 7, 9, 0, 0),
     datetime.datetime(2023, 7, 10, 0, 0),
     datetime.datetime(2023, 7, 11, 0, 0),
     datetime.datetime(2023, 7, 12, 0, 0),
     datetime.datetime(2023, 7, 13, 0, 0),
     datetime.datetime(2023, 7, 14, 0, 0),
     datetime.datetime(2023, 7, 15, 0, 0),
     datetime.datetime(2023, 7, 16, 0, 0),
     datetime.datetime(2023, 7, 17, 0, 0),
     datetime.datetime(2023, 7, 18, 0, 0),
     datetime.datetime(2023, 7, 19, 0, 0),
     datetime.datetime(2023, 7, 20, 0, 0),
     datetime.datetime(2023, 7, 21, 0, 0),
     datetime.datetime(2023, 7, 22, 0, 0),
     datetime.datetime(2023, 7, 23, 0, 0),
     datetime.datetime(2023, 7, 24, 0, 0),
     datetime.datetime(2023, 7, 25, 0, 0),
     datetime.datetime(2023, 7, 26, 0, 0),
     datetime.datetime(2023, 7, 27, 0, 0),
     datetime.datetime(2023, 7, 28, 0, 0),
     datetime.datetime(2023, 7, 29, 0, 0),
     datetime.datetime(2023, 7, 30, 0, 0),
     datetime.datetime(2023, 7, 31, 0, 0),
     datetime.datetime(2023, 8, 1, 0, 0),
     datetime.datetime(2023, 8, 2, 0, 0),
     datetime.datetime(2023, 8, 3, 0, 0),
     datetime.datetime(2023, 8, 4, 0, 0),
     datetime.datetime(2023, 8, 5, 0, 0),
     datetime.datetime(2023, 8, 6, 0, 0),
     datetime.datetime(2023, 8, 7, 0, 0),
     datetime.datetime(2023, 8, 8, 0, 0),
     datetime.datetime(2023, 8, 9, 0, 0),
     datetime.datetime(2023, 8, 10, 0, 0),
     datetime.datetime(2023, 8, 11, 0, 0),
     datetime.datetime(2023, 8, 12, 0, 0),
     datetime.datetime(2023, 8, 13, 0, 0),
     datetime.datetime(2023, 8, 14, 0, 0),
     datetime.datetime(2023, 8, 15, 0, 0),
     datetime.datetime(2023, 8, 16, 0, 0),
     datetime.datetime(2023, 8, 17, 0, 0),
     datetime.datetime(2023, 8, 18, 0, 0),
     datetime.datetime(2023, 8, 19, 0, 0),
     datetime.datetime(2023, 8, 20, 0, 0),
     datetime.datetime(2023, 8, 21, 0, 0),
     datetime.datetime(2023, 8, 22, 0, 0),
     datetime.datetime(2023, 8, 23, 0, 0),
     datetime.datetime(2023, 8, 24, 0, 0),
     datetime.datetime(2023, 8, 25, 0, 0),
     datetime.datetime(2023, 8, 26, 0, 0),
     datetime.datetime(2023, 8, 27, 0, 0),
     datetime.datetime(2023, 8, 28, 0, 0),
     datetime.datetime(2023, 8, 29, 0, 0),
     datetime.datetime(2023, 8, 30, 0, 0),
     datetime.datetime(2023, 8, 31, 0, 0),
     datetime.datetime(2023, 9, 1, 0, 0),
     datetime.datetime(2023, 9, 2, 0, 0),
     datetime.datetime(2023, 9, 3, 0, 0),
     datetime.datetime(2023, 9, 4, 0, 0),
     datetime.datetime(2023, 9, 5, 0, 0),
     datetime.datetime(2023, 9, 6, 0, 0),
     datetime.datetime(2023, 9, 7, 0, 0),
     datetime.datetime(2023, 9, 8, 0, 0),
     datetime.datetime(2023, 9, 9, 0, 0),
     datetime.datetime(2023, 9, 10, 0, 0),
     datetime.datetime(2023, 9, 11, 0, 0),
     datetime.datetime(2023, 9, 12, 0, 0),
     datetime.datetime(2023, 9, 13, 0, 0),
     datetime.datetime(2023, 9, 14, 0, 0),
     datetime.datetime(2023, 9, 15, 0, 0),
     datetime.datetime(2023, 9, 16, 0, 0),
     datetime.datetime(2023, 9, 17, 0, 0),
     datetime.datetime(2023, 9, 18, 0, 0),
     datetime.datetime(2023, 9, 19, 0, 0),
     datetime.datetime(2023, 9, 20, 0, 0),
     datetime.datetime(2023, 9, 21, 0, 0),
     datetime.datetime(2023, 9, 22, 0, 0),
     datetime.datetime(2023, 9, 23, 0, 0),
     datetime.datetime(2023, 9, 24, 0, 0),
     datetime.datetime(2023, 9, 25, 0, 0),
     datetime.datetime(2023, 9, 26, 0, 0),
     datetime.datetime(2023, 9, 27, 0, 0),
     datetime.datetime(2023, 9, 28, 0, 0),
     datetime.datetime(2023, 9, 29, 0, 0),
     datetime.datetime(2023, 9, 30, 0, 0),
     datetime.datetime(2023, 10, 1, 0, 0),
     datetime.datetime(2023, 10, 2, 0, 0),
     datetime.datetime(2023, 10, 3, 0, 0),
     datetime.datetime(2023, 10, 4, 0, 0),
     datetime.datetime(2023, 10, 5, 0, 0),
     datetime.datetime(2023, 10, 6, 0, 0),
     datetime.datetime(2023, 10, 7, 0, 0),
     datetime.datetime(2023, 10, 8, 0, 0),
     datetime.datetime(2023, 10, 9, 0, 0),
     datetime.datetime(2023, 10, 10, 0, 0),
     datetime.datetime(2023, 10, 11, 0, 0),
     datetime.datetime(2023, 10, 12, 0, 0),
     datetime.datetime(2023, 10, 13, 0, 0),
     datetime.datetime(2023, 10, 14, 0, 0),
     datetime.datetime(2023, 10, 15, 0, 0),
     datetime.datetime(2023, 10, 16, 0, 0),
     datetime.datetime(2023, 10, 17, 0, 0),
     datetime.datetime(2023, 10, 18, 0, 0),
     datetime.datetime(2023, 10, 19, 0, 0),
     datetime.datetime(2023, 10, 20, 0, 0),
     datetime.datetime(2023, 10, 21, 0, 0),
     datetime.datetime(2023, 10, 22, 0, 0),
     datetime.datetime(2023, 10, 23, 0, 0),
     datetime.datetime(2023, 10, 24, 0, 0),
     datetime.datetime(2023, 10, 25, 0, 0),
     datetime.datetime(2023, 10, 26, 0, 0),
     datetime.datetime(2023, 10, 27, 0, 0),
     datetime.datetime(2023, 10, 28, 0, 0),
     datetime.datetime(2023, 10, 29, 0, 0),
     datetime.datetime(2023, 10, 30, 0, 0),
     datetime.datetime(2023, 10, 31, 0, 0)]

plt.plot(x, y)畫線圖

plt.plot()
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240923/20162398qcLj6oIwo2.png

x = d
y = p['數量']

plt.plot(x, y)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240923/20162398M5l2NqDl1l.png

調整畫框

fig=plt.figure(dpi=80, figsize=(8,6))

plt.plot(x, y)

plt.show()

https://ithelp.ithome.com.tw/upload/images/20240923/201623984Sl03MO5id.png

自動調整x軸的 日期格式

fig=plt.figure(dpi=80, figsize=(8,6))

plt.plot(x, y)
fig.autofmt_xdate()

plt.show()

https://ithelp.ithome.com.tw/upload/images/20240923/201623980QEWdpNyRO.png

內容預告:

14 [R]for迴圈 和 ggplot 畫線圖

15 for 迴圈 和 html網頁資料解析


上一篇
13 [R] 畫長條圖統計 [16th 鐵人 Day 26]
下一篇
14 [R] for迴圈 和 ggplot 畫線圖 [16th 鐵人 Day 28]
系列文
Python和R入門語法比較30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言