python画饼图存在的问题_matplotlib饼图函数的Python问题
(错误的标签)
我在使⽤matplotlib设置饼图时遇到了⼀个奇怪的问题。由于某些原因,它似乎没有正确地处理我的labels参数。在
⼀点背景知识…我正在开发⼀个⼯具,它将允许我们创建汇总web地图服务点击率的表格。这基本上只是遍历所有⽇志⽂件并获取⽤户名、站点名和⽇期\时间信息。我已经让第⼀部分⼯作得很好,它创建了⼀些很好的汇总表。不过,如果能⽣成饼图来显⽰每个⽹站的⽤户(按⽤户名)最多,那将是⼀件好事。我还得到了饼图创建⼯作,其中每个图表都是以⽹站命名的。在
不正常⼯作的是标签。在
不可否认,我根本没有太多地使⽤matplotlib,但是根据我在⽹上看到的代码⽰例,我的代码似乎很好。为了再次检查标签的数量是否与饼图的切⽚数匹配,我打印出每个图表的结果,并且输⼊参数似乎是正确的,但可能我遗漏了什么?在
我使⽤的输⼊表看起来不错,这就是饼图所使⽤的数据。以下是我使⽤的特定功能:#! /usr/local/bin/python
import arcpy, os
from pylab import *
import numpy as np
def CreatePieChart(table, out, case_field, name_field, data_field):
# Make new folder
_dir = os.path.join(out, 'Figures')
if not ists(_dir):
os.makedirs(_dir)
# Grab unique values
rows = arcpy.SearchCursor(table)
cases = sorted(list(Value(case_field) for r in rows)))
del rows
# vals dictionary
vals_dict = {}
# Make table views
tv = arcpy.MakeTableView_management(table, 'temp_table')
for case in cases:
query = ''' {0} = '{1}' '''.format(arcpy.AddFieldDelimiters(tv, case_field), case)
arcpy.SelectLayerByAttribute_management(tv, 'NEW_SELECTION', query)
# Get total number for pie slice
rows = arcpy.SearchCursor(tv)
vals_dict[case] = [[r.getValue(name_field),r.getValue(data_field)] for r in rows]
del rows
# Populate Pie Chart
for case,vals in vals_dict.iteritems():
the_fig = figure(1, figsize=(8,8))
axes([0.1, 0.1, 0.8, 0.8])
fig = os.path.join(_dir, '{0}.png'.place(' ','_')))
ax = the_fig.add_subplot(111)
label = [v[0] for v in vals]
fracs = [v[1] for v in vals]
print '%s %s %s' %(case,label,fracs)
if len(label) == len(fracs): # to make sure same number of labels as slices
cmap = prism
color = cmap(np.linspace(0., 1., len(fracs)))
pie_wedges = pie(fracs,colors=color,labels=label,pctdistance=0.5, labeldistance=1.05)
for wedge in pie_wedges[0]:
wedge.set_edgecolor('white')
ax.set_title(case)
savefig(fig)
print 'Created: %s' %fig
del the_fig,label,pie_wedges,fracs
return fig
if __name__ == '__main__':
table = r'C:\TEMP\Usage_Summary.dbf'
out = r'C:\TEMP'
case = 'Site_ID'
name = 'Username'
data = 'Count'
CreatePieChart(table, out, case, name, data)
下⾯是打印到Python窗⼝的内容(切⽚计数确实与标签数量匹配):
^{pr2}$
⼀些奇怪的事情正在幕后发⽣,因为图表中的标签⽐我传递给pie()函数的标签多得多。在
我还没有⼀个声誉,张贴图⽚,但我已经张贴在这⾥的地理信息系统堆栈交换图⽚。Here是可以查看图⽚的链接。图⽚来⾃创建
的“Osseo”图表,您可以从我的打印声明中看到这些是切⽚和标签:Osseo [u'johnsh', u'karlan', u'kevinbi', u'marcusth', u'nickme', u'osseo', u'scottja'] [22, 2, 23, 11, 66, 54, 3]
我不知道为什么要创建这么多额外的标签。我是不是少了点什么?在
下⾯是代码的⼲净版本,以便其他⼈可以测试:#! /usr/local/bin/python
import os
from pylab import *
import numpy as np
_dir = os.path.wd(), 'Figures')
if not ists(_dir):
os.makedirs(_dir)
vals_dict = {u'ElkRiver': [[u'elkriver', 731], [u'jasonco', 1], [u'jenibr', 2], [u'johnsh', 55], [u'nickme', 58]],
u'LongPrairie': [[u'brianya', 6], [u'chuckde', 7], [u'johnsh', 61], [u'longprairie', 129], [u'nickme', 25],
[u'scottja', 3]], u'Madison': [[u'deanhe', 7], [u'johnsh', 9], [u'kathrynst', 1], [u'madison', 39],
[u'scottja', 1]], u'NorthMankato': [[u' ', 20], [u'adamja', 13],[u'brianma', 65], [u'johnsh', 64],
[u'johnvo', 8], [u'marksc', 2], [u'mattme', 4], [u'nickme', 63], [u'nmankato', 64], [u'scottja', 1]],
u'Orono': [[u'arroned', 2], [u'davidma', 11], [u'dionsc', 1], [u'dougkl', 3], [u'gis_guest', 5], [u'jenibr', 1],
[u'johnsh', 40], [u'kenad', 6], [u'kevinfi', 1], [u'kylele', 1], [u'marksc', 5], [u'natest', 1], [u'nickme', 37],
[u'orono', 819], [u'samel', 8], [u'scottja', 5], [u'sethpe', 2], [u'sueda', 2]], u'BellePlaine': [[u'billhe', 3],
[u'billsc', 1], [u'bplaine', 331], [u'christopherga', 1], [u'craigla', 1], [u'dennisst', 40], [u'elkriver', 1],
[u'empire', 1], [u'gis_guest', 12], [u'jasonfe', 1], [u'joedu', 27], [u'johnsh', 61], [u'joshst', 4], [u'lancefr', 1],
[u'nickme', 47], [u'richardde', 3], [u'scottja', 12], [u'teresabu', 2], [u'travisje', 2], [u'wadena', 1]],
u'Osseo': [[u'johnsh', 22], [u'karlan', 2], [u'kevinbi', 23], [u'marcusth', 11], [u'nickme', 66], [u'osseo', 54],
[u'scottja', 3]], u'EmpireTownship': [[u'empire', 96], [u'johnsh', 10], [u'lancefr', 1], [u'lanile', 14], [u'marksc', 2], [u'nickme', 224], [u'richardde', 1], [u'scottja', 1], [u'travisje', 3]], u'Courtland': [[u'courtland', 24], [u'empire', 3], [u'joedu', 3], [u'johnsh', 10], [u'nickme', 27], [u'scottja', 2]], u'LeSueur': [[u' ', 8], [u'johnsh', 6], [u'marksc', 1], [u'nickme', 98]], u'Stratford': [[u'johnsh', 9], [u'neilgu', 3], [u'scottja', 2], [u'stratford', 47]]}
# Populate Pie Chart
for case,vals in vals_dict.iteritems():
the_fig = figure(1, figsize=(8,8))
axes([0.1, 0.1, 0.8, 0.8])
fig = os.path.join(_dir, '{0}.png'.place(' ','_')))
ax = the_fig.add_subplot(111)
label = [v[0] for v in vals]
fracs = [v[1] for v in vals]
print '%s %s %s' %(case,label,fracs)
if len(label) == len(fracs): # to make sure same number of labels as slices
cmap = prism
color = cmap(np.linspace(0., 1., len(fracs)))
pie_wedges = pie(fracs,colors=color,labels=label,pctdistance=0.5, labeldistance=1.1)
linspace函数pythonfor wedge in pie_wedges[0]: wedge.set_edgecolor('white') ax.set_title(case)
savefig(fig)
print 'Created: %s' %fig
del label,pie_wedges,fracs the_fig.clf()