python制作分布图

制作分布图类似密度图,在python中利用pandas来提取分布数据是比较方便的。主要用到pandas的cut和groupby等函数。

第一步,从数据库中提取数据

import pandas
from sqlalchemy import create_engine
host_mysql_test = '127.0.0.1'
port_mysql_test = 3306
user_mysql_test = 'admin'
pwd_mysql_test = '1234'
db_name_mysql_test = 'mydb'
engine_hq = create_engine('mysql+mysqldb://%s:%s@%s:%d/%s' % (user_mysql_test,
                                                              pwd_mysql_test,
                                                              host_mysql_test,
                                                              port_mysql_test,
                                                              'hq_db'), connect_args={'charset': 'utf8'})

sql = "SELECT * FROM fund_data where quarter>=8 order by yanzhi desc"
df = pd.read_sql(sql, engine)
#将yanzhi数据转换为百分比
df['yanzhi'] = df['yanzhi'].apply(lambda x: x * 100)

第二步,面元划分

  • cut函数:
pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False)

官方文档链接

主要参数为x和bins。
x为数据源,数组格式的都支持,list,numpy.narray, pandas.Series。
bins可以为int,也可以为序列。

bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
cats = pd.cut(df['yanzhi'], bins)

我们定义bins为一个序列,默认为左开右闭的区间:

In[]:print cats
Out[]:
0      (90, 100]
1      (90, 100]
2      (90, 100]
3       (80, 90]
4       (80, 90]
         ...    
970     (10, 20]
971     (10, 20]
972     (10, 20]
973     (10, 20]
974     (10, 20]
Name: yanzhi, dtype: category
Categories (10, object): [(0, 10], (10, 20], (20, 30], (30, 40], ..., (60, 70], (70, 80], (80, 90]
                         , (90, 100]]

第三步,groupby

对言值列按cats做groupby,然后调用get_stats统计函数,再用unstack函数将层次化的行索引“展开”为列。

def get_stats(group):
    return {'count': group.count()}

grouped = df['yanzhi'].groupby(cats)
bin_counts = grouped.apply(get_stats).unstack()

print bin_counts

           count
yanzhi          
(0, 10]        0
(10, 20]       5
(20, 30]      22
(30, 40]      92
(40, 50]     258
(50, 60]     357
(60, 70]     178
(70, 80]      51
(80, 90]       9
(90, 100]      3

第四步,重命名索引,pandas绘图

bin_counts.index = ['0~10', '10~20', '20~30', '30~40', '40~50', '50~60', '60~70',
                    '70~80', '80~90', '90~100']
bin_counts.index.name = 'yanzhi'
bin_counts.plot(kind='bar', alpha=0.5, rot=0)
python制作分布图_第1张图片

扩展:其它工具绘制

一,用G2绘制

G2在之前的文章中有介绍,文章《python结合G2绘制精美图形》。

1,生成json数据

datas = []
for ix, row in bin_counts.iterrows():
    # if row['机构数量'] > 0:
    sss = {'name': ix, 'count': row['count']}
    datas.append(sss)
encodejson = json.dumps(datas, ensure_ascii=False)
f = open('yanzhi.json', 'w')
f.write(encodejson)
f.close()

2,配置html文件



  
    
    分布图
    
    
    
    
    
    
  
  
    

3,显示结果

python制作分布图_第2张图片

二、DataFrame密度图

一句话绘制出来,但具体的区间段难以区分出来。

df["yanzhi"].hist(bins=20, alpha=0.5)
python制作分布图_第3张图片

三、bokeh绘图

bokeh是python的一个优秀的绘图工具包,与pandas结合的比较好。bokeh文档

from bokeh.charts import Histogram, output_file,show

hist=Histogram(df, values='yanzhi',bins=30, title='分布图', legend='top_right')
output_file('hist.html', title='hist example')
show(hist)
python制作分布图_第4张图片

作者原文链接:python制作分布图

你可能感兴趣的:(python制作分布图)