pandas 8 画图
from __future__ import print_function
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# plot data
Series 构造1列数据,data.plot()
# Series 构造1列数据
np.random.seed(1)
data = pd.Series(np.random.randn(1000), index=np.arange(1000)) # 生成随机数据,有正有负
data = data.cumsum() # 累加,列方向上
data.plot() # 根据数据画图
plt.show()
print(data.head())
> 0 1.624345
> 1 1.012589
> 2 0.484417
> 3 -0.588551
> 4 0.276856
> dtype: float64
DataFrame 构造4列数据,data.plot()
# DataFrame 构造4列数据
data = pd.DataFrame(np.random.randn(1000, 4), index=np.arange(1000), columns=list("ABCD"))
data = data.cumsum() # 累加,列方向上
data.plot()
plt.show()
print(data.head())
> A B C D
> 0 -0.153236 -2.432509 0.507984 -0.324032
> 1 -1.664313 -3.303931 -0.356846 0.284717
> 2 -1.102675 -1.789180 0.291079 -1.066933
> 3 -2.511884 -0.658455 1.857765 -1.304681
> 4 -1.953081 -2.163346 -0.086156 -2.478704
data.plot.scatter(x='A', y='C')
# plot methods:
# 'bar', 'hist', 'box', 'kde', 'area', scatter', hexbin', 'pie'
ax = data.plot.scatter(x='A', y='B', color='DarkBlue', label="Class 1")
data.plot.scatter(x='A', y='C', color='LightGreen', label='Class 2', ax=ax)
plt.show()
END