Radar / Spider Chars

There was recently a post on Radar/Spider plotting
(http://sourceforge.net/mailarchive/message.php?msg_id=4845303A.9050204%40epcc.ed.ac.uk).
I too am interested in creating Radar plots with matplot. Is there a
simple way to do this?

Thanks,
Curtis

Here's a hack to get part of what you want:

···

On Jun 15, 2008, at 11:54 AM, Curtis Jensen wrote:

There was recently a post on Radar/Spider plotting
(http://sourceforge.net/mailarchive/message.php?msg_id=4845303A.9050204%40epcc.ed.ac.uk).
I too am interested in creating Radar plots with matplot. Is there a
simple way to do this?

=====
from matplotlib.projections.polar import PolarAxes
from pylab import *

# Create 6 points (plus 7th point that matches the first) with coordinates r, theta
N = 6
theta = 2 * pi * linspace(0, 1, N+1)
r = rand(N+1)
r[N] = r[0]

# HACK: force PolarAxes to use 1 line segment to connect specified points
PolarAxes.RESOLUTION = 1

ax = subplot(111, polar=True)
c = ax.plot(theta, r, 'r-o')
show()

I think this only works on matplotlib 0.98. I tried using rgrids and thetagrids to change the labels, but for some reason I was getting a TypeError when I called either of those functions.

-Tony

Nice. Thanks. I had tried to do something similar, but kept getting
a curved line between each data point.
Also, I too got errors with a previous versions of matplotlib, but 0.98 works.

If someone were willing to add Radar plots to the matplotlib
functionality, would this be wanted by the users or maintainers?

Thanks,
Curtis

···

On Tue, Jun 17, 2008 at 4:18 PM, Tony S Yu <tonyyu@...1166...> wrote:

On Jun 15, 2008, at 11:54 AM, Curtis Jensen wrote:

There was recently a post on Radar/Spider plotting

(http://sourceforge.net/mailarchive/message.php?msg_id=4845303A.9050204%40epcc.ed.ac.uk).
I too am interested in creating Radar plots with matplot. Is there a
simple way to do this?

Here's a hack to get part of what you want:

=====
from matplotlib.projections.polar import PolarAxes
from pylab import *

# Create 6 points (plus 7th point that matches the first) with coordinates
r, theta
N = 6
theta = 2 * pi * linspace(0, 1, N+1)
r = rand(N+1)
r[N] = r[0]

# HACK: force PolarAxes to use 1 line segment to connect specified points
PolarAxes.RESOLUTION = 1

ax = subplot(111, polar=True)
c = ax.plot(theta, r, 'r-o')
show()

I think this only works on matplotlib 0.98. I tried using rgrids and
thetagrids to change the labels, but for some reason I was getting a
TypeError when I called either of those functions.

-Tony

Yes, certainly. You may want to take a look at the polar
implementation (can you inherit from it?) to reuse as much as
possible. Michael has also written a short guide to developers
working with nonlinear projections

http://matplotlib.sourceforge.net/doc/html/devel/add_new_projection.html

JDH

···

On Wed, Jun 18, 2008 at 2:43 PM, Curtis Jensen <curtis@...2044...> wrote:

Nice. Thanks. I had tried to do something similar, but kept getting
a curved line between each data point.
Also, I too got errors with a previous versions of matplotlib, but 0.98 works.

If someone were willing to add Radar plots to the matplotlib
functionality, would this be wanted by the users or maintainers?

No guarantees on when I'll have time, but I'll work on it.

Thanks for the info.

···

--
Curtis

On Thu, Jun 19, 2008 at 8:17 AM, John Hunter <jdh2358@...287...> wrote:

On Wed, Jun 18, 2008 at 2:43 PM, Curtis Jensen <curtis@...2044...> wrote:

Nice. Thanks. I had tried to do something similar, but kept getting
a curved line between each data point.
Also, I too got errors with a previous versions of matplotlib, but 0.98 works.

If someone were willing to add Radar plots to the matplotlib
functionality, would this be wanted by the users or maintainers?

Yes, certainly. You may want to take a look at the polar
implementation (can you inherit from it?) to reuse as much as
possible. Michael has also written a short guide to developers
working with nonlinear projections

http://matplotlib.sourceforge.net/doc/html/devel/add_new_projection.html

JDH

I was reading through the projections docs and decided to take a shot at creating a radar chart class. The layout of the labels and legend is really off, but other than that, this seems to work OK. I couldn't figure out a good way to initialize the number of axes/variables, so there's an ugly function wrapping around the class def.

Anyway, I hope this is useful to somebody:

radar.py (3.11 KB)

Tony,

I know this is a year later but your code was hugely helpful to me last
week, so thank you. I needed to make a few modifications to get exactly what
I needed, so I thought I'd add to the post for posterity...

First, here is the graphic that the sample code generates (>>
execfile('radarPlotExample.py'):

http://www.nabble.com/file/p24688050/profileComparisonPub.png
profileComparisonPub.png

Suffice it to say, I think this looks way better than what you get out of R
or MATLAB (e.g.
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=123).

Here is the code. I have tried to add comments in places that differ from
Tony's code. I am a relatively new Python and matplotlib convert, so please
feel free to comment on better ways to do this. But, like Tony said,
hopefully this will help someone.

Josh

···

---------------------------------------------------------------------------------------
radarPlotExample.py
---------------------------------------------------------------------------------------
from matplotlib.projections.polar import PolarAxes
from matplotlib.projections import register_projection
from pylab import *

def radar_factory(num_vars, frame='polygon'):
    """Create a radar chart with `num_vars` axes.
    """
    # calculate evenly-spaced axis angles
    theta = 2*pi * linspace(0, 1-1/float(num_vars), num_vars)
    #print theta
    #print
    # rotate theta such that the first axis is at the top
    theta += pi/2
    
    def draw_poly_frame(self, x0, y0, r):
        # TODO: should use transforms to convert (x, y) to (r, theta)
        verts = [(r*cos(t) + x0, r*sin(t) + y0) for t in theta]
        return Polygon(verts, closed=True)
        
    def draw_circle_frame(self, x0, y0, r):
        return Circle((x0, y0), r)
        
    frame_dict = {'polygon': draw_poly_frame, 'circle': draw_circle_frame}
    if frame not in frame_dict:
        raise ValueError, 'unknown value for `frame`: %s' % frame
    
    class RadarAxes(PolarAxes):
        """Class for creating a radar chart (a.k.a. a spider or star chart)
        
        http://en.wikipedia.org/wiki/Radar_chart
        """
        name = 'radar'
        # use 1 line segment to connect specified points
        RESOLUTION = 1
        # define draw_frame method
        draw_frame = frame_dict[frame]

        def fill(self, *args, **kwargs):
            """Override fill so that line is closed by default"""
            closed = kwargs.pop('closed', True)
            return super(RadarAxes, self).fill(closed=closed, *args,
**kwargs)
            
        def plot(self, *args, **kwargs):
            """Override plot so that line is closed by default"""
            lines = super(RadarAxes, self).plot(*args, **kwargs)
            for line in lines:
                self._close_line(line)
        
        def _close_line(self, line):
            x, y = line.get_data()
            # FIXME: markers at x[0], y[0] get doubled-up
            if x[0] != x[-1]:
                x = concatenate((x, [x[0]]))
                y = concatenate((y, [y[0]]))
                line.set_data(x, y)
                
        def set_varlabels(self, labels, rvals, rlabels):
            self.set_thetagrids(theta * 180/pi, labels)
            #Josh says: The rvals and rlabels parameters were added to
support
            #the call to the set_rgrid method so you can control the
position
            #and labelling of the circular grid lines. Make the radii labels
            #smaller than the default size...
            self.set_rgrids(rvals, labels=rlabels, size='small')
            
        def get_axes_patch(self):
            x0, y0 = (0.5, 0.5)
            r = 0.5
            return self.draw_frame(x0, y0, r)
            
    register_projection(RadarAxes)
    return theta

if __name__ == '__main__':
    #The following data is from the Denver Aerosol Sources and Health study.
    #See doi:10.1016/j.atmosenv.2008.12.017
    #
    #The data are pollution source profile estimates for five modeled
pollution
    #sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical species.
    #The radar charts are experimented with here to see if we can nicely
    #visualize how the modeled source profiles change across four scenarios:
    # 1) No gas-phase species present, just seven particulate counts on
    # Sulfate
    # Nitrate
    # Elemental Carbon (EC)
    # Organic Carbon fraction 1 (OC)
    # Organic Carbon fraction 2 (OC2)
    # Organic Carbon fraction 3 (OC3)
    # Pyrolized Organic Carbon (OP)
    # 2)Inclusion of gas-phase specie carbon monoxide (CO)
    # 3)Inclusion of gas-phase specie ozone (O3).
    # 4)Inclusion of both gas-phase speciesis present...
    
    N = 9
    theta = radar_factory(N)
   
    f1_base = [0.88, 0.01, 0.03, 0.03, 0.00, 0.06, 0.01, 0.00, 0.00]
    f1_CO = [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00]
    f1_O3 = [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03]
    f1_both = [0.87, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01]

    f2_base = [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00]
    f2_CO = [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00]
    f2_O3 = [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00]
    f2_both = [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00]

    f3_base = [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00]
    f3_CO = [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00]
    f3_O3 = [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00]
    f3_both = [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00]

    f4_base = [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]
    f4_CO = [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00]
    f4_O3 = [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95]
    f4_both = [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88]
    
    f5_base = [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00]
    f5_CO = [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00]
    f5_O3 = [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00]
    f5_both = [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16]

    fig = figure(figsize=(9,9))
    fig.subplots_adjust(wspace=0.25, hspace=0.20)
    axlist = []
    axisNum = 0
    #The base vs with-gas ordering of the modeled profiles is swapped for
    #factors 4/5, so we'll swap their ordering in the basecase list just to
keep
    #the coloring consistent across the four plots...
    bases = [f1_base, f2_base, f3_base, f5_base, f4_base]
    COs = [f1_CO, f2_CO, f3_CO, f4_CO, f5_CO]
    O3s = [f1_O3, f2_O3, f3_O3, f4_O3, f5_O3]
    boths = [f1_both, f2_both, f3_both, f4_both, f5_both]
    everything = [bases, COs, O3s, boths]
    titles = ['Basecase', 'With CO', 'With O3', 'CO & O3']
    colors = ['b', 'r', 'g', 'm', 'y']
    for row in range(2):
        for col in range(2):
            axisNum += 1
            if axisNum == 2:
                #Unfortunately, it looks like the loc keyword to legend() is
                #relative to a specific subplot, rather than the figure
itself.
                #So, the positioning seen looks good, but if you resize the
                #figure to be larger the legend becomes obviously bound to a
                #specific subplot. This is in contrast to how the position
works
                #in something like figtext(). Had trouble using figlegend(),
but
                #need to try some more...
                legend(('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4',
                        'Factor 5'), loc=(0.95, 0.895), borderpad=0.01,
                        shadow=False, prop=matplotlib.font_manager
                        .FontProperties(size='smaller'), markerscale=0.4)
                                
            data = everything[axisNum-1]
            ax = fig.add_subplot(2, 2, axisNum, projection='radar')
            ax.set_title(titles[axisNum-1], weight='bold', size='medium',
                         horizontalalignment='center',
                         verticalalignment='center',
backgroundcolor='white',
                         position=(0.5, 1.1))
            p1 = ax.plot(theta, data[0], color=colors[0])
            p2 = ax.plot(theta, data[1], color=colors[1])
            p3 = ax.plot(theta, data[2], color=colors[2])
            p4 = ax.plot(theta, data[3], color=colors[3])
            p5 = ax.plot(theta, data[4], color=colors[4])
            ax.fill(theta, data[0], facecolor=colors[0])
            ax.fill(theta, data[1], facecolor=colors[1])
            ax.fill(theta, data[2], facecolor=colors[2])
            ax.fill(theta, data[3], facecolor=colors[3])
            ax.fill(theta, data[4], facecolor=colors[4])
            #axlist.extend(ax) #This does not work because ax is a
                               #RadarAxesSubplot object, which is not
iterable
            axlist.append(ax) #append() works because it simply tacks on to
                               #the list, as opposed to merging items from
two
                               #lists
            for patch in ax.patches:
                patch.set_alpha(0.25)

    figtext(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios
',
                ha='center', color='black', weight='bold', size='large')
    
    #Crudely plot the grid lines I want to see: normalized concentrations of
    #chemicals range from 0 to 1...
    radiiGrid = [0.2, 0.4, 0.6, 0.8]
    theta_rgrid = radar_factory(100)
    for ax in axlist:
        for r in radiiGrid:
            radii = repeat(r, 100)
            ax.plot(theta_rgrid, radii, color='lightgrey')
    
    # FIXME: legend doesn't work when fill is called
    spokeLabels = ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP',
'CO',
                   'O3']
    radiiLabels = [str(rg) for rg in radiiGrid]
    for ax in axlist:
        ax.set_varlabels(spokeLabels, radiiGrid, radiiLabels)

    show()

View this message in context: http://www.nabble.com/Radar---Spider-Chars-tp17876254p24688050.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Would you (Josh and Tony) be amenable to us including this in the set of examples? It would make it easier for users to find it. Eventually, it might be nice to include this as a core plotting command, but in the meantime, I think it would still be useful as-is.

Mike

Josh Hemann wrote:

···

Tony,

I know this is a year later but your code was hugely helpful to me last
week, so thank you. I needed to make a few modifications to get exactly what
I needed, so I thought I'd add to the post for posterity...

First, here is the graphic that the sample code generates (>>
execfile('radarPlotExample.py'):

http://www.nabble.com/file/p24688050/profileComparisonPub.png
profileComparisonPub.png

Suffice it to say, I think this looks way better than what you get out of R
or MATLAB (e.g.
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=123).

Here is the code. I have tried to add comments in places that differ from
Tony's code. I am a relatively new Python and matplotlib convert, so please
feel free to comment on better ways to do this. But, like Tony said,
hopefully this will help someone.

Josh

---------------------------------------------------------------------------------------
radarPlotExample.py
---------------------------------------------------------------------------------------
from matplotlib.projections.polar import PolarAxes from matplotlib.projections import register_projection from pylab import *

def radar_factory(num_vars, frame='polygon'): """Create a radar chart with `num_vars` axes. """ # calculate evenly-spaced axis angles theta = 2*pi * linspace(0, 1-1/float(num_vars), num_vars) #print theta
    #print
    # rotate theta such that the first axis is at the top theta += pi/2
        def draw_poly_frame(self, x0, y0, r): # TODO: should use transforms to convert (x, y) to (r, theta) verts = [(r*cos(t) + x0, r*sin(t) + y0) for t in theta] return Polygon(verts, closed=True)
            def draw_circle_frame(self, x0, y0, r): return Circle((x0, y0), r)
            frame_dict = {'polygon': draw_poly_frame, 'circle': draw_circle_frame} if frame not in frame_dict: raise ValueError, 'unknown value for `frame`: %s' % frame
        class RadarAxes(PolarAxes): """Class for creating a radar chart (a.k.a. a spider or star chart)
                http://en.wikipedia.org/wiki/Radar_chart """ name = 'radar' # use 1 line segment to connect specified points RESOLUTION = 1 # define draw_frame method draw_frame = frame_dict[frame]

        def fill(self, *args, **kwargs): """Override fill so that line is closed by default""" closed = kwargs.pop('closed', True) return super(RadarAxes, self).fill(closed=closed, *args,
**kwargs)
                    def plot(self, *args, **kwargs): """Override plot so that line is closed by default""" lines = super(RadarAxes, self).plot(*args, **kwargs) for line in lines: self._close_line(line)
                def _close_line(self, line): x, y = line.get_data() # FIXME: markers at x[0], y[0] get doubled-up if x[0] != x[-1]: x = concatenate((x, [x[0]])) y = concatenate((y, [y[0]])) line.set_data(x, y)
                        def set_varlabels(self, labels, rvals, rlabels): self.set_thetagrids(theta * 180/pi, labels) #Josh says: The rvals and rlabels parameters were added to
support
            #the call to the set_rgrid method so you can control the
position
            #and labelling of the circular grid lines. Make the radii labels #smaller than the default size...
            self.set_rgrids(rvals, labels=rlabels, size='small')
                    def get_axes_patch(self): x0, y0 = (0.5, 0.5) r = 0.5 return self.draw_frame(x0, y0, r)
                register_projection(RadarAxes) return theta

    if __name__ == '__main__': #The following data is from the Denver Aerosol Sources and Health study. #See doi:10.1016/j.atmosenv.2008.12.017 #
    #The data are pollution source profile estimates for five modeled
pollution
    #sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical species.
    #The radar charts are experimented with here to see if we can nicely #visualize how the modeled source profiles change across four scenarios:
    # 1) No gas-phase species present, just seven particulate counts on
    # Sulfate
    # Nitrate
    # Elemental Carbon (EC)
    # Organic Carbon fraction 1 (OC)
    # Organic Carbon fraction 2 (OC2)
    # Organic Carbon fraction 3 (OC3)
    # Pyrolized Organic Carbon (OP)
    # 2)Inclusion of gas-phase specie carbon monoxide (CO) # 3)Inclusion of gas-phase specie ozone (O3). # 4)Inclusion of both gas-phase speciesis present...
        N = 9
    theta = radar_factory(N)
       f1_base = [0.88, 0.01, 0.03, 0.03, 0.00, 0.06, 0.01, 0.00, 0.00]
    f1_CO = [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00] f1_O3 = [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03] f1_both = [0.87, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01]

    f2_base = [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00]
    f2_CO = [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00] f2_O3 = [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00] f2_both = [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00]

    f3_base = [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00]
    f3_CO = [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00] f3_O3 = [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00] f3_both = [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00]

    f4_base = [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]
    f4_CO = [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00] f4_O3 = [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95] f4_both = [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88]
        f5_base = [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00]
    f5_CO = [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00] f5_O3 = [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00] f5_both = [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16]

    fig = figure(figsize=(9,9))
    fig.subplots_adjust(wspace=0.25, hspace=0.20)
    axlist = []
    axisNum = 0
    #The base vs with-gas ordering of the modeled profiles is swapped for #factors 4/5, so we'll swap their ordering in the basecase list just to
keep #the coloring consistent across the four plots...
    bases = [f1_base, f2_base, f3_base, f5_base, f4_base]
    COs = [f1_CO, f2_CO, f3_CO, f4_CO, f5_CO]
    O3s = [f1_O3, f2_O3, f3_O3, f4_O3, f5_O3]
    boths = [f1_both, f2_both, f3_both, f4_both, f5_both]
    everything = [bases, COs, O3s, boths]
    titles = ['Basecase', 'With CO', 'With O3', 'CO & O3']
    colors = ['b', 'r', 'g', 'm', 'y']
    for row in range(2):
        for col in range(2):
            axisNum += 1
            if axisNum == 2:
                #Unfortunately, it looks like the loc keyword to legend() is #relative to a specific subplot, rather than the figure
itself. #So, the positioning seen looks good, but if you resize the #figure to be larger the legend becomes obviously bound to a #specific subplot. This is in contrast to how the position
works
                #in something like figtext(). Had trouble using figlegend(),
but
                #need to try some more...
                legend(('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5'), loc=(0.95, 0.895), borderpad=0.01, shadow=False, prop=matplotlib.font_manager
                        .FontProperties(size='smaller'), markerscale=0.4)
                                            data = everything[axisNum-1]
            ax = fig.add_subplot(2, 2, axisNum, projection='radar')
            ax.set_title(titles[axisNum-1], weight='bold', size='medium', horizontalalignment='center', verticalalignment='center',
backgroundcolor='white', position=(0.5, 1.1))
            p1 = ax.plot(theta, data[0], color=colors[0]) p2 = ax.plot(theta, data[1], color=colors[1])
            p3 = ax.plot(theta, data[2], color=colors[2])
            p4 = ax.plot(theta, data[3], color=colors[3])
            p5 = ax.plot(theta, data[4], color=colors[4])
            ax.fill(theta, data[0], facecolor=colors[0]) ax.fill(theta, data[1], facecolor=colors[1]) ax.fill(theta, data[2], facecolor=colors[2]) ax.fill(theta, data[3], facecolor=colors[3]) ax.fill(theta, data[4], facecolor=colors[4]) #axlist.extend(ax) #This does not work because ax is a #RadarAxesSubplot object, which is not
iterable
            axlist.append(ax) #append() works because it simply tacks on to #the list, as opposed to merging items from
two
                               #lists
            for patch in ax.patches: patch.set_alpha(0.25)

                                               figtext(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios
', ha='center', color='black', weight='bold', size='large')
        #Crudely plot the grid lines I want to see: normalized concentrations of
    #chemicals range from 0 to 1...
    radiiGrid = [0.2, 0.4, 0.6, 0.8]
    theta_rgrid = radar_factory(100)
    for ax in axlist:
        for r in radiiGrid:
            radii = repeat(r, 100)
            ax.plot(theta_rgrid, radii, color='lightgrey')
        # FIXME: legend doesn't work when fill is called spokeLabels = ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP',
'CO', 'O3']
    radiiLabels = [str(rg) for rg in radiiGrid]
    for ax in axlist:
        ax.set_varlabels(spokeLabels, radiiGrid, radiiLabels)

    show()
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Michael Droettboom-3 wrote:

Would you (Josh and Tony) be amenable to us including this in the set of
examples? It would make it easier for users to find it. Eventually, it
might be nice to include this as a core plotting command, but in the
meantime, I think it would still be useful as-is.

Mike

Mike,

I would be all for including it as an example, and agree that it would be
nice to see this routine made more robust for inclusion into matplotlib. I'd
volunteer, but I am still on the steep part of the learning curve. Maybe in
coming months...

FYI I have some other examples I was thinking would be useful. Here is an
enhanced boxplot example
that might compliment the simple examples well:

http://www.nabble.com/file/p24705282/boxplotExample.png

Its based on the same data as in the radar charts, just a different
visualization (a more detailed, side by side comparison between the
with-and-without-carbon-monoxide solution variability across hundreds of
simulations). The upper x-axis labels are the box medians, so you can scan
these labels for a faster gleaning of the actual numbers.

Cheers,

Josh

···


View this message in context: http://www.nabble.com/Radar---Spider-Chars-tp17876254p24705282.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Please! That example with the top labels looks great, and it's a very
useful way of displaying the numerical key parts of the dataset.

Cheers,

f

···

On Tue, Jul 28, 2009 at 11:26 AM, Josh Hemann<jhemann@...1899...> wrote:

FYI I have some other examples I was thinking would be useful. Here is an
enhanced boxplot example
that might compliment the simple examples well:

http://www.nabble.com/file/p24705282/boxplotExample.png

Josh Hemann wrote:

Tony,

I know this is a year later but your code was hugely helpful to me last
week, so thank you

I'm glad you found it helpful.

Would you (Josh and Tony) be amenable to us including this in the set of
examples? It would make it easier for users to find it. Eventually, it
might be nice to include this as a core plotting command, but in the
meantime, I think it would still be useful as-is.

Mike

Hey Mike,

I'm always happy to contribute what little I can to matplotlib. However, if it's going to be an official example, it should be cleaned up a bit (see attached). Summary of changes.

* My original example (i.e. not Josh's fault) didn't play well with namespaces (i.e. from pylab import *). This has been changed to use the proper imports (i.e. np and plt).

* Also, there were problems with rgrids when I originally wrote the code, which has since been fixed (at least on trunk it is). This eliminates the need to manually draw a grid.

* I made a few other clean ups for clarity. (Josh: I hope you don't mind, I switched the data for f4_base and f5_base so that I could remove the associated comment.)

* Final note. The polygon frame no longer works properly and I couldn't really figure out how to fix it.

Best,
-Tony

radar_chart.py (6.39 KB)

···

On Jul 28, 2009, at 12:56 PM, Michael Droettboom wrote:

Thanks Tony -- I added this to examples/api/radar_chart.py in svn and
it will show up in the examples and gallery next time I update the
website.

JDH

···

On Tue, Jul 28, 2009 at 2:27 PM, Tony S Yu<tonyyu@...1166...> wrote:

I'm always happy to contribute what little I can to matplotlib. However, if
it's going to be an official example, it should be cleaned up a bit (see
attached). Summary of changes.

Tony,

This looks great, and I am enjoying seeing how to accomplish things in a
more Pythonic way. I don't see the radial grid lines, but I am using
0.98.5.3. I am downloading 0.99.3 now and maybe that will be the fix (I have
yet to figure out how to build out of svn on Windows...). One small nit: I
don't see any code to set the color or alpha level of the grid lines. In my
example, I set the color to be a light grey because I wanted the grid lines
to be seen but not be distracting from the data. Just a preference.

Cheers,

Josh

Josh Hemann wrote:

Tony,

I know this is a year later but your code was hugely helpful to me
last
week, so thank you

I'm glad you found it helpful.

Would you (Josh and Tony) be amenable to us including this in the
set of
examples? It would make it easier for users to find it.
Eventually, it
might be nice to include this as a core plotting command, but in the
meantime, I think it would still be useful as-is.

Mike

Hey Mike,

I'm always happy to contribute what little I can to matplotlib.
However, if it's going to be an official example, it should be cleaned
up a bit (see attached). Summary of changes.

* My original example (i.e. not Josh's fault) didn't play well with
namespaces (i.e. from pylab import *). This has been changed to use
the proper imports (i.e. np and plt).

* Also, there were problems with rgrids when I originally wrote the
code, which has since been fixed (at least on trunk it is). This
eliminates the need to manually draw a grid.

* I made a few other clean ups for clarity. (Josh: I hope you don't
mind, I switched the data for f4_base and f5_base so that I could
remove the associated comment.)

* Final note. The polygon frame no longer works properly and I
couldn't really figure out how to fix it.

Best,
-Tony
<\quote>

···

On Jul 28, 2009, at 12:56 PM, Michael Droettboom wrote:

View this message in context: http://www.nabble.com/Radar---Spider-Chars-tp17876254p24707635.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Fernando Perez wrote:

FYI I have some other examples I was thinking would be useful. Here is an
enhanced boxplot example
that might compliment the simple examples well:

http://www.nabble.com/file/p24705282/boxplotExample.png

Please! That example with the top labels looks great, and it's a very
useful way of displaying the numerical key parts of the dataset.

Cheers,

f

Great! I'll get started on extracting the plotting code from a larger
application I have, and I'll do my best to clean it up. Unless I hear
otherwise, I'll just post the code in this thread and let you guys run with
it if you want to.

Josh

···

On Tue, Jul 28, 2009 at 11:26 AM, Josh Hemann<jhemann@...1899...> wrote:


View this message in context: http://www.nabble.com/Radar---Spider-Chars-tp17876254p24707753.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

One small nit: I
don't see any code to set the color or alpha level of the grid lines. In my
example, I set the color to be a light grey because I wanted the grid lines
to be seen but not be distracting from the data. Just a preference.

Good point. It turns out the grid lines are already pretty faint on my computer, so I didn't need to adjust this. The most obvious thing to do is replace

>>> plt.rgrids(radial_grid)

with

>>> lines, labels = plt.rgrids(radial_grid)
>>> plt.setp(lines, alpha=0.4)

**However**, this modification doesn't help. In fact, nothing I did to the gridlines changed their appearance. Out of frustration, I went to the code and found that `rgrids` calls *get_ticklines* instead of *get_gridlines*. My guess is that this is a typo, but there may be a compelling reason to return the tick lines. If that's not the case, here's a ridiculously simple patch:

Index: lib/matplotlib/projections/polar.py

···

On Jul 28, 2009, at 4:53 PM, Josh Hemann wrote:

--- lib/matplotlib/projections/polar.py (revision 7300)
+++ lib/matplotlib/projections/polar.py (working copy)
@@ -397,7 +397,7 @@
          self._r_label2_position.clear().translate(angle, -self._rpad * rmax)
          for t in self.yaxis.get_ticklabels():
              t.update(kwargs)
- return self.yaxis.get_ticklines(), self.yaxis.get_ticklabels()
+ return self.yaxis.get_gridlines(), self.yaxis.get_ticklabels()

      set_rgrids.__doc__ = cbook.dedent(set_rgrids.__doc__) % kwdocd

Fernando Perez wrote:

Please! That example with the top labels looks great, and it's a very
useful way of displaying the numerical key parts of the dataset.

Cheers,

f

OK, here is a bloxplot example based on the one previously shown. I just
replaced my environmental data with some random data to make things easier
to run, and accordingly I had to make up some story around the data (testing
bootstrap resampling). Fee free to rework the code as you see fit, but
hopefully this is a helpful example.

http://www.nabble.com/file/p24764036/boxplotExampleForums.png

···

--------------------------
boxplotdemo.py
--------------------------
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

#Generate some data from five different probability distributions, each with
#different characteristics. We want to play with how an IID bootstrap
resample
#of the data preserves the distributional properties of the original sample,
and
#a boxplot is one visual tool to make this assessment
numDists = 5
randomDists = ['Normal(1,1)',' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)',
               'Triangular(2,9,11)']
N = 500
norm = np.random.normal(1,1, N)
logn = np.random.lognormal(1,1, N)
expo = np.random.exponential(1, N)
gumb = np.random.gumbel(6, 4, N)
tria = np.random.triangular(2, 9, 11, N)

#Generate some random indices that we'll use to resample the original data
#arrays. For code brevity, just use the same random indices for each array
bootstrapIndices = np.random.random_integers(0, N-1, N)
normBoot = norm[bootstrapIndices]
expoBoot = expo[bootstrapIndices]
gumbBoot = gumb[bootstrapIndices]
lognBoot = logn[bootstrapIndices]
triaBoot = tria[bootstrapIndices]

data = [norm, normBoot, logn, lognBoot, expo, expoBoot, gumb, gumbBoot,
        tria, triaBoot]

fig = plt.figure(figsize=(10,6))
fig.canvas.set_window_title('A Boxplot Example')
ax1 = fig.add_subplot(111)
plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25)

bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['fliers'], color='red', marker='+')

#Add a horizontal grid to the plot, but make it very light in color so we
can
#use it for reading data values but not be distracting
ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey',
               alpha=0.5)
#Hide these grid behind plot objects
ax1.set_axisbelow(True)
ax1.set_title('Comparison of IID Bootstrap Resampling Across Five
Distributions')
ax1.set_xlabel('Distribution')
ax1.set_ylabel('Value')

#Now fill the boxes with desired colors
boxColors = ['darkkhaki','royalblue']
numBoxes = numDists*2
medians = range(numBoxes)
for i in range(numBoxes):
   box = bp['boxes'][i]
   boxX = []
   boxY = []
   for j in range(5):
       boxX.append(box.get_xdata()[j])
       boxY.append(box.get_ydata()[j])
   boxCoords = zip(boxX,boxY)
   #Alternate between Dark Khaki and Royal Blue
   k = i % 2
   boxPolygon = Polygon(boxCoords, facecolor=boxColors[k])
   ax1.add_patch(boxPolygon)
   #Now draw the median lines back over what we just filled in
   med = bp['medians'][i]
   medianX = []
   medianY = []
   for j in range(2):
       medianX.append(med.get_xdata()[j])
       medianY.append(med.get_ydata()[j])
       plt.plot(medianX, medianY, 'k')
       medians[i] = medianY[0]
   #Finally, overplot the sample averages, with horixzontal alignment in the
   #center of each box
   plt.plot([np.average(med.get_xdata().data)], [np.average(data[i])],
            color='w', marker='*', markeredgecolor='k')

#Set the axes ranges and axes labels
ax1.set_xlim(0.5, numBoxes+0.5)
top = 40
bottom = -5
ax1.set_ylim(bottom, top)
xtickNames = plt.setp(ax1, xticklabels=np.repeat(randomDists, 2))
plt.setp(xtickNames, rotation=45, fontsize=8)

#Due to the Y-axis scale being different across samples, it can be hard to
#compare differences in medians across the samples. Add upper X-axis tick
labels
#with the sample medians to aid in comparison (just use two decimal places
of
#precision)
pos = np.arange(numBoxes)+1
upperLabels = [str(np.round(s, 2)) for s in medians]
weights = ['bold', 'semibold']
for tick,label in zip(range(numBoxes),ax1.get_xticklabels()):
    k = tick % 2
    ax1.text(pos[tick], top-(top*0.05), upperLabels[tick],
         horizontalalignment='center', size='x-small', weight=weights[k],
         color=boxColors[k])

#Finally, add a basic legend
plt.figtext(0.80, 0.08, str(N) + ' Random Numbers' ,
            backgroundcolor=boxColors[0], color='black', weight='roman',
            size='x-small')
plt.figtext(0.80, 0.045, 'IID Bootstrap Resample',
backgroundcolor=boxColors[1],
            color='white', weight='roman', size='x-small')
plt.figtext(0.80, 0.015, '*', color='white', backgroundcolor='silver',
            weight='roman', size='medium')
plt.figtext(0.815, 0.013, ' Average Value', color='black', weight='roman',
            size='x-small')

plt.show()

-----
Josh Hemann
Statistical Advisor
http://www.vni.com/ Visual Numerics
jhemann@...1899... | P 720.407.4214 | F 720.407.4199


View this message in context: http://www.nabble.com/Radar---Spider-Chars-tp17876254p24764036.html
Sent from the matplotlib - users mailing list archive at Nabble.com.