Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sunflower scatter plot using matplotlib

I am interested in constructing a sunflower scatter plot (as depicted in, for example, http://www.jstatsoft.org/v08/i03/paper [PDF link]). Before I write my own implementation, does anyone know of an existing one? I am aware of the functions in Stata and R, but am looking for one in matplotlib.

Thank you.

like image 569
cytochrome Avatar asked Mar 04 '14 03:03

cytochrome


1 Answers

I don't know of any matplotlib implementations but it's not hard to do. Here I let hexbin do the counting, and then go through each cell and add the appropriate number of petals:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

np.random.seed(0)
n = 2000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

cmap = colors.ListedColormap(['white', 'yellow', 'orange'])
hb = plt.hexbin(x,y, bins='log', cmap=cmap, gridsize=20, edgecolor='gray')
plt.axis([-2, 2, -12, 12])
plt.title("sunflower plot")

counts = hb.get_array()
coords = hb.get_offsets()

for i, count in enumerate(counts):
    x, y = coords[i,:]
    count = int(10**count)
    if count>3 and count<=12:
        n = count // 1
        if n>1:
            plt.plot([x], [y], 'k.')
            plt.plot([x], [y], marker=(n, 2), color='k', markersize=18)
    if count>12:
        n = count // 5
        if n>1:
            plt.plot([x], [y], 'k.')
            plt.plot([x], [y], marker=(n, 2), color='k', markersize=18)

plt.show()

Here yellow is 1 petal = 1, and orange 1 petal = 5.

One obvious place for improvement here is working with the colormap. For example, do you want to preset the colors boundaries or calculate them from the data, etc? Here I just kludged it a bit: I used bins='log' just to get a reasonable ratio between yellow and orange cells for the particular sample I used; and also I hard coded the borders between white, yellow, and orange cells (3 and 12).

Being able to use a tuple to specify the marker characteristics in matplotlib makes it really easy to draw all the different petal numbers.

like image 119
tom10 Avatar answered Sep 25 '22 22:09

tom10