CoffeeBean
CoffeeBean

Reputation: 5

"Exploding" wedges of pie chart when plotting them on a map (Python, matplotlib)

So I've successfully plotted pie charts on a map as markers using ax.scatter, but I'm having trouble with some of the wedges "exploding" out of the pie chart. I can't seem to find the reason for this in my code, and have been unable to find an explanation anywhere online. This code is based on the example here , which a colleague has also used and resulted in normal, uniform pie charts. Between us we can't find the issue, and no errors occur.

The code:

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)

Example map showing strange pie chart wedges

Any ideas as to why this is happening (and how to fix it!) would be greatly appreciated! Edit: it seems to be an issue with the number of wedges in the pie chart - reducing this to 6 results in uniform pies, but 7+ causes some wedges to "explode".

Upvotes: 0

Views: 3190

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339430

Looking at the scatter piecharts example, you forgot to adjust the size of the pie wedges according to the maximum distance from 0 to the the arc of the wedge. This is necessary because markers normalize the path given before drawing it, hence different wedges need different sizes in order to appear with the same size in the final plot.

import numpy as np
import matplotlib.pyplot as plt

#function to draw pie charts on map
def draw_pie(ax,X=0, Y=0, size = 1500): 
    xy = []; s=[]
    start = 0.0 
    ratios=[1/12.]*12
    for ratio in ratios:
        x = [0] + np.cos(np.linspace(2*np.pi*start,2*np.pi*(start+ratio))).tolist() #30
        y = [0] + np.sin(np.linspace(2*np.pi*start,2*np.pi*(start+ratio))).tolist() #30

        xy1 = np.column_stack([x, y])
        s1 = np.abs(xy1).max()

        xy.append(xy1)
        s.append(s1)
        start -= ratio

    for xyi, si in zip(xy,s):
       ax.scatter([X],[Y] , marker=(xyi,0), s=size*si**2, edgecolor="k")

fig, ax = plt.subplots() 

X,Y=166,50 
draw_pie(ax,X, Y,size=3000)

plt.show()

enter image description here

Upvotes: 1

Related Questions