pandas入门基础-可视化
演示:
1 基本绘图:plot
in:
import matplotlib.pyplot as plt#导入可视化的模块
import pandas as pd
import numpy as np
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000', periods=1000))#从20001月1日开始,间隔1000天
ts = ts.cumsum()
ts.plot()
out:
in:多条绘图
df = pd.DataFrame(np.random.randn(1000, 4),
index=ts.index, columns=list('ABCD'))
df = df.cumsum()#累加和
df.plot()
out:
2 条形图
in:
df = pd.DataFrame(np.random.randn(1000, 4),
index=ts.index, columns=list('ABCD'))
df = df.cumsum()#累加和
df.iloc[5].plot(kind='bar')#提取指定行,iloc[5]第5行,树状图显示
out:
in:
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2.plot.bar()
out:
in:
df2.plot.bar(stacked=True)#生成堆叠的条形图
out:
in:
df2.plot.barh(stacked=True)#获取水平条形图
out:
3 箱线图
in:
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
out:
in:
color = {'boxes': 'DarkGreen', 'whiskers': 'DarkOrange','medians': 'DarkBlue', 'caps': 'Gray'}
df.plot.box(color=color, sym='r+')#Boxplot可以通过传递color关键字来着色
out:
in:先建立一个二维数组
df = pd.DataFrame(np.random.rand(10, 3), columns=['Col1', 'Col2', 'Col3'])
df
out:
in:传递列的子集进行绘图,也可以按多个列进行分组
df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'])
df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'])
plt.figure()
bp = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])
out:
4 面积图
in:
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
out:
5 散点图
in:
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')
out:
in:单个轴上绘制多个列组,请重复plot指定target的方法ax。且需要指定color和label关键字以区分每个组
ax = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1')
df.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2', ax=ax)
out:
更详细和其他,请见:
链接: https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html