因此,我已经使用ax.scatter成功地在地图上绘制了饼图作为标记,但是我遇到了一些从饼图中“爆炸”的楔形的麻烦。我似乎无法在代码中找到这样做的原因,并且无法在线找到任何解释。这段代码基于示例here,该示例也由同事使用,并生成了正常的统一饼图。在我们之间,我们找不到问题,也没有发生错误。
代码:
import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm
australia_data = np.zeros((24,12))
colors = ['red','yellow','blue','mediumorchid']
#pie chart locations
xlon=[146.7,166,101.6,137.4,145.1,113.6,169.7,113.3,176.0,139.6,148.9,124.2,132.4,142.0,129.6,148.0,116.5,142.8,141.7,128.0,113.6,120.7,128.3,148.6]
ylat=[-42.2,-19.2,-0.5,-3.5,-34.4,-8.7,-45.1,-1.0,-38.6,-26.7,-29.1,-20.0,-14.4,-18.9,-31.3,-6.6,-23.8,-3.4,-7.5,-25.6,3.8,-3.1,-1.9,-23.2]
#function to draw pie charts on map
def draw_pie(ax,X=0, Y=0, size = 1500):
xy = []
start = 0.17
ratios=[1/12.]*12
for ratio in ratios:
x = [0] + np.cos(np.linspace(2*math.pi*start,2*math.pi*(start+ratio))).tolist() #30
y = [0] + np.sin(np.linspace(2*math.pi*start,2*math.pi*(start+ratio))).tolist() #30
xy1=(zip(x,y))
xy.append(xy1)
start -= ratio
piecolors = []
for lt in range(12):
c = australia_data[b,lt]-1
c=int(c)
piecolors.append(colors[c])
for i, xyi in enumerate(xy):
ax.scatter([X],[Y] , marker=(xyi,0), s=size, facecolor=piecolors[i],linewidth=0.5,alpha=.7)
australia_data[:,11] = 1
australia_data[:,4] = 3
australia_data[:,1] = 2
fig = plt.figure()
ax = fig.add_axes([.05,.01,.79,.95])
x1 = 90 #left
x2 = 180 #right
y1 = -50 #bottom
y2 = 10 #top
#Create the map
m = Basemap(resolution='l',projection='merc', llcrnrlat=y1,urcrnrlat=y2,llcrnrlon=x1,urcrnrlon=x2,lat_ts=0) #,lat_ts=(x1+x2)/2
m.drawcoastlines()
#plots pie charts:
for b in range(24):
X,Y=m(xlon[b],ylat[b])
draw_pie(ax,X, Y,size=400)
plt.savefig('australia_pies.png',dpi=400)
任何关于为什么发生这种情况(以及如何解决它!)的想法将不胜感激! 编辑:饼图中的楔形数量似乎是一个问题-将其减少到6会导致均匀的饼,但是7+会导致某些楔形“爆炸”。
答案 0 :(得分:0)
看着scatter piecharts example,您忘了根据从0到楔形弧的最大距离来调整饼形楔的大小。这是必要的,因为标记会在绘制之前规范化给定的路径,因此不同的楔形需要不同的大小才能在最终绘图中以相同的大小出现。
/Users/User/Documents/Folder/projectname/utils/configs(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)