72

I have following simple plot, and I would like to display the origin axis (x, y). I already have grid, but I need the x, y axis to be emphasized.

enter image description here

this is my code:

x = linspace(0.2,10,100)
plot(x, 1/x)
plot(x, log(x))
axis('equal')
grid()

I have seen this question. The accepted answer suggests to use "Axis spine" and just links to some example. The example is however too complicated, using subplots. I am unable to figure out, how to use "Axis spine" in my simple example.

Community
  • 1
  • 1
Martin Vegter
  • 136
  • 9
  • 32
  • 56

3 Answers3

119

Using subplots is not too complicated, the spines might be.

Dumb, simple way:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')

And I get:

with axlines

(you can't see the vertical axis since the lower x-limit is zero.)

Alternative using simple spines

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

# set the x-spine (see below for more info on `set_position`)
ax.spines['left'].set_position('zero')

# turn off the right spine/ticks
ax.spines['right'].set_color('none')
ax.yaxis.tick_left()

# set the y-spine
ax.spines['bottom'].set_position('zero')

# turn off the top spine/ticks
ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()

with_spines

Alternative using seaborn (my favorite)

import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set(style='ticks')

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
seaborn.despine(ax=ax, offset=0) # the important part here

with_seaborn

Using the set_position method of a spine

Here are the docs for a the set_position method of spines:

Spine position is specified by a 2 tuple of (position type, amount). The position types are:

  • 'outward' : place the spine out from the data area by the specified number of points. (Negative values specify placing the
    spine inward.)

  • 'axes' : place the spine at the specified Axes coordinate (from 0.0-1.0).

  • 'data' : place the spine at the specified data coordinate.

Additionally, shorthand notations define a special positions:

  • 'center' -> ('axes',0.5)
  • 'zero' -> ('data', 0.0)

So you can place, say the left spine anywhere with:

ax.spines['left'].set_position((system, poisition))

where system is 'outward', 'axes', or 'data' and position in the place in that coordinate system.

Eric
  • 95,302
  • 53
  • 242
  • 374
Paul H
  • 65,268
  • 20
  • 159
  • 136
3

Some time has passed since this question was asked. With Matplotlib 3.6.2 it looks like this works:

plt.axhline(0, color='black', linewidth=.5)
plt.axvline(0, color='black', linewidth=.5)

and there are other options.

Kenneth Evans
  • 2,179
  • 19
  • 26
0

Let me answer to this (rather old) question for those who will search for it as I just did. Although it suggested working solutions, I consider the (only) provided answer as way too complex, when it comes to such a simple situation like that described in the question (note: this method requires you to specify all axes endpoints).

I found a simple working solution in one of the first tutorials on matplotlib's pyplot. It is sufficient to add the following line after the creation of the plot

plt.axis([xmin, xmax, ymin, ymax])

as in the following example:

from matplotlib import pyplot as plt

xs = [1,2,3,4,5]
ys = [3,5,1,2,4]

plt.scatter(xs, ys)
plt.axis([0,6,0,6])  #this line does the job
plt.show()

which produces the following result:

matplotlib plot with axes endpoints specified

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 2
    the user needs the x, y axis to be emphasized, but your solution seems only clip the axis. this does not provide the solution to the question – armamut Jan 18 '21 at 19:29
  • 2
    This solution helped me. Granted it clips the grid, which isn't ideal for this particular question(I would recommend the publisher edit it), but it does demonstrate how to force the axes to start from a particular number, which is exactly what I was looking for. – MrJedi2U Feb 10 '21 at 16:59