在制作情节

时间:2018-02-26 13:51:18

标签: python arrays sorting matplotlib

我想制作直方图(条形图)。 我有一对阵列:

1)classes_1,proba_1 2)classes_2,proba_2  每个数组有101个元素。 classes_1 / classes_2是字符串(x坐标)和proba_1,proba_2是浮点数。

我的目的:

(classes_1, proba_1)(classes_2, proba_2)

的一个情节栏直方图中制作

问题: classes_2和classes_1都包含101个元素。它们具有相同的元素,但顺序不同,以便:

classes_1 == classes_2
False

我的问题:

1)如何将classes_1和classes_2归为同一个顺序? classes_1和classes_2中的新订单应更新proba_1和proba_2中的订单。

classes_1和classes_2是列表<class 'list'> proba_1和proba_2是<class 'numpy.ndarray'>

2)鉴于有序的课程列表,我想用不同的颜色制作选定的课程(让我们说是黑色)

我尝试过:

    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')

以下是我的数据

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])

我想用不同颜色绘制的类是:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我不记得必须按照另一个列表的顺序对列表进行排序,但这就是我在你的情况下会做的事情:

appstore

现在,data2 = list(zip(classes_2, proba_2)) data2.sort(key=lambda x: classes_1.index(x[0])) classes_2, proba_2 = zip(*data2) classes_1都遵循相同的顺序。

编辑:请注意,此方法的复杂性是二次方的。因此,对于更大的数组或更严格的时间约束,您可以通过在dict中索引classes_2来快速索引查找来加快速度。