0

l would like to make histogram (bars). l have a pair of arrays :

1) classes_1, proba_1 2) classes_2, proba_2 each array has 101 elements. classes_1/classes_2 are strings (x coordinates) and proba_1, proba_2 are float.

My purpose :

make in one plot bar histogram for (classes_1, proba_1) and (classes_2, proba_2)

Problem : classes_2 and classes_1 both contains 101 elements. They have the same elements but in different order such that :

classes_1 == classes_2
False

My questions :

1) How can l get classes_1 and classes_2 into the same order ? The new order in classes_1 and classes_2 should update the order in proba_1 and proba_2 .

classes_1 and classes_2 are lists <class 'list'> proba_1 and proba_2 are <class 'numpy.ndarray'>

2) Given the ordered list of classes, l would like to make selected classes in a different color (let's say black color)

What l have tried:

    import numpy as np
    import  matplotlib.pyplot as plt
    classes_1 == classes_2
    classes_1 = sorted(classes_1)
    classes_2=sorted(classes_2)
    # Only classes that are sorted but not proba_1 and proba_2
    fig2, ax2 = plt.subplots()
    ax2.tick_params(rotation=90)
    ax2.bar(classes_1 +  classes_2, [0.1] * len(classes_1 +  classes_2), visible=False)
    # How can l specify the list of classes to be plotted in differet color ?
    ax2.bar(classes_1, proba_1, label=' classes 1')
    ax2.bar(classes_2, proba_2, label=' classes 2')
    ax2.set_xlabel("classes", rotation='vertical')
    ax2.set_ylabel("performance")
    ax2.legend(loc='best')

Here are my data

classes_1

['tennisswing', 'soccerjuggling', 'stillrings', 'drumming', 'rafting', 'wallpushups', 'cuttinginkitchen', 'writingonboard', 'salsaspin', 'diving', 'surfing', 'headmassage', 'mixing', 'playingguitar', 'skydiving', 'horseriding', 'trampolinejumping', 'swing', 'basketballdunk', 'moppingfloor', 'biking', 'hammerthrow', 'jugglingballs', 'longjump', 'boxingboxingspeedbag', 'rowing', 'playingtabla', 'throwdiscus', 'shotput', 'knitting', 'javelinthrow', 'baseballpitch', 'pommelhorse', 'playingdhol', 'playingdaf', 'pushups', 'nunchucks', 'bodyweightsquats', 'hammering', 'sumowrestling', 'walkingwithdog', 'skiing', 'haircut', 'unevenbars', 'playingsitar', 'playingviolin', 'applylipstick', 'cliffdiving', 'skateboarding', 'icedancing', 'pullups', 'bowling', 'babycrawling', 'shavingbeard', 'breaststroke', 'cricketbowling', 'highjump', 'horserace', 'floorgymnastics', 'jumpingjack', 'militaryparade', 'archery', 'playingflute', 'typing', 'benchpress', 'rockclimbingindoor', 'golfswing', 'fencing', 'soccerpenalty', 'applyeyemakeup', 'kayaking', 'skijet', 'taichi', 'pizzatossing', 'punch', 'lunges', 'handstandpushups', 'yoyo', 'playingcello', 'parallelbars', 'fieldhockeypenalty', 'frontcrawl', 'blowingcandles', 'bandmarching', 'playingpiano', 'hulahoop', 'jumprope', 'handstandwalking', 'brushingteeth', 'ropeclimbing', 'cricketshot', 'balancebeam', 'boxingspeedbag', 'polevault', 'cleanandjerk', 'billiards', 'basketball', 'frisbeecatch', 'blowdryhair', 'tabletennisshot', 'volleyballspiking']

proba_1

array([ 0.85416667,  0.30769231,  0.7       ,  0.88372093,  0.87878788,
        0.33333333,  0.80645161,  0.62162162,  0.38461538,  0.95454545,
        0.97368421,  0.75      ,  0.97727273,  0.97826087,  1.        ,
        0.86363636,  0.82857143,  0.76470588,  1.        ,  0.64285714,
        0.63888889,  0.7173913 ,  0.38235294,  0.73809524,  0.56097561,
        0.70454545,  0.8125    ,  0.18918919,  0.30952381,  0.88888889,
        0.63636364,  0.69230769,  0.79411765,  0.83333333,  0.90243902,
        0.82142857,  0.275     ,  0.375     ,  0.65853659,  1.        ,
        0.85714286,  0.97297297,  0.67567568,  0.92857143,  0.97674419,
        0.85714286,  0.51724138,  0.92682927,  0.82857143,  1.        ,
        0.28571429,  0.95348837,  0.85714286,  0.23255814,  0.71428571,
        0.28205128,  0.67647059,  1.        ,  0.68571429,  0.22222222,
        0.77777778,  0.65853659,  0.83333333,  0.65217391,  0.88636364,
        0.87179487,  0.72727273,  0.60606061,  0.9       ,  0.56410256,
        0.86111111,  0.85714286,  0.35714286,  0.44827586,  0.83333333,
        0.48717949,  0.37837838,  0.54285714,  0.84444444,  0.72727273,
        0.86486486,  0.63157895,  0.63333333,  0.88888889,  0.89285714,
        0.33333333,  0.23076923,  0.21428571,  0.29411765,  0.15151515,
        0.36734694,  0.89655172,  0.65      ,  0.74358974,  0.81818182,
        1.        ,  0.39393939,  0.88235294,  0.6       ,  0.63636364,  1.        ])

classes_2

['wallpushups', 'taichi', 'salsaspin', 'knitting', 'biking', 'throwdiscus', 'stillrings', 'skateboarding', 'playingtabla', 'bodyweightsquats', 'hammering', 'swing', 'playingflute', 'cricketbowling', 'billiards', 'playingviolin', 'cuttinginkitchen', 'cricketshot', 'ropeclimbing', 'unevenbars', 'applyeyemakeup', 'babycrawling', 'blowingcandles', 'writingonboard', 'playingdaf', 'pushups', 'polevault', 'surfing', 'baseballpitch', 'blowdryhair', 'highjump', 'jugglingballs', 'skydiving', 'playingdhol', 'rockclimbingindoor', 'diving', 'militaryparade', 'playingpiano', 'tabletennisshot', 'pommelhorse', 'archery', 'volleyballspiking', 'basketball', 'golfswing', 'typing', 'skiing', 'javelinthrow', 'parallelbars', 'breaststroke', 'trampolinejumping', 'fieldhockeypenalty', 'shavingbeard', 'sumowrestling', 'bowling', 'balancebeam', 'frontcrawl', 'rafting', 'boxingspeedbag', 'cleanandjerk', 'brushingteeth', 'punch', 'soccerjuggling', 'tennisswing', 'nunchucks', 'lunges', 'pullups', 'cliffdiving', 'floorgymnastics', 'basketballdunk', 'playingsitar', 'handstandwalking', 'yoyo', 'handstandpushups', 'soccerpenalty', 'fencing', 'icedancing', 'benchpress', 'skijet', 'rowing', 'longjump', 'mixing', 'applylipstick', 'moppingfloor', 'headmassage', 'horserace', 'drumming', 'walkingwithdog', 'horseriding', 'kayaking', 'shotput', 'hammerthrow', 'jumprope', 'playingguitar', 'hulahoop', 'bandmarching', 'pizzatossing', 'boxingboxingspeedbag', 'frisbeecatch', 'jumpingjack', 'haircut', 'playingcello']

proba_2

array([ 0.25      ,  0.67857143,  0.76923077,  1.        ,  0.44444444,
        0.37837838,  0.7       ,  0.88571429,  0.71875   ,  0.03125   ,
        0.26829268,  0.76470588,  0.80952381,  0.33333333,  1.        ,
        0.89285714,  0.48387097,  0.3877551 ,  0.03030303,  0.92857143,
        0.41025641,  0.48571429,  0.7       ,  0.72972973,  0.75609756,
        0.82142857,  0.8974359 ,  0.94736842,  0.71794872,  0.425     ,
        0.82352941,  0.79411765,  1.        ,  0.52083333,  0.94871795,
        0.88636364,  0.77777778,  0.89285714,  0.60606061,  0.79411765,
        0.68292683,  0.89189189,  0.33333333,  0.81818182,  0.36956522,
        0.94594595,  0.6969697 ,  0.6969697 ,  0.64285714,  0.8       ,
        0.67567568,  0.30232558,  0.96969697,  0.97674419,  0.89655172,
        0.5       ,  0.78787879,  0.625     ,  0.87878788,  0.82352941,
        0.83333333,  0.07692308,  0.83333333,  0.075     ,  0.46153846,
        0.85714286,  0.97560976,  0.31428571,  1.        ,  0.48837209,
        0.07142857,  0.31428571,  0.40540541,  0.85      ,  0.60606061,
        1.        ,  1.        ,  0.39285714,  0.72727273,  0.80952381,
        0.93181818,  0.24137931,  0.75      ,  0.65      ,  0.87878788,
        0.76744186,  0.62857143,  0.86363636,  0.83333333,  0.42857143,
        0.65217391,  0.28205128,  1.        ,  0.69444444,  0.73333333,
        0.13793103,  0.04878049,  0.76470588,  0.11111111,  0.48648649,
        0.97777778])

The classes that l would like to plot in different colors are :

['bowling', 'babycrawling', 'shavingbeard', 'breaststroke','lunges', 'handstandpushups', 'yoyo', 'drumming']

Thank you for your help

2
  • Do you need proba_1 to stay in order, or do you just want proba_1[i] to match with classes_1[i]? Commented Feb 26, 2018 at 14:04
  • @Stephen, l would like proba_1[i] to match with classes_1[i] and proba_2[i] to match with classes_2[i] Commented Feb 26, 2018 at 14:05

2 Answers 2

2

I can't recall having to sort a list following the order of another list, but this is what I would do in your case:

data2 = list(zip(classes_2, proba_2))
data2.sort(key=lambda x: classes_1.index(x[0]))
classes_2, proba_2 = zip(*data2)

Now both classes_1 and classes_2 follow the same order.

Edit: Be advised that this method is quadratic in complexity. So for larger arrays or stricter time constraints, you can probably speed things up by indexing classes_1 in a dict for fast index lookup

Sign up to request clarification or add additional context in comments.

7 Comments

AttributeError: 'numpy.ndarray' object has no attribute 'index' at data2.sort(key=lambda x: classes_1.index(x[0]))
You specifically said that classes_1 and classes_2 were lists, not ndarrays
Either way... Cast classes_1 to a list before doing this and you should be fine
sorry l transformed back my array to a list. l tested your solution but when l make classes_2 == classes_1 it returns false
It doesn't mean it's not sorted. Try assert all(c1 == c2 for c1, c2 in zip(classes_1, classes_2))
|
0

Since you want proba_1[i] to match with classes_1[i], one such way to make sure they stay together is to zip them. Thus you can do something like this:

classes_1, proba_1 = zip(*sorted(zip(classes_1, proba_1)))
classes_2 proba_2 = zip(*sorted(zip(classes_2, proba_2)))

You will end up with with tuples, but this should work fine for what you want.


Explain:

The inner zip makes sure the matching elements will stay together. We sort that list of tuples. The * unpacks them into the zip function to separate the tuples back into separate lists (though they end up as a long tuple, which you can easily cast as list if needed)

Because it will perform the sort based on the elements of classes_1, and classes_1 and classes_2 have the same elements, you will end up with the lists sorted the same way

3 Comments

classes_1 == classes_2 returns false
So the lists are not sorted
So sorry. I mixed up which were supposed to be equal. Need to swap p1, c1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.