我正在尝试显示带有长标签的数据框。 当我希望它显示标签时,情节主要由图表占据。 我有:
new_labels = []
for i, index in enumerate(df.index):
new_label = "%s (%.2f)"%(index,df.performance[i])
new_labels.append(new_label)
fig , axes = plt.subplots(1,1)
df.sort_values(col_name).plot(kind='barh', ax=axes)
axes.xaxis.set_ticklabels(new_labels)
axes.xaxis.set_major_locator(ticker.MultipleLocator(1))
如您所见,标签不会显示。 确实有价值观:
new_labels
['PLS regression\n\n PLSRe (0.12)',
'Regression based on k-nea (0.44)',
'The Gaussian Process mode (0.46)',
'Orthogonal Matching Pursu (0.52)',
'An extremely randomized t (0.54)',
'RANSAC (RANdom SAmple Con (0.56)',
'Elastic Net model with it (0.66)',
'Kernel ridge regression. (0.67)',
'Cross-validated Orthogona (0.67)',
'Linear Model trained with (0.68)',
'Linear regression with co (0.68)',
'Theil-Sen Estimator (0.68)',
'Lasso linear model with i (0.69)',
'Bayesian ridge regression (0.70)'...
如何为标签提供更多空间,并使用更短的条形图?
答案 0 :(得分:0)
试试这个:
df.sort_values(col_name).plot(x=new_labels, kind='barh', ax=axes)
# NOTE: ^^^^^^^^^^^^
答案 1 :(得分:0)
您可以在绘图的左侧添加更多空间,以便标签有机会完全显示。这可以使用
完成fig.subplots_adjust(left=0.5)
其中0.5
表示数字宽度的50%。
完整示例:
import pandas as pd
import matplotlib.pyplot as plt
labels = ['PLS regression PLSRe (0.12)',
'Regression based on k-nea (0.44)',
'The Gaussian Process mode (0.46)',
'Orthogonal Matching Pursu (0.52)',
'An extremely randomized t (0.54)',
'RANSAC (RANdom SAmple Con (0.56)',
'Elastic Net model with it (0.66)',
'Kernel ridge regression. (0.67)',
'Cross-validated Orthogona (0.67)',
'Linear Model trained with (0.68)',
'Linear regression with co (0.68)',
'Theil-Sen Estimator (0.68)',
'Lasso linear model with i (0.69)',
'Bayesian ridge regression (0.70)']
values = [0.12, .44,.46,.52,.54,.56,.66,.67,.67,.68,.68,.68,.69,.7]
df = pd.DataFrame(values, index=labels)
fig , axes = plt.subplots(1,1)
fig.subplots_adjust(left=0.5)
df.plot(kind='barh', ax=axes)
plt.show()