10

How do I create space around the points I've plotted with matplotlib?

For example, in this plot, the bottom left point is cutoff by the axis, but I would like a little more space between the point and the axis. 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()

In interactive mode, the xlim line returns (16.0, 16384), the old values instead of the new values I'm trying to set.

2 Answers 2

23

Zero can not be plotted on a loglog graph (log(0) = -inf). It is silently failing because it can not use 0 as a limit.

Try plt.xlim([1,2**14]) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Silently failing wasn't helpful, but of course 0 can't be plotted! Good answer.
1

Here I use plt.axis() to set xmin and xmax values (similar to your plt.xlim call); but I use a variable 'buffer' built on the range and an interval. The range of the axis is derived from using the min and max values. Since log scale doesn't plot 0 or negatives, I set the xmin argument equal to 1 inside of the .axis() function call.

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
)

We can do the same with the y-axis as needed. It's a lot of code to control one aspect of the plot, but I like using it within a user function if matplotlib.pyplot is being ornery.
Here's a two template user routines for trial and error. I tested the first one and it worked fine; I just built the second one as an alternative option but didn't test it...if it gives errors please let me know.

User Function #1: Total Control within Function

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
    )

User Function #2: Pass Arguments for One Axis & Interval

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.