【问题标题】:How to extend the space for y ticks in barh - python matplotlib如何在 barh 中扩展 y 刻度的空间 - python matplotlib
【发布时间】:2015-10-06 12:57:24
【问题描述】:

请看附图

我在python中有如下源代码

def plotBarChartH(self,data):
           LogManager.logDebug('Executing Plotter.plotBarChartH')

           if type(data) is not dict:
               LogManager.logError('Input data parameter is not in right format. Need a dict')
               return False

           testNames = []
           testTimes = []

           for val in data:
                testNames.append(val)
                testTimes.append(data[val])

           matplotlib.rcParams.update({'font.size': 8})     
           yPos = np.arange(len(testNames))
           plt.barh(yPos, testTimes, height=0.4, align='center', alpha=0.4)
           plt.yticks(yPos, testNames)
           plt.xlabel('time (seconds)')
           plt.title('Test Execution Times')
           savePath = os.path.join(ConfigurationManager.applicationConfig['robotreportspath'],'bar.jpg')
           plt.savefig(savePath)
           plt.clf()
           return True

栏看起来不错,但我有两个问题

  1. y轴上的文字如何才能完整显示?我的意思是有些文本被截断了,我想扩展占用的空间,以便它可以完整显示。

  2. 我可以增加绘制图表的整个绘图区域吗?我想增加绘图区域的宽度,使图像看起来更大

谢谢

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以在使用plt.figure(figsize=(width,height)), and callplt.tight_layout()` 创建Figure 对象时明确设置图形大小(以英寸为单位),以便为刻度标签腾出空间,如下所示:

    import matplotlib.pyplot as plt
    
    names = ['Content Channels','Kittens for Xbox Platform','Tigers for PS Platform',
             'Content Series', 'Wombats for Mobile Platform']
    
    values = [260, 255, 420, 300, 270]
    
    fig = plt.figure(figsize=(10,4))
    ax = fig.add_subplot(111)
    yvals = range(len(names))
    ax.barh(yvals, values, align='center', alpha=0.4)
    plt.yticks(yvals,names)
    plt.tight_layout()
    
    plt.show()
    

    【讨论】:

    • 你用的那个可爱的调色板是什么?
    • @mel 我认为它只是(旧的)默认 Matplotlib 蓝色,带有一些 alpha。
    【解决方案2】:
    1. y轴上的文字如何才能完整显示?我的意思是有些文本被截断了,我想扩展占用的空间,以便它可以完整显示。

    您可以使用plt.axes 来控制坐标轴的绘制位置,这样您就可以在左侧区域留出更多空间。一个例子可以是plt.axes([0.2,0.1,0.9,0.9])

    1. 我可以增加绘制图表的整个绘图区域吗?我想增加绘图区域的宽度,使图像看起来更大

    我不明白你的意思。

    • 您可以使用plt.figure(例如plt.figure(figsize = (6,12)))控制图形的大小
    • 您可以使用plt.[xy]lim 控制信息和轴之间的空间。例如,如果您想在右侧区域留出更多空白,您可以使用plt.xlim(200, 600)
    • 您可以使用plt.axes 节省一些边距空间(参见上面的问题1)。

    【讨论】:

    • 你好,数组代表什么[0.2,0.1,0.9,0.9]?我正在玩弄这些价值观,但无法理解它们实际代表什么。
    • 表示xmin、ymin、xmax和ymax的归一化值。因此,如果您使用 0,0,1,1 则没有边距,如果您使用 0.1,0.1,0.9,0.9 您在轴和图形边框之间的每一侧留下 10%,...
    【解决方案3】:
    1. 一种选择是在您的字符串中包含换行符\n(或使用"\n".join(wrap(longstring,60) 之类的东西,如this 答案)。 您可以使用fig.subplots_adjust(left=0.3) 调整绘图区域以确保显示整个字符串,

    例子:

    import matplotlib.pyplot as plt
    import numpy as np
    
    val = 1000*np.random.rand(5)    # the bar lengths
    pos = np.arange(5)+.5    # the bar centers on the y axis
    name = ['name','really long name here', 
            'name 2', 
            'test', 
            'another really long \n name here too']
    
    fig, ax = plt.subplots(1,1)
    ax.barh(pos, val, align='center')
    plt.yticks(pos, name)
    fig.subplots_adjust(left=0.3)
    plt.show()
    

    给了

    1. 您可以使用figsize 参数将物理图形大小调整为子图或图形。

    例子:

    fig, ax = plt.subplots(1,1, figsize=(12,8))
    

    可以根据数据设置轴来调整图中的空间大小,

    ax.set_xlim((0,800))
    

    或使用ax.set_xlim((0,data.max()+200)) 自动化。

    【讨论】:

      【解决方案4】:

      截至 2020 年:只需在 plt 上调用 .autoscale 即可:

      你可以在plt.show()之前这样做

      plt.autoscale()
      

      plt.autoscale(enable=True, axis='y', tight=None)
      

      【讨论】:

        猜你喜欢
        • 2018-09-08
        • 2012-05-08
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多