Matplotlib: Grouped boxplots using data from numpy array and lists of group/subgroup labels -


i'm new matplotlib / python, , trying make grouped boxplot similar joe kington's excellent example shown here:

how make grouped boxplot graph in matplotlib

i'd modify joe's example own requirements.

for demo data below, have 5 individuals each have 4 attempts ( = "attempts": '1st','2nd','3rd','4th') @ each of 3 different tasks (= "tasks": 'a','b','c').

i'd able to:

1) input data series of 2d numpy arrays, 1 array per task shown, each composed of scores of 5 individuals nested within 4 sequential attempts.

2) label both tasks , attempts on shared x-axis of plot using strings, saved sequential items in lists "tasklist" , "attemptlist" respectively.

3) generalise solution make appropriate plots number of individuals, , number of tasks, each requiring number of repeated attempts.

edit: 2 apr 2015:

the problem outstanding seemingly counter-intuitive way python lists assemble non-sequential order when using .keys() method; hence tasklist keeps coming out "a,c,b" rather "a,b,c". workaround import , create ordered dictionary. new me, seem require item names in tasklist declared twice joe did in example - once associate tasks corresponding data matrices, , once associate item names in ordered dictionary corresponding sequential numeric keys...

was wondering: there method (akin .keys() method regular dictionaries) iterate on data matrices create ordered dictionary in order shown ("a,b,c"), without requiring me enter details of tasklist twice?

many thanks

dave

import matplotlib.pyplot plt import numpy np  data = {} data ['a'] = np.array([[1,2,3,4,9],[2,3,4,4,4],[3,4,4,5,5],[5,6,6,7,7,7]]) data ['b'] = np.array([[2,3,4,4,5],[3,4,5,6,10],[4,5,6,6,7],[5,6,7,7,8]]) data ['c'] = np.array([[4,5,6,6,10],[6,7,8,8,8],[7,8,9,9,10],[2,10,11,11,12]])  tasklist = data.keys() #  list of labels tasks 'a' 'c' (each containing 4 attempts labelled '1st' '4th') attemptlist = ['1st','2nd','3rd','4th'] # list of labels attempts 1 4 within each task  fig, axes = plt.subplots(ncols= len(tasklist), sharey=true) fig.subplots_adjust(wspace=0)  ax,task in zip(axes,tasklist):     ax.boxplot([data[task][attemptlist.index(attempt)] attempt in attemptlist],showfliers=false)     ax.set(xticklabels=attemptlist, xlabel=task) plt.show() 

@cphlewis: many thanks: on advice have re-written code data formatted list of tuples (task, data), , have control on order in tasks plotted.

mwe posted below in case helpful else.

import matplotlib.pyplot plt  data = [[('a'),[[1,2,3,4,9],[2,3,4,4,4],[3,4,4,5,5],[5,6,6,7,7,7]]],     [('b'),[[2,3,4,4,5],[3,4,5,6,10],[4,5,6,6,7],[5,6,7,7,8]]],      [('c'),[[4,5,6,6,10],[6,7,8,8,8],[7,8,9,9,10],[2,10,11,11,12]]]      ] attemptlist = ['1st','2nd','3rd','4th']  fig, axes = plt.subplots(ncols= len(data), sharey=true) fig.subplots_adjust(wspace=0)  ax,d in zip(axes,data):          ax.boxplot([d[1][attemptlist.index(attempt)] attempt in attemptlist],showfliers=false)     ax.set(xticklabels=attemptlist, xlabel=d[0]) plt.show() 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -