记录一些自己的 pandas 心得
pandas 绘图的标签添加
之前在读罗斯的《公司理财》,做课后习题的时候突然想到用 pandas 做一做练练手,具体如下:
import pandas as pd
import matplotlib.pyplot as plt
import pylab as pl
columns = ['衰退','正常','扩张']
n = 3642
i=88500*0.08
rows = ['EBIT','I','T','净利润','股数','EPS']
#列索引直接选取,行索引使用loc方法选取
def get_data(columns,rows,n=5000,i=0):#生成pandas数组
zero = [0,0,0]
data = pd.DataFrame([
[23000*0.6,23000,23000*1.4],
[i,i,i],
zero,
zero,
[n,n,n],
zero
],columns=columns,index=rows)
data.loc['T'] = (data.loc['EBIT']-data.loc['I'])*0.35
data.loc['净利润'] = data.loc['EBIT']-data.loc['I']-data.loc['T']
data.loc['EPS'] = data.loc['净利润']/data.loc['股数']
return data
data = get_data(columns,rows)
figure = plt.figure(dpi=720)#设置像素
plt.rc('font',family='simhei')#设置字体黑体
axes = data.plot.bar(ax = plt.gca())
pl.xticks(rotation=360)
plt.show()
得到的柱形图如下:
但是在我添加标签的时候出现了问题,我无法像使用matplotlib一样选取 bar 迭代添加标签,因此我们需要从axes中选取我们需要的实例。
在这里本人实在是找不到相关的文章,但是考虑到 pandas 使用 matplotlib 绘图,而 matplotlib 又仿照 MATLAB 编制,因此我去MATLAB中文论坛中找到了一个函数
children = get_children(axes)
回来 dir 一下 axes 找到了相同的方法
children = axes.get_children()
[<matplotlib.patches.Rectangle at 0x1ffaaa74220>,
<matplotlib.patches.Rectangle at 0x1ffaaa740a0>,
<matplotlib.patches.Rectangle at 0x1ffaaa74700>,
<matplotlib.patches.Rectangle at 0x1ffaaa749d0>,
<matplotlib.patches.Rectangle at 0x1ffaaa74ca0>,
<matplotlib.patches.Rectangle at 0x1ffaaa74f70>,
<matplotlib.patches.Rectangle at 0x1ffaaa80400>,
<matplotlib.patches.Rectangle at 0x1ffaaa741f0>,
<matplotlib.patches.Rectangle at 0x1ffaaa80850>,
<matplotlib.patches.Rectangle at 0x1ffaaa80b20>,
<matplotlib.patches.Rectangle at 0x1ffaaa80df0>,
<matplotlib.patches.Rectangle at 0x1ffaaa90100>,
<matplotlib.patches.Rectangle at 0x1ffaaa90550>,
<matplotlib.patches.Rectangle at 0x1ffaaa74040>,
<matplotlib.patches.Rectangle at 0x1ffaaa909a0>,
<matplotlib.patches.Rectangle at 0x1ffaaa90c70>,
<matplotlib.patches.Rectangle at 0x1ffaaa90f40>,
<matplotlib.patches.Rectangle at 0x1ffaaa9e250>,
<matplotlib.spines.Spine at 0x1ffaaa47160>,
<matplotlib.spines.Spine at 0x1ffaaa47280>,
<matplotlib.spines.Spine at 0x1ffaaa473a0>,
<matplotlib.spines.Spine at 0x1ffaaa47490>,
<matplotlib.axis.XAxis at 0x1ffaaa470d0>,
<matplotlib.axis.YAxis at 0x1ffaaa477f0>,
Text(0.5, 1.0, ''),
Text(0.0, 1.0, ''),
Text(1.0, 1.0, ''),
<matplotlib.legend.Legend at 0x1ffaaa64460>,
<matplotlib.patches.Rectangle at 0x1ffaaa50d00>]
可以看到, pandas.plot() 直接返回一个 axes 对象,其中 bar 以 Rectangle 类存在,只要选取该对象添加标签即可,全代码如下:
import pandas as pd
import matplotlib.pyplot as plt
import pylab as pl
columns = ['衰退','正常','扩张']
n = 3642
i=88500*0.08
rows = ['EBIT','I','T','净利润','股数','EPS']
#列索引直接选取,行索引使用loc方法选取
def get_data(columns,rows,n=5000,i=0):#生成pandas数组
zero = [0,0,0]
data = pd.DataFrame([
[23000*0.6,23000,23000*1.4],
[i,i,i],
zero,
zero,
[n,n,n],
zero
],columns=columns,index=rows)
data.loc['T'] = (data.loc['EBIT']-data.loc['I'])*0.35
data.loc['净利润'] = data.loc['EBIT']-data.loc['I']-data.loc['T']
data.loc['EPS'] = data.loc['净利润']/data.loc['股数']
return data
"""
pandas绘制后直接生成axes实例,需要使用get_children方法选取相应实例
pylab设置坐标轴文字方向
"""
def draw(data):#使用pandas绘制柱状图,并添加标签
plt.figure(dpi=720)#设置图片像素
plt.rc('font',family='simhei')#设置字体黑体
draw = data.plot.bar(ax = plt.gca())#draw为axes实例
children = draw.get_children()
bars = children[:18]
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x(),height,str(int(height)))
pl.xticks(rotation=360)
data = get_data(columns,rows)
data_i = get_data(columns,rows,n,i)
data_cont = pd.DataFrame({'重组前':data.loc['EPS'],'重组后':data_i.loc['EPS']})
print(data)
print(data_i)
print(data_cont)
plt.rc('font',family='simhei')#设置字体黑体
draw(data)
draw(data_i)
draw(data_cont)
plt.show()