0

I am trying to draw a plot like this.

I studied the examples come from matplotlib.

What I have known is

  1. left spine could play a role of y axis in a Cartesian coordinate plane.

  2. bottom spine could play a role of x axis in a Cartesian coordinate plane.

  3. set_position() could shift spines.

My question is what the difference between putting a spine (say, 'left') to 'center' and 'zero'.

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2 * np.sin(x)

ax = fig.add_subplot(2, 2, 1)
ax.set_title('centered spines')
ax.plot(x, y)
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

ax = fig.add_subplot(2, 2, 2)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.show()
  • If `0` is in the center of your plot, there obviously is no difference between `"zero"` and `"center"`. Use non-symmetric data like `x = np.linspace(-1.5, np.pi, 100)` to see the difference. – ImportanceOfBeingErnest Feb 26 '19 at 00:55
  • @ImportanceOfBeingErnest would you please describe a scenario where 0 is not the center, besides the examples in matplotlib site ('spines at data (1, 2)' and 'spines at axes (0.6, 0.1)') –  Feb 26 '19 at 02:17
  • An example is the one I mentionned in the first comment, `x = np.linspace(-1.5, np.pi, 100)`. – ImportanceOfBeingErnest Feb 27 '19 at 21:48

0 Answers0