Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用“绘制水平条形图”;“漂亮的路线”;标签_Python_Matplotlib - Fatal编程技术网

Python 用“绘制水平条形图”;“漂亮的路线”;标签

Python 用“绘制水平条形图”;“漂亮的路线”;标签,python,matplotlib,Python,Matplotlib,我正在尝试绘制以下代码。但在Y轴中,我希望标签为特征_1,特征_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 = [

我正在尝试绘制以下代码。但在Y轴中,我希望标签为
特征_1
特征_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()

显而易见的解决方案是:使图形更大,字体更小,绘图更少bars@JulienSpronck-你能帮我一下吗,怎么做。?图,ax1=plt。子图(图大小=(10,20))相应地改变10和20。它代表x轴和y轴。