set axis limits in loglog plot with matplotlib


Question

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.

1
2
7/6/2012 11:12:08 PM

Accepted Answer

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.

10
7/6/2012 11:16:58 PM

If you are looking for a general way to handle this problem and want to automatically adjust the limits of your plot (even without knowing anything of your data), you can also write a snippet inspired by this answer to a similar question.

Note that you will have to tweak the code a little bit and change it so it also do the job for the y axis.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon