首页 文章

条形图与python / matplotlib中的垂直标签

提问于
浏览
45

我正在使用matplotlib生成(垂直)条形图 . 问题是我的标签很长 . 有没有办法在栏中或上方或下方垂直显示它们?

4 回答

  • 2

    你的意思是这样的:

    >>> from matplotlib import *
    >>> plot(xrange(10))
    >>> yticks(xrange(10), rotation='vertical')
    

    通常,要在matplotlib中显示垂直方向的任何文本,可以添加关键字 rotation='vertical' .

    有关更多选项,您可以查看帮助(matplotlib.pyplot.text)

    yticks函数绘制y轴上的刻度;我不确定你最初是指这个还是ylabel函数,但程序是相同的,你必须添加rotation ='vertical'

    也许您还可以找到有用的选项'verticalalignment'和'horizontalalignment',它允许您定义如何将文本与刻度线或其他元素对齐 .

  • 5

    在Jupyter Notebook中你可能会使用这样的东西

    %matplotlib inline
    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.xticks(rotation='vertical')
    plt.plot(np.random.randn(100).cumsum())
    

    或者您可以使用:

    plt.xticks(rotation=90)
    
  • 9

    我建议看the matplotlib gallery . 至少有两个例子似乎是相关的:

  • 68

    请看看这个链接:https://python-graph-gallery.com/7-custom-barplot-layout/

    import matplotlib.pyplot as plt
    
    heights = [10, 20, 15]
    bars = ['A_long', 'B_long', 'C_long']
    y_pos = range(len(bars))
    plt.bar(y_pos, heights)
    # Rotation of the bars names
    plt.xticks(y_pos, bars, rotation=90)
    

    结果就像这样
    enter image description here

    希望它有所帮助 .

相关问题