1.1 第一个绘图
(原本在matplotlib中需要几段代码,在pandas中只需要一行代码)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
s = pd.Series(
np.random.randn(10).cumsum() #产生一个10行4列的随机数
index = np.arange(0,100,10) #0-100之间,以10作为分割
)
s.plot()
plt.show()
1.2 pandas利用DataFrame的plot方法绘图
df = pd.DataFrame(
np.random.randn(10,4),
columns = list('ABCD'),
index = np.range(0,100,10)
)
pd.plot()
plt.show()
1.3 pandas利用DataFrame绘图柱状图/堆积图
fig = plt.figure()
fig,axes = plt.subplots(2,1)
data = pd.Series(np.random.randn(16),
index = list('abcdefghijklmnop'))
data.plot(kind = 'bar',ax = axes[0],color = 'k',alpha = 0.7)
data.plot(kind = 'barh',ax = axes[1],color = 'k',alpha = 0.7)
plt.show()
1.4 pandas绘图直方图
df = pd.read_excel('./pandas-matplotlib.xlsx','Sheet1')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(df['Age'],bins = 7)
plt.title('Age discribution')
plt.xlabel('Age')
plt.ylabel('Employee')
plt.show()
1.5 pandas绘图箱型图
df = pd.read_excel('./pandas-matplotlib.xlsx','Sheet1')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.boxplot(df['Age'])
plt.show()
1.6 利用pandas 处理后的数据绘图
◆条形图
var = df.groupby('Gender').Sales.sum()
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_xlabel('Gender')
ax1.set_ylabel('Sum of Sales')
ax1.set_title('Gender wise Sum of Sales')
var.plot(kind = 'bar')
◆折线图
line = df.groupby('BMI').Sales.sum()
ax1 = fig.add_subplot(212)
ax1.set_xlabel('BMI')
ax1.set_ylabel('Sum of Sales')
ax1.set_title('BMI wise Sum of Sales')
line.plot(kind = 'line')
plt.tight_layout()
plt.show()
1.7 利用pandas 绘制柱状堆积图
var =df.groupby(['BMI','Gender']).Sales.sum()
var.unstack().plot(kind = 'bar',stacked = True,color = ['red','blue'])
plt.show()
1.8 利用pandas 绘制点状图
◆绘制 散点图
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax.scatter(df['Age'],df[Sales])
plt.show()
◆绘制 气泡图
fig =plt.figure()
ax1 = fig.add_subplot(111)
ax.scatter(df['Age'],df[Sales],s = df['income'])
plt.show()
1.9 利用pandas 绘制饼图
var = df.groupby(['Gender']).sum().stack()
temp = var.unstack()
x_list = temp['Sales']
label_list = temp.index
plt.axis('equal')
plt.pie(x_list,labels = label_list,autopct = '%1.1f%%' )
plt.title('expense')
plt.show()