绘制水平条形图与"良好对齐"标签

时间:2015-05-07 09:33:28

标签: python matplotlib

我正在尝试绘制以下代码。但在我的Y轴上,我希望标签为feature_1feature_2等。但是,当我绘制标签时,标签不清楚。它很拥挤。任何人都可以帮助我。

"""
Simple demo of a horizontal bar chart.
"""
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt


# Example data

important_features = [(0.0457, 'feat_34'), (0.042, 'feat_11'), (0.0359, 'feat_25'), (0.0355, 'feat_60'), (0.0316, 'feat_40'), (0.0298, 'feat_14'), (0.0272, 'feat_80'), (0.0252, 'feat_42'), (0.0241, 'feat_86'), (0.0236, 'feat_67'), (0.0232, 'feat_15'), (0.0217, 'feat_62'), (0.02, 'feat_24'), (0.0196, 'feat_26'), (0.0191, 'feat_54'), (0.0189, 'feat_36'), (0.0183, 'feat_48'), (0.0177, 'feat_75'), (0.016, 'feat_88'), (0.0155, 'feat_64'), (0.0147, 'feat_90'), (0.014, 'feat_72'), (0.014, 'feat_69'), (0.0131, 'feat_9'), (0.0131, 'feat_8'), (0.0131, 'feat_57'), (0.0129, 'feat_43'), (0.0126, 'feat_32'), (0.0126, 'feat_16'), (0.012, 'feat_76'), (0.0117, 'feat_33'), (0.0109, 'feat_68'), (0.0109, 'feat_4'), (0.0107, 'feat_39'), (0.0106, 'feat_70'), (0.0104, 'feat_78'), (0.0103, 'feat_59'), (0.0101, 'feat_53'), (0.0096, 'feat_85'), (0.0087, 'feat_38'), (0.0086, 'feat_47'), (0.0083, 'feat_50'), (0.0083, 'feat_46'), (0.0079, 'feat_92'), (0.0078, 'feat_71'), (0.0078, 'feat_30'), (0.0076, 'feat_17'), (0.0073, 'feat_66'), (0.007, 'feat_41'), (0.0069, 'feat_13'), (0.0068, 'feat_83'), (0.0068, 'feat_73'), (0.0068, 'feat_56'), (0.0066, 'feat_20'), (0.0064, 'feat_1'), (0.0062, 'feat_37'), (0.0061, 'feat_27'), (0.006, 'feat_87'), (0.006, 'feat_2'), (0.0058, 'feat_58'), (0.0058, 'feat_35'), (0.0058, 'feat_22'), (0.0056, 'feat_18'), (0.0055, 'feat_55'), (0.0054, 'feat_74'), (0.0053, 'feat_91'), (0.0053, 'feat_79'), (0.005, 'feat_29'), (0.0049, 'feat_44'), (0.0046, 'feat_89'), (0.0045, 'feat_45'), (0.0041, 'feat_10'), (0.0039, 'feat_19'), (0.0037, 'feat_21'), (0.0035, 'feat_3'), (0.0033, 'feat_65'), (0.0031, 'feat_7'), (0.003, 'feat_23'), (0.0029, 'feat_63'), (0.0029, 'feat_52'), (0.0028, 'feat_77'), (0.0027, 'feat_49'), (0.0025, 'feat_12'), (0.0022, 'feat_93'), (0.0021, 'feat_28'), (0.002, 'feat_61'), (0.002, 'feat_5'), (0.0019, 'feat_81'), (0.0017, 'feat_31'), (0.0011, 'feat_84'), (0.0011, 'feat_51'), (0.0009, 'feat_82'), (0.0009, 'feat_6')]

x_value = []
y_feat = []
for (a, b) in important_features:
    y_feat.append(b)
    x_value.append(a)

features = y_feat
y_pos = range(1,2*len(features),2)

performance = x_value


plt.barh(y_pos, performance, align='center', alpha=0.4)
plt.yticks(y_pos, features)
plt.ylim(0,200)
plt.xlabel('Values')
plt.title('Feature Plot')

plt.show()

1 个答案:

答案 0 :(得分:0)

这是我要做的。有太多的条形,所以文本是相互叠加的。我的解决方案是将其中一半放在栏的左侧,另一半放在栏右侧(你可以将它改为你希望它们的任何地方但是想法是将其中一半放在某处而另一半放在某处其他)。此外,使数字更大有助于。

