簡體   English   中英

用標簽的“精確對齊”繪制水平條形圖

[英]Plot horizontal bar chart with “nice alignment” of label

我正在嘗試繪制以下代碼。 但是在我的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()

這就是我要做的。 條形太多了,因此文本彼此重疊。 我的解決方案是將它們的一半放在條形圖的左側,另一半放在條形圖的右側(您可以將其更改為您想要的位置,但想法是將它們的一半放在某個位置,另一半放在某個位置其他)。 另外,增大身材也有幫助。

"""
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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM