【问题标题】:Make the long labels on axis visible [duplicate]使轴上的长标签可见[重复]
【发布时间】:2019-10-06 05:34:45
【问题描述】:

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

我用来制作这个情节的代码是:-

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')

这是我想要的:-

任何帮助将不胜感激。我一直在为此苦苦挣扎。

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

    • 我试过这个:- "plt.xticks(heights, bar, rotation='vertical')" 但这会抹去刻度。我想扩大空间而不是旋转它。
    • 如果我正确理解您的代码,您的 x 值应该是条形。所以试试plt.xticks(bars, bars, rotation='vertical')
    猜你喜欢
    • 2015-01-31
    • 2014-04-30
    • 2019-04-13
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多