我想绘制共享相同x轴(即类)的三个向量的直方图。类(101个字符串值)是一组标签
classes={'playingguitar', 'billiards', 'boxingboxingspeedbag', 'applylipstick', 'playingsitar', 'fieldhockeypenalty', 'blowingcandles', 'longjump', 'playingdhol', 'biking', 'playingpiano', 'handstandwalking', 'playingcello', 'ropeclimbing', 'hulahoop', 'cricketshot', 'punch', 'pushups', 'floorgymnastics', 'jumpingjack', 'lunges', 'golfswing', 'bandmarching', 'skiing', 'playingtabla', 'archery', 'breaststroke', 'unevenbars', 'playingviolin', 'babycrawling', 'moppingfloor', 'bowling', 'knitting', 'rockclimbingindoor', 'shavingbeard', 'writingonboard', 'shotput', 'stillrings', 'drumming', 'applyeyemakeup', 'cuttinginkitchen', 'pizzatossing', 'soccerpenalty', 'bodyweightsquats', 'taichi', 'benchpress', 'trampolinejumping', 'playingdaf', 'pullups', 'pommelhorse', 'jumprope', 'headmassage', 'horserace', 'skijet', 'surfing', 'basketballdunk', 'polevault', 'brushingteeth', 'salsaspin', 'frontcrawl', 'horseriding', 'typing', 'throwdiscus', 'nunchucks', 'diving', 'balancebeam', 'highjump', 'volleyballspiking', 'icedancing', 'cricketbowling', 'rafting', 'yoyo', 'walkingwithdog', 'swing', 'hammering', 'mixing', 'wallpushups', 'parallelbars', 'skateboarding', 'skydiving', 'jugglingballs', 'soccerjuggling', 'kayaking', 'cleanandjerk', 'tennisswing', 'playingflute', 'javelinthrow', 'haircut', 'blowdryhair', 'cliffdiving', 'frisbeecatch', 'boxingspeedbag', 'handstandpushups', 'militaryparade', 'hammerthrow', 'rowing', 'basketball', 'baseballpitch', 'tabletennisshot', 'fencing', 'sumowrestling'}
len(classes)=101
在labels2
,labels2_test
,labels2_full_train
中,我们按不同顺序列出每个班级的出现次数:
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
import pylab
labels2, values2 = zip(*Counter(train2).items())
labels2_test, values_test2 = zip(*Counter(test).items())
labels2_full_train, values2_full_train = zip(*Counter(full_train).items())
我想制作一个图,使得x轴代表values2, values_test2, values2_full_train
我尝试了什么?
pylab.rcParams['figure.figsize'] = (30, 10)
fig1, ax1 = plt.subplots()
ax1.tick_params(rotation=90)
ax1.plot(labels2, values2, label='train classes')
ax1.plot(labels2_test, values_test2, label='test classes')
ax1.plot(labels2_full_train, values2_full_train, label='test classes')
ax1.set_xlabel("classes",rotation='vertical')
ax1.set_ylabel("number of examples")
ax1.set_title("data distibution")
ax1.legend(loc='best')
fig1.show()
由于labels2
,labels2_test
,labels2_full_train
在
labels2, values2 = zip(*Counter(train2).items())
labels2_test, values_test2 = zip(*Counter(test).items())
labels2_full_train, values2_full_train = zip(*Counter(full_train).items())
那么如何以相同的顺序(例如类中定义的)获得labels2
,labels2_test
,labels2_full_train
?
例如
labels2=['rafting', 'punch', 'applyeyemakeup',...]
values2=[78, 112, 106,...]
labels2_test=['typing', 'surfing', 'cricketbowling',..]
values_test2=[46, 38, 39,...]
labels2_full_train=['archery', 'benchpress', 'brushingteeth',...]
values2_full_train=[1046, 1043, 1065,...]
谢谢
答案 0 :(得分:2)
因为matplotlib显示在轴上排序的分类变量,所以您需要在绘图之前按字母顺序对列表进行排序。因此,让我们创建一个完整且可验证的示例:
from collections import Counter
list1 = list("ABADEAABEDDAEDBBBBBD") # letters A, B, D, E
list2 = list("AABAEEDCCFFFEDABEEC") # all letters A-F
items1, counts1 = zip(*sorted(Counter(list1).items()))
items2, counts2 = zip(*sorted(Counter(list2).items()))
import matplotlib.pyplot as plt
plt.plot(items1, counts1, label="list1")
plt.plot(items2, counts2, label="list2")
plt.legend()
plt.show()
请注意,第一个列表包含所有可能项的子集。输出如下:
不幸的是,虽然列表本身已经排序,但是图表显示了一些奇怪的行为,因为轴最后显示C
和F
。
解决这个问题的方法是让轴知道所有可能的项目,以便在之前绘制。我们可以例如绘制所有项目到轴的隐形图,
import matplotlib.pyplot as plt
plt.plot(items1+items2, [5]*len(items1+items2), visible=False)
plt.plot(items1, counts1, "o-", label="list1")
plt.plot(items2, counts2, "o-", label="list2")
plt.legend()
plt.show()