python中pcolor_在matplotlib中使用pcolor进行热图?

# -*- coding: utf-8 -*-# 3.0# ------------------------------------------------------------------------# Filename : heatmap.py# Date : 2013-04-19# Updated : 2014-01-04# Author : @LotzJoe >> Joe Lotz# Description: My attempt at reproducing the FlowingData graphic in Python# Source : http://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/## Other Links:# http://stackoverflow.com/questions/14391959/heatmap-in-matplotlib-with-pcolor## ------------------------------------------------------------------------importmatplotlib.pyplotaspltimportpandasaspdfromurllib2importurlopenimportnumpyasnp%pylab inline

page=urlopen("http://datasets.flowingdata.com/ppg2008.csv")nba=pd.read_csv(page,index_col=0)# Normalize data columnsnba_norm=(nba-nba.mean())/(nba.max()-nba.min())# Sort data according to Points, lowest to highest# This was just a design choice made by Yau# inplace=False (default) ->thanks SO user d1337nba_sort=nba_norm.sort('PTS',ascending=True)nba_sort['PTS'].head(10)# Plot it outfig,ax=plt.subplots()heatmap=ax.pcolor(nba_sort,cmap=plt.cm.Blues,alpha=0.8)# Formatfig=plt.gcf()fig.set_size_inches(8,11)# turn off the frameax.set_frame_on(False)# put the major ticks at the middle of each cellax.set_yticks(np.arange(nba_sort.shape[0])+0.5,minor=False)ax.set_xticks(np.arange(nba_sort.shape[1])+0.5,minor=False)# want a more natural, table-like displayax.invert_yaxis()ax.xaxis.tick_top()# Set the labels# label source:https://en.wikipedia.org/wiki/Basketball_statisticslabels=['Games','Minutes','Points','Field goals made','Field goal attempts','Field goal percentage','Free throws made','Free throws attempts','Free throws percentage','Three-pointers made','Three-point attempt','Three-point percentage','Offensive rebounds','Defensive rebounds','Total rebounds','Assists','Steals','Blocks','Turnover','Personal foul']# note I could have used nba_sort.columns but made "labels" insteadax.set_xticklabels(labels,minor=False)ax.set_yticklabels(nba_sort.index,minor=False)# rotate theplt.xticks(rotation=90)ax.grid(False)# Turn off all the ticksax=plt.gca()fortinax.xaxis.get_major_ticks():t.tick1On=Falset.tick2On=Falsefortinax.yaxis.get_major_ticks():t.tick1On=Falset.tick2On=False

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值