使轴上的长标签可见

时间:2019-05-19 18:42:35

标签: python-3.x matplotlib axis-labels

我的标签很长,想制作条形图。我已经使用了堆栈溢出时给出的几乎所有解决方案,但似乎没有一个起作用。标签相互叠放,不可见。这是当前正在制作的图表:-

enter image description here

,我用来生成该图的代码是:-

with open('features', 'rb') as fp:
    X=pickle.load(fp)

with open('labels', 'rb') as fp:
    y=pickle.load(fp)

from sklearn.feature_selection import mutual_info_classif

imp=mutual_info_classif(X,y)
#getting mutual information

imp=np.array(imp)*100


df_item=pd.read_excel('item_label.xlsx')

items=list(df_item['Item_id'].astype(str).values)


ids=["220045","220046","220047","220050","220051","220059","220060","220210","220603","224167",
    "224643","225108","225122","225664","227007","227242","227243","227688","227916","227918","227919"
    ,"227923","227925","227926"]

df_item=df_item[df_item['Item_id'].isin(ids)]
vals=list(df_item['Label'].values)
#plotting mutual info with features names

people=["GENDER","ADMISSION_TYPE","ADMISSION_LOCATION","RELIGION","MARITAL_STATUS","ETHNICITY",
    "DIAGNOSIS"]

arr=[]
for p in people:
    arr.append(p)
for v in vals:
    arr.append(v)
people=arr

import matplotlib.pyplot as plt

heights = imp
bars = people

f, ax = plt.subplots(figsize=(18,5))
plt.bar(bars, heights)
plt.savefig('mutual_info.png')

这是我想要的东西:-

enter image description here

任何帮助将不胜感激。为此,我一直很努力。

1 个答案:

答案 0 :(得分:1)

rotationxticks()axes.set_xticklabels()一起使用。

plt.xticks(x, labels, rotation='vertical')  # you can set rotation to an angle as well (eg: 90)

请参考以下示例:

  

https://matplotlib.org/gallery/ticks_and_spines/ticklabels_rotation.html

因此,此代码应修改为:

f, ax = plt.subplots(figsize=(18,5))
ax.bar(bars, heights)
ax.set_xticklabels(bars, rotation=90)