Matplotlib:如何在表格单元格中绘制条形图?

时间:2015-11-24 18:17:33

标签: python matplotlib charts

Excel具有在表格单元格内绘制数据条的功能 条件格式>>数据栏。

查看图片。

bars within spreadsheet cells

如何使用matplotlib以编程方式创建类似的结果?

1 个答案:

答案 0 :(得分:0)

我认为您要么必须手动绘制内容,要么稍微调整条形图。一个快速的例子:

enter image description here

import numpy as np
import matplotlib.pylab as pl

def remove_ax():
    ax=pl.gca()
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.set_xticks([])
    ax.get_yaxis().tick_left()

names  = ['Bill','John','Mark']
weight = [155,225,180]
age    = [30,40,50]

dh = 0.8
yloc = np.arange(3) 

pl.figure()
ax=pl.subplot(121)
remove_ax()
pl.title('Weight')
pl.barh(yloc-dh/2, weight, dh, alpha=0.4)
xloc = ax.get_xlim()[-1] - 0.05 * (ax.get_xlim()[-1] - ax.get_xlim()[0])
for i in range(len(weight)):
    pl.text(xloc, yloc[i], '{}'.format(weight[i]), va='center', ha='right', size=18)
ax.set_yticks(yloc)
ax.set_yticklabels(names)
ax.tick_params(labelbottom='off')  

ax=pl.subplot(122)
remove_ax()
pl.title('Age')
pl.barh(yloc-dh/2, age, dh, alpha=0.4)
xloc = ax.get_xlim()[-1] - 0.05 * (ax.get_xlim()[-1] - ax.get_xlim()[0])
for i in range(len(weight)):
    pl.text(xloc, yloc[i], '{}'.format(age[i]), va='center', ha='right', size=18)
ax.set_yticks(yloc)
ax.tick_params(labelleft='off')  
ax.tick_params(labelbottom='off')