"""
Simple demo of a horizontal bar chart.
"""
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt


# Example data

important_features = [(0.0457, 'feat_34'), (0.042, 'feat_11'), (0.0359, 'feat_25'), (0.0355, 'feat_60'), (0.0316, 'feat_40'), (0.0298, 'feat_14'), (0.0272, 'feat_80'), (0.0252, 'feat_42'), (0.0241, 'feat_86'), (0.0236, 'feat_67'), (0.0232, 'feat_15'), (0.0217, 'feat_62'), (0.02, 'feat_24'), (0.0196, 'feat_26'), (0.0191, 'feat_54'), (0.0189, 'feat_36'), (0.0183, 'feat_48'), (0.0177, 'feat_75'), (0.016, 'feat_88'), (0.0155, 'feat_64'), (0.0147, 'feat_90'), (0.014, 'feat_72'), (0.014, 'feat_69'), (0.0131, 'feat_9'), (0.0131, 'feat_8'), (0.0131, 'feat_57'), (0.0129, 'feat_43'), (0.0126, 'feat_32'), (0.0126, 'feat_16'), (0.012, 'feat_76'), (0.0117, 'feat_33'), (0.0109, 'feat_68'), (0.0109, 'feat_4'), (0.0107, 'feat_39'), (0.0106, 'feat_70'), (0.0104, 'feat_78'), (0.0103, 'feat_59'), (0.0101, 'feat_53'), (0.0096, 'feat_85'), (0.0087, 'feat_38'), (0.0086, 'feat_47'), (0.0083, 'feat_50'), (0.0083, 'feat_46'), (0.0079, 'feat_92'), (0.0078, 'feat_71'), (0.0078, 'feat_30'), (0.0076, 'feat_17'), (0.0073, 'feat_66'), (0.007, 'feat_41'), (0.0069, 'feat_13'), (0.0068, 'feat_83'), (0.0068, 'feat_73'), (0.0068, 'feat_56'), (0.0066, 'feat_20'), (0.0064, 'feat_1'), (0.0062, 'feat_37'), (0.0061, 'feat_27'), (0.006, 'feat_87'), (0.006, 'feat_2'), (0.0058, 'feat_58'), (0.0058, 'feat_35'), (0.0058, 'feat_22'), (0.0056, 'feat_18'), (0.0055, 'feat_55'), (0.0054, 'feat_74'), (0.0053, 'feat_91'), (0.0053, 'feat_79'), (0.005, 'feat_29'), (0.0049, 'feat_44'), (0.0046, 'feat_89'), (0.0045, 'feat_45'), (0.0041, 'feat_10'), (0.0039, 'feat_19'), (0.0037, 'feat_21'), (0.0035, 'feat_3'), (0.0033, 'feat_65'), (0.0031, 'feat_7'), (0.003, 'feat_23'), (0.0029, 'feat_63'), (0.0029, 'feat_52'), (0.0028, 'feat_77'), (0.0027, 'feat_49'), (0.0025, 'feat_12'), (0.0022, 'feat_93'), (0.0021, 'feat_28'), (0.002, 'feat_61'), (0.002, 'feat_5'), (0.0019, 'feat_81'), (0.0017, 'feat_31'), (0.0011, 'feat_84'), (0.0011, 'feat_51'), (0.0009, 'feat_82'), (0.0009, 'feat_6')]

x_value = []
y_feat = []
for (a, b) in important_features:
    y_feat.append(b)
    x_value.append(a)

features = y_feat
y_pos = range(1,2*len(features),2)

performance = x_value

fig = plt.figure(figsize=(16, 12))
plt.barh(y_pos, performance, align='center', alpha=0.4)
# no ticks
plt.yticks([])
j = 0
for x, y, label in zip(performance, y_pos, features):
    if j % 2 == 0: # half of them on the left of the axes
        plt.text(-0.0003, y, label, horizontalalignment='right', verticalalignment='center')
    else:          # half of them on the right of the bars
        plt.text(x+0.0003, y, label, horizontalalignment='left', verticalalignment='center')
    j +=1
plt.ylim(0,200)
plt.xlabel('Values')
plt.title('Feature Plot')

plt.show()