Series和DataFrame都有一个用于生成各类图表的plot方法。默认情况下,它们所生成的是线形图。
解决中文和负号下的重要技巧:
matplotlib的默认字体不是中文,所以不能显示中文
plt.rcParams['font.sans-serif']='SimHei'
解决中文字体下,负号无法正常显示的问题
plt.rcParams['axes.unicode_minus']=False
简单的Series图表示例,plot()
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
s = Series(data=np.random.randint(0, 10, size=10))
s.plot()

简单的DataFrame图表示例,plot()
图例的位置可能会随着数据的不同而不同
index=['张三','李四','王五','赵六']
columns=['语文','数学','英语','python']
data=np.random.randint(80,150,size=(4,4))
df = DataFrame(data=data,columns=columns,index=index)
df
'''
out:
语文 数学 英语 python
张三 136 124 137 122
李四 82 87 111 146
王五 134 92 85 82
赵六 96 126 101 103
'''
df

Series柱状图示例,kind=‘bar’/‘barh’
s.plot(kind='barh')

DataFrame柱状图示例
df.plot(kind='bar')

案例:读取文件tips.csv,查看每天的聚会人数情况,每天各种聚会规模的比例
求和df.sum(),注意灵活使用axis
tips = pd.read_csv('../data/tips.csv')
tips

tips.set_index(key='day', inplace=True)
tips

tips.plot(kind='bar')

tips/tips.sum(axis=1) # 不能这么除,除完全是NaN
result = tips.div(tips.sum(axis=1), axis='index')
result.plot(kind='bar')

默认的直方图y轴式数据出现的次数
histogram
直方图反映数据的分布情况
n = np.array([1, 1, 2, 3, 3, 4, 5, 5, 6, 6])
s = Series(data=n)
s.plot(kind='hist')

# 计算直方图数据,返回出现的次数和组距
np.histogram(n)
'''
out: (array([2, 0, 1, 0, 2, 0, 1, 0, 2, 2], dtype=int64),
array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ]))
'''
n # out: array([1, 1, 2, 3, 3, 4, 5, 5, 6, 6])
rondom生成随机数百分比直方图,调用hist方法
# 加上density,变成概率密度,概率/组距
s.plot(kind='hist', bins=10, density=True)

kde图:核密度估计,用于弥补直方图由于参数bins设置的不合理导致的精度缺失问题
# 经常把直方图和kde图画在一起。
# kde:kernel density estimate
s.plot(kind='hist',bins=10,density=True)![14]
s.plot(kind='kde')

案例:由两个不同的正态分布组成的双峰分布
n1 = np.random.normal(loc=10, scale=3, size=1000)
n2 = np.random.normal(loc=30, scale=6, size=1000)
data = np.hstack(n1, n2)
s = Series(data=data)
s.plot(kind='hist', density=True, bins=100)
s.plot(kind='kde')

散点图是观察两个一维数列之间的关系的有效方法,DataFrame对象可用
使用方法:设置kind=‘scatter’,给明标签columns
x = np.linspce(2, 8, 10)
y = np.linspce(0, 10, 10)
display(x,y)
'''
out:
array([2. , 2.66666667, 3.33333333, 4. , 4.66666667,
5.33333333, 6. , 6.66666667, 7.33333333, 8. ])
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
'''
plt.scatter(x,y)

df = DataFrame({'x':x, 'y':y})
df
'''
out:
x y
0 2.000000 0.000000
1 2.666667 1.111111
2 3.333333 2.222222
3 4.000000 3.333333
4 4.666667 4.444444
5 5.333333 5.555556
6 6.000000 6.666667
7 6.666667 7.777778
8 7.333333 8.888889
9 8.000000 10.000000
'''
df.plot(x='x', y='y', kind='scatter') # 结果图同上
# 正弦图
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
df = DataFrame({'x':x, 'y':y})
df.plot(x='x', y='y', kind='scatter')

散点图矩阵,当有多个点时,两两点的关系
使用函数:pd.plotting.scater_matrix()
# 观察dataframe列与列两两形成的关系,可以画一个散点图矩阵
df = DataFrame({'A':np.random.randn(300),'B':np.random.randn(300),'C':np.random.randn(300),'D':np.random.randn(300)})
df

_=pd.plotting.scatter_matrix(df, figsize=(16,16),alpha=1,diagonal='kde')
