Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot()

加入好友
加入社群
Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

import pandas as pd
import matplotlib.pyplot as plt

testDF = pd.DataFrame([[3,4,6],
[3,6,4]])

testSer = pd.Series(range(2))

fig, ax = plt.subplots()

ax.plot(testSer, testDF)

#ax.plot(testSer, testDF.iloc[:, 0])
plt.show()

 

testDF有三欄:

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

code: 

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

增加label參數,以便增加legend()

(就算沒設定label參數,

也會自動抓欄標籤當label)

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

matplotlib (點此或下圖連結官網)

y可以放2D array

若兩者都是2D array,形狀必須相同

如果只有其中一個是2D array

則另外一個長度(列數)必須相同

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

 

2D array不像DF有.columns 屬性可以賦值給label的話

ax.plot(x, y, label=range(y.shape[1])) 

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

subplots() 方法返回的是

一个 Figure 对象和一个 Axes 对象数组,

如果只需要画一张图的话,

可以直接使用 add_subplot() 方法

来创建单个 Axes 对象,

这个方法返回的是一个单独的 Axes 对象。

因此,如果只需要画一张图的话,

可以使用 subplot() 替代 subplots(),例如:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
# 替代成
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

其中 1, 1, 1 表示创建一个 1 行 1 列的图表,

并且使用第一个子图(即唯一的子图)。

 

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
'A': [1, 2, 3],
'B': [3, 2, 1],
'C': [2, 3, 1]
})

ax = df.plot()

#沒有指定label
ax.legend()

#自動使用columns name當label
plt.show()

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

ax.set_xlabel()

ax.grid()

ax.tick_params()

都跟一般fig, ax = plt.subplot() 的方法一樣

也能指定figsize

用plt.savefig() 存檔

Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

使用ax=DF.plot() 繪圖,

不需要自己import matplotlib.pyplot, 自動import,

但如果想進一步調整圖形的細節,

可能需要使用matplotlib.pyplot中的其他函数。

不需要for迴圈,自動畫所有columns vs index,

不需要設定label, ax.legend()自動使用欄標籤當label

推薦hahow線上學習python: https://igrape.net/30afN

加入好友
加入社群
Python: matplotlib繪圖, ax.plot (pandas.Series, pandas.DataFrame) 一次畫出多條折線圖; fig, ax = plt.subplots() #一次創建fig, ax兩個物件 ; fig = plt.figure() ; ax = fig.add_subplot(1, 1, 1) #fig, ax 分兩次建立 ; ax = df.plot() - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *