简体   繁体   中英

Matplotlib x-labels vertical cut

I would like to generate a plot containing various labels arranged vertically by using this method:

def feat_imp(df, model, title, n_features, file_exp, format_exp):

d = dict(zip(df.columns, model.feature_importances_))
ss = sorted(d, key=d.get, reverse=True)
top_names = ss[0:n_features]

plt.figure()
plt.tight_layout()
plt.title(title)
plt.bar(range(n_features), [d[i] for i in top_names], color="g", align="center")
plt.xlim(-1, n_features)
plt.xticks(range(n_features), top_names, rotation='vertical')
plt.savefig(file_exp, format=format_exp, dpi=1200)

However, when doing so, the labels are not taken into account when matplotlib creates the EPS file (I need EPS):

在此处输入图像描述

This is how I call the def:

feat_imp(df_K562, model, "Feature importances (" + cell_name + " - " + model_name + " - TOP 30)", 30, root_path + dest_path + "TOP30.eps", "eps")

I have tried to activate ”autoSize” in the matplotlib config file, I have tried to put a specific dpi and also to set plt.tight_layout(). Nothing seems to work. Any ideas?

I have edited the def like this, and it works:

def feat_imp(df, model, title, n_features, file_exp, format_exp):
d = dict(zip(df.columns, model.feature_importances_))
ss = sorted(d, key=d.get, reverse=True)
top_names = ss[0:n_features]
plt.figure()
plt.title(title)
plt.bar(range(n_features), [d[i] for i in top_names], color="g", align="center")
plt.xlim(-1, n_features)
plt.tick_params(axis='both', which='minor', labelsize=6)
plt.xticks(range(n_features), top_names, rotation='vertical')
plt.autoscale()
plt.savefig(file_exp, format=format_exp, bbox_inches = "tight")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM