R style data-axis buffer in matplotlib

2024/10/2 12:16:53

R plots automatically set the x and y limits to put some space between the data and the axes. I was wondering if there is a way for matplotlib to do the same automatically. If not, is there a good formula or 'rule of thumb' for how R sets its axis limits?

Answer

In matplotlib you can achieve this by setting the margins

import matplotlib.pyplot as plt fig, ax = plt.subplots()
ax.margins(0.04)
data = range(1, 11) 
ax.plot(data, 'wo') 
plt.savefig('margins.png')

margins

However, it doesn't seems, that there is an rc parameter to get this automatically.

Update 4/2013

The possibility to add an rc param for the margins is now in matplotlib master (Thanks @tcaswell). So it should work with the next matplotlib release (current release is 1.2.1).

https://en.xdnf.cn/q/70852.html

Related Q&A

Python code for the coin toss issues

Ive been writing a program in python that simulates 100 coin tosses and gives the total number of tosses. The problem is that I also want to print the total number of heads and tails.Heres my code:impo…

Preprocess a Tensorflow tensor in Numpy

I have set up a CNN in Tensorflow where I read my data with a TFRecordReader. It works well but I would like to do some more preprocessing and data augmentation than offered by the tf.image functions. …

Os.path : can you explain this behavior?

I love Python because it comes batteries included, and I use built-in functions, a lot, to do the dirty job for me.I have always been using happily the os.path module to deal with file path but recentl…

admin.py for project, not app

How can I specify a project level admin.py?I asked this question some time ago and was just awarded the Tumbleweed award because of the lack of activity on the question! >_<Project:settings.py a…

Python Socket Receive/Send Multi-threading

I am writing a Python program where in the main thread I am continuously (in a loop) receiving data through a TCP socket, using the recv function. In a callback function, I am sending data through the …

numpy array2string applied on huge array, skips central values, ( ... in the middle )

I have array of size (3, 3, 19, 19), which I applied flatten to get array of size 3249.I had to write these values to file along with some other data, so I did following to get the array in string.np.a…

save password as salted hash in mongodb in users collection using python/bcrypt

I want to generate a salted password hash and store it in MongoDB collection called users, like this:users_doc = { "username": "James","password": "<salted_hash_pa…

Get the min of [0, x] element wise for a column

I need to compute a column where the value is the result of a vectorized operation over other columns: df["new_col"] = df["col1"] - min(0,df["col2"])It turned out, however…

Virtual column in QTableView?

Im started to learning Qt4 Model/View Programming and I have beginner question.I have simple application which show sqlite table in QTableView:class Model(QtSql.QSqlTableModel):def __init__(self, paren…

Python httplib2, AttributeError: set object has no attribute items

Im playing with the Python library httplib2. The following is my code. import urllib.parse import httplib2httplib2.debuglevel = 1http = httplib2.Http()url = "http://login.sina.com.cn/hd/signin.php…