Question

Some data I have to plot have coordinates such as (20,0), (10,0), etc... Basically some of the points belong to the x-axis.

The problem is, these points are hidden by the axis; i.e. the markers are behind the line and therefore cannot be seen properly.

Here is an example of my figure: http://i.stack.imgur.com/FNcob.png

Does anybody have an idea to solve this problem? I am running out of idea...

Thanks.

Viktor

Was it helpful?

Solution

Matplotlib "snaps" plot limits to "whole" (factors of 2, 5, 10, 100, etc) numbers, by default. This often means that your data may wind up on the boundary of the plot.

ax.margins allows you to add a padding factor before this autoscaling for the plot is calculated. It's a quick way to avoid the problem of points on the plot boundary.

As a quick example of the problem:

import matplotlib.pyplot as plt

x, y = [0, 10, 20], [10, 0, 0]
fig, ax = plt.subplots()
ax.plot(x, y, 'ko')
plt.show()

enter image description here

And an easy solution:

import matplotlib.pyplot as plt

x, y = [0, 10, 20], [10, 0, 0]
fig, ax = plt.subplots()
ax.plot(x, y, 'ko')

# Pad by 5% of the data range before autoscaling:
ax.margins(0.05)

plt.show()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top