首先,我开发了此代码,使其仅适用于饼图。下面的工作代码。
#!/usr/bin/python2.7
#coding=utf8
import os
import matplotlib as mpl
#mpl.use('Agg')
#import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
fig=plt.figure(figsize=(12,12))
graph_grid=GridSpec(1,3)
datas=[[(u'hangup', 480L), (u'invalid', 46L), (u'one', 1235L), (u'repeat', 153L), (u'three', 987L), (u'two', 2379L), (u'wait', 810L)],
[(u'hangBut', 2L), (u'gotoButik', 113L), (u'goBackfrom', 29L), (u'fromIM2Hot', 398L), (u'choice3_to', 1L), (u'choice2_to', 1L)],
[(0L, 5L), (1L, 6L), (2L, 9L), (9L, 419L), (10L, 609L), (11L, 685L), (12L, 694L), (13L, 639L), (14L, 611L), (15L, 566L), (16L, 523L), (17L, 484L), (18L, 327L), (19L, 253L), (20L, 155L), (21L, 61L), (22L, 33L), (23L, 11L)]]
counter=0
for ds in datas:
labels=[ k[0] for k in ds]
vals=[ k[1] for k in ds]
ziplists=zip(vals,labels)
ziplists=sorted(ziplists,reverse=True)
vals, labels = zip(*ziplists)
total=sum(vals)
plt.subplot(graph_grid[0,counter], aspect=1)
#print len(labels)
#print len(vals)
print()
plt.title("title")
if counter <= 1:
piechart=plt.pie(vals, autopct=lambda(p): '{:.0f}'.format(p * total / 100), shadow=True,pctdistance=1.2)
plt.legend(piechart[0], labels, loc="lower right", prop={'size': 6}, bbox_to_anchor=(0.1, 0.04),)
elif counter == 2:
ypos=np.arange(len(labels))
print ypos
print len(vals)
plt.bar(ypos,vals,color='red', width=0.3 )
plt.xticks(ypos,vals)
plt.xlabel(u'hours')
plt.ylabel(u'count')
counter+=1
fig.tight_layout()
fig.set_size_inches(w=11, h=7)
plt.show()
看起来不错。大多。但是一些数据在条形图中可能看起来更好。说-完成。 可悲的是,条形图绘制得太窄,我几乎无法更改其大小。 一些疯狂的宽度值,例如witdth = 1000可以完成一些工作,但图形仍然显得糟透了。问题出在哪里?如何解决?