Category ArchiveGeneric Mapping Tools
Coding Tips & Computers & Generic Mapping Tools 01 Aug 2008 03:37 pm
Creating Pie Wedge plots in GMT
A coworker recently approached me if asked if I knew how to make Pie Wedge plots in the Generic Mapping tools using the -Sw(w) switch. I had never done this before, but I thought it would be a cool thing to do so I tried my hand a making one.
It was tougher than I thought, and while I have seen these types of plots quite a bit in the fisheries world, there didn’t seem to be any examples of how to do this. So I thought I would post up here what I did, both for myself in the future and for anyone else in the world who may be interested in this type of plot.
Basically what I want to do is to make a dummy plot with a pie chart centered at every 5×5 degree box with a different size outer circle based on the total number in the box, and wedges representing percentages of that total amount.
For this exercise I am using the PXSY routine of GMT4.2.0 with my default MEASURE_UNIT = in.
To make this chart you have to have 5 columns of data:
Longitude — Latitude — Radius — StartAngle — EndAngle
So say I want a pie chart to represent how many types of widgets I sold in the area from 160-155W, 18-23N, with each wedge a portion of the widgets and centered in the middle of the 5×5 box. Here’s the data:
#lon lat blueWidget greenWidget redWidget
-157.5 20.5 200 200 400
So let’s say I make a map where each inch = 1 degree. The largest that I want my pie wedge diameter is 1 inch, so I know that for this example (only one data point) I will make the radius 0.5. I also know that the total for this example point is 800, so I convert this into angles. I actually have to make 3 rows of data now since I have three widgets. I also converted to 0-360 degrees.
$>cat pienc.xy
#lon lat radius startAngle endAngle
202.5 20.5 0.5 0 90 #end angle is 360 * (200/800)
202.5 20.5 0.5 90 180 #start angle is row-1 end angle
202.5 20.5 0.5 180 360 #Finish circle
So, a nice shell script to plot this up with the output below:
$>cat pienc
#!/bin/ksh
psfile=pie.ps
psbasemap -Jm1 -R200/206/17/23 -Bf1a1g1/f1a1g1WeSn -X1.5 -Y4 -P -K > $psfile
pscoast -Jm -R -O -K -Di -G200/200/200 -W1/0/0/0 >> $psfile
psxy pienc.xy -Sw -Jm -R -O -K >> $psfile
echo “203 24 12 0 0 6 Pie Chart Example” | pstext -Jm -R -O -N >> $psfile
$>display pie.ps
So that’s all well and good, except it would be nice to have different colors for each wedge representing a different widget. To get color in there you have to give a new column of values that will be mapped to a color value in a color lookup table (a cptfile in GMT). This column must be in the third row and then the -C switch must be given in the psxy call.
$>cat pie.xy
#lon lat COLORVALUE radius startAngle endAngle
202.5 20.5 1 0.5 0 90 #end angle is 360 * (200/800)
202.5 20.5 2 0.5 90 180 #start angle is row-1 end angle
202.5 20.5 3 0.5 180 360 #Finish circle
And my cptfile:
$>cat pie.cpt
0 0 0 255 1.1 0 0 255
1.1 0 255 0 2.1 0 255 0
2.1 255 0 0 3.1 255 0 0
The adjusted script:
$>cat pie
#!/bin/ksh
psfile=pie.ps
psbasemap -Jm1 -R200/206/17/23 -Bf1a1g1/f1a1g1WeSn -X1.5 -Y4 -P -K > $psfile
pscoast -Jm -R -O -K -Di -G200/200/200 -W1/0/0/0 >> $psfile
psxy pie.xy -Sw -Jm -R -O -K -Cpie.cpt >> $psfile
echo “203 24 12 0 0 6 Pie Chart Example with Color” | pstext -Jm -R -O -N >> $psfile
$>display pie.ps
And that’s pretty much it. Now to go sell some more widgets.
Computers & Generic Mapping Tools 03 Jan 2007 10:41 am
Generic Mapping Tools 4 DEGREE_FORMAT errors
There was huge changes between versions 3 and 4 in the way that GMT dealt with plotting degrees and degree formatting. I tend to use GMT 4 exclusively now, but on one of my work machines we have both versions installed (for backwards compatibility.) I had a problem recently trying to use GMT 3.4.5 with .grd files created by GMT 4.1.3. Specifically, the errors I would get were:
pstext: WARNING: DEGREE_FORMAT decoded (1) but is obsolete. Please use PLOT_DEGREE_FORMAT (ddd:mm:ss)
GMT Warning: Selected character encoding does not have suitable degree symbol - will use space instead
GMT Warning: Selected character encoding does not have minute symbol (single quote) - will use space instead
GMT Warning: Selected character encoding does not have second symbol (double quote) - will use space instead




