【數據分析與可視化】Pandas繪圖之DataFrame

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
from pandas import Series,DataFrame
df = DataFrame(
    # 範圍1~10取40個
    np.random.randint(1,10,40).reshape(10,4),
    columns=['A','B','C','D']
)
df
A B C D
0 4 6 4 2
1 8 6 5 3
2 5 4 6 3
3 9 5 9 4
4 6 8 5 2
5 5 9 2 6
6 9 4 9 4
7 9 3 2 1
8 1 3 4 7
9 6 3 4 9
# 按列名畫圖
# 默認按DataFrame每列畫一條曲線
# 參數和Series一樣可以設置
df.plot(kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0x119f0a850>

在這裏插入圖片描述

# 圖像堆疊
df.plot(kind='bar', stacked=True)
<matplotlib.axes._subplots.AxesSubplot at 0x119585cd0>

在這裏插入圖片描述

# 填充方式
df.plot(kind='area')

在這裏插入圖片描述 <matplotlib.axes._subplots.AxesSubplot at 0x119922c90>

# 除了上述對列取值,對行取值
a = df.iloc[5]
a
A    5
B    9
C    2
D    6
Name: 5, dtype: int64
type(a)
pandas.core.series.Series
# 某一行取值
df.iloc[5].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x11b5daa90>

在這裏插入圖片描述

# 取所有的行
for i in df.index:
    df.iloc[i].plot(label = str(i))
plt.legend()
<matplotlib.legend.Legend at 0x11b4ed790>

在這裏插入圖片描述

# 更簡單的取所有行操作 轉置
df.T.plot()

在這裏插入圖片描述
<matplotlib.axes._subplots.AxesSubplot at 0x11f1733d0>

# 取一列
df['A'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x11ae7fc10>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章