使用matplotlib在loglog图中设置轴限制

时间:2012-07-06 23:12:08

标签: python matplotlib

如何在使用matplotlib绘制的点周围创建空间?

例如,在此图中,左下角点被轴截断,但我想在点和轴之间留出更多空间。 example plot

import matplotlib.pyplot as plt
x = [2**i for i in xrange(4,14)]
y = [i**2 for i in x]
plt.loglog(x,y,'ro',basex=2,basey=2)
plt.xlim([0, 2**14]) # <--- this line does nothing
plt.show()

在交互模式下,xlim行返回(16.0, 16384),旧值而不是我想要设置的新值。

3 个答案:

答案 0 :(得分:10)

无法在loglog图上绘制零(log(0)= -inf)。它默默地失败,因为它不能使用0作为限制。

请尝试plt.xlim([1,2**14])

答案 1 :(得分:1)

如果您正在寻找一种处理此问题的一般方法,并希望自动调整绘图的限制(即使您不知道任何数据),您也可以编写一个受this answer启发的代码段。类似的问题。

请注意,您必须稍微调整一下代码并更改它,以便它也能为y轴完成工作。

答案 2 :(得分:1)

在这里,我使用plt.axis()设置xmin和xmax值(类似于您的plt.xlim调用);但我使用了基于范围和间隔的变量“缓冲区”。轴的范围是使用最小值和最大值得出的。由于对数小数位数不会显示0或负数,因此我在xmin函数调用中将.axis()参数设置为等于1。

interval = 10

plot_range_buffer = (data.column.max() - data.column.min() / interval

plt.axis(
    xmin=1, # to keep scale if minimum is 0 or close to 0
    #xmin = data.column.min()-plot_range_buffer # subtracts interval buffer from min value
    xmax=data.column.max()+plot_range_buffer # adds the interval buffer to max value
)

我们可以根据需要对y轴执行相同的操作。要控制绘图的一个方面,需要很多代码,但是如果matplotlib.pyplot过于繁琐,我喜欢在用户函数中使用它。
这是两个用于反复试验的模板用户例程。我测试了第一个,它运行良好;我只是构建了第二个作为替代选项,但没有对其进行测试...如果它提供了错误,请告诉我。

用户功能1:功能内的总体控制权

def plotcolumn(some_row_entry):
    """Selects data for some row entry
       Creates a scatter plot from two column variables
       Allows for user control over buffers through manipulation
           of interval that is relative to axis max,min range"""

    # numpy fancy selector for input argument
    data = data[data.some_row_entry == some_row_entry]

    # establish plot
    data.plot.scatter(
        'first_column',
        'second_column',
        logx=True,                                   # turn log xaxis on/off
        #logy=True                                    # turn log yaxis on/off
    )

    # axis range controls
    x_interval = 10
    y_interval = 10

    # x axis (ie x-axis variable)
    x_buffer = (data.first_column.max() - data.first_column.min()) / x_interval

    # y axis (ie y-axis variable)
    y_buffer = (data.second_column.max() - data.second_column.min()) / y_interval

    plt.axis(
        xmin=1,                                      # use for xaxis lower buffer if logx and close to 0
        xmax=data.first_column.max()+x_buffer,       # sets xaxis upper buffer
        #xmin=data.first_column.min()-x_buffer,      # sets xaxis lower buffer if not logx close to 0

        #ymin= 1,                                     # use for yaxis lower buffer if logy and close to 0
        ymax= data.second_column.max()+y_buffer,     # sets yaxis upper buffer
        ymin= data.second_column.min()-y_buffer     # sets yaxis lower if not logy close to 0
    )

用户功能2:一轴和间隔的传递参数

def plotcolumn_log_cond(some_row_entry, logaxis = 'x', interval = 10):

    """Selects data for some row entry
       Creates a scatter plot from two column variables.
       Arguments:
           Set axis to be logged (x or y as string)
           Pass interval value (as number)
       """


    # numpy fancy selector for input argument
    data = data[data.some_row_entry == some_row_entry]

    # establish plot
    data.plot.scatter(
        'first_column',
        'second_column',
        logx=True)


    # LOG XAXIS
    if logaxis = 'x':

        # establish plot
        data.plot.scatter(
            'first_column',
            'second_column',
            logx=True
        )

        # axis range controls
        x_interval = interval

        # x axis (ie x-axis variable)
        x_buffer = (data.first_column.max() - data.first_column.min()) / x_interval

        plt.axis(
            xmin=1,                                      # use for xaxis lower buffer if logx and close to 0
            xmax=data.first_column.max()+x_buffer,       # sets xaxis upper buffer
            #xmin=data.first_column.min()-x_buffer,      # sets xaxis lower buffer if not logx close to 0
        )


    # LOG YAXIS
    if logaxis = 'y':

        # establish plot
        data.plot.scatter(
            'first_column',
            'second_column',
            logy=True
        )

        # axis range controls
        y_interval = interval

        # x axis (ie x-axis variable)
        y_buffer = (data.second_column.max() - data.second_column.min()) / y_interval

        plt.axis(
            ymin=1,                                       # use for yaxis lower buffer if logy and close to 0
            ymax=data.second_column.max()+y_buffer,       # sets yaxis upper buffer
            #ymin=data.second_column.min()-y_buffer,      # sets yaxis lower buffer if not logy close to 0
        )


    # NOT X OR Y PASSED
    if (logaxis != 'x') & (logaxis != 'y'):

        # establish plot
        data.plot.scatter(
            'first_column',
            'second_column')