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