0

As title, I have multiple subplots and would like to have the x-axis tick labels shown at y=-1 (all my subplots have ylimits (-1.1 to 1.1), while the x-axis line and ticks shown at y=0. My current redacted code is as follows:

while x < 10: 
    plt.add_subplot(10,1,x).yaxis.tick_right()
    plt.subplot(10,1,x).spines['bottom'].set_position('zero')
    plt.subplot(10,1,x).spines['bottom'].set_color('xkcd:light grey')
    plt.subplot(10,1,x).set_facecolor('xkcd:dark grey')
    plt.tick_params(axis='x', direction='out', labelbottom='on', color='xkcd:light grey', labelcolor='xkcd:light grey')
    plt.plot()
    plt.ylim(-1.1,1.1)

    x+=1

plt.show()

Current example of a subplot looks like this:

enter image description here

  • You could use `Axes.tick_params('x', pad=100)` to create padding above the x-axis labels, pushing them down to `y=-1`. Adjust the padding value until you find the position you want. – Aziz Apr 03 '18 at 04:25
  • To get better answers, please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) including the expected output. – Mr. T Apr 03 '18 at 08:05

2 Answers2

3

It's rather unusual to set the ticklabel position in data coordinates. But it's still possible. Similar to this question you may use a transform to shift the labels. Here we would first shift them by 1 data unit and then apply the usual transform.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)
import matplotlib.transforms as mtrans

x = np.linspace(2013.6,2018.4)
y = np.cumsum(np.random.randn(50))/12.+0.3

ax = plt.subplot(111)
ax.yaxis.tick_right()
ax.spines['bottom'].set_position('zero')
ax.spines['bottom'].set_color('xkcd:light grey')
ax.set_facecolor('xkcd:dark grey')
ax.tick_params(axis='x', direction='out', labelbottom=True, pad=0, 
               color='xkcd:light grey', labelcolor='xkcd:light grey')
ax.plot(x,y)
ax.set_ylim(-1.1,1.1)


trans = mtrans.Affine2D().translate(0,-1.)
for t in ax.get_xticklabels():
    t.set_transform(trans + t.get_transform())

plt.tight_layout()
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

Usually this can be set by set_position, and specifying 'data':

plt.subplot(10,1,x).spines['bottom'].set_position(('data', -1))

data’ : place the spine at the specified data coordinate.

matplotlib.spines | set_position

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • This brings the whole axis (including the ticks) down to -1. Is there a way to just bring the tick labels (ie the dates in this case) down to y=-1? – Singapore 123 Apr 03 '18 at 05:57
  • 1
    @Singapore123 : Sure there’s a way, although I’m confused by what you are asking apparently. Maybe post an example screenshot of how you want it to look. Currently when I try your code I just get a grey bar, so more of your code displaying data might be helpful as well. – l'L'l Apr 03 '18 at 06:01