Matplotlib简单画图(四) -- pandas绘图之DataFrame

import numpy as np
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
# 创建一个10行4列的DataFrame
df = DataFrame(
    np.random.randint(1,10,40).reshape(10,4),
    columns = {'A','B','C','D'}
)
df
B   A   D   C
0   1   3   8   4
1   2   3   1   9
2   6   5   7   1
3   1   2   9   7
4   4   2   4   7
5   2   1   4   5
6   9   8   1   5
7   2   3   5   1
8   4   2   1   6
9   9   4   3   8
# kind='bar'是柱形图,默认为line
df.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0x1ee7b401d30>
plt.show()

这里写图片描述

# 横柱形图
df.plot(kind='barh')

这里写图片描述

# stacked=True堆叠
df.plot(kind='bar',stacked=True)
plt.show()

这里写图片描述

df.plot(kind='area')
plt.show()

这里写图片描述

# 画一行
df.iloc[5].plot()
plt.show()

这里写图片描述

# 画10行
for i in df.index:
    df.iloc[i].plot(label=str(i))
plt.legend()
plt.show()
# 画一列
df['A'].plot()
plt.show()

这里写图片描述

df.T

0   1   2   3   4   5   6   7   8   9
B   2   7   9   7   9   8   6   4   6   1
A   2   5   5   3   6   1   7   7   5   9
D   1   6   4   9   1   8   1   4   8   5
C   9   5   2   4   5   1   1   4   2   9
# 转置画行
df.T.plot()
plt.show()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39778570/article/details/81143763