如何旋转Matplotlib条形图?

6
以下代码用于生成柱状图。我想将其旋转,使其变为垂直的,例如:当前x轴上的标签移到y轴上,当前y轴标签移到x轴上,并相应地旋转柱子。
我对matplotlib和Python都很陌生,所以需要您的帮助。
def plot_coefficients(classifier, feature_names, top_features=40):

    if classifier.__class__.__name__ == 'SVC':
        coef = classifier.coef_
        coef2 = coef.toarray().ravel()
        coef1 = coef2[:len(feature_names)]  
    else:
        coef2 = classifier.coef_.ravel()
        coef1 = coef2[:len(feature_names)]

    top_positive_coefficients = np.argsort(coef1)[-top_features:]
    top_negative_coefficients = np.argsort(coef1)[:top_features]
    top_coefficients = np.hstack([top_negative_coefficients, top_positive_coefficients])
     # create plot
    plt.figure(figsize=(15, 5))
    colors = ['red' if c < 0 else 'blue' for c in coef1[top_coefficients]]
    plt.bar(np.arange(2 * top_features), coef1[top_coefficients], color=colors)
    feature_names = np.array(feature_names)
    plt.xticks(np.arange(1, 1 + 2 * top_features), feature_names[top_coefficients], rotation=90, ha='right')
    plt.show()

此处为图片描述

更新 期望输出: 此处为图片描述

1个回答

11

如果您真的想旋转垂直条形图怎么办?比如当您立即收到一个垂直条形图作为预编程Python包函数的输出,并希望对图形进行后处理以符合自己的风格(例如将其转换为水平条形图而不是垂直条形图)时,该怎么办? - Matthi9000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接