Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set axis limits in loglog plot with matplotlib

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.

like image 414
Joe Avatar asked Jul 06 '12 23:07

Joe


2 Answers

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.

like image 54
tacaswell Avatar answered Oct 10 '22 18:10

tacaswell


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.

like image 28
gcalmettes Avatar answered Oct 10 '22 19:10

gcalmettes