0

Based of the plot shown in the last post of this topic : (here for Y-axis) I tried to change the script in order to obtain the same things but for the bottom of X axis. I modified the script in this way but: 1) the red Axis is present on the top as well 2) the 2 external axis have not the ticks

the code is this:

import matplotlib.pyplot as plt
import numpy as np
# To make things reproducible...
#np.random.seed(1977)
#plt.style.use(['mystyle'])
fig, ax = plt.subplots()

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twiny(), ax.twiny()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(bottom=0.5)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[1].spines['bottom'].set_position(('axes', -0.25))
axes[2].spines['bottom'].set_position(('axes', -0.5))

# To make the border of the right-most axis visible, we need to turn the frame
# on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

# And finally we get to plot things...
colors = ('Green', 'Red', 'Blue')
intAxNo = 0
x = [[0],[0],[0],[0]]
i=0
for ax, color in zip(axes, colors):
    intAxNo += 1
    i +=1
    print(i)
    #data = np.random.random(1) * np.random.random(10)
    x[i]=np.linspace(0.,4*i,100)

    ax.plot(x[i], np.sin(i*x[i]) , linestyle='-', color=color)
    if (intAxNo > 1):
        if (intAxNo == 2):
            ax.set_xlabel('%s Thing' % color, color=color, labelpad = -255 )
            ax.tick_params(axis='x', colors=color, labelbottom=True)
        elif (intAxNo == 3):
            ax.set_xlabel('%s Thing' % color, color=color, labelpad = -295 )
            ax.tick_params(axis='x', colors=color, labelbottom=True)
        ax.get_xaxis().set_tick_params(direction='out',length=6, width=2, colors='r')
    else:
        ax.set_xlabel('%s Thing' % color, color=color, labelpad = +0 )

    ax.tick_params(axis='x', colors=color, labelbottom=True)
axes[0].set_ylabel('Y-axis')


plt.show()

and I obtain this plot , may somebody help me explain what I wrong ? and what I should do

Drudox lebowsky
  • 1,020
  • 7
  • 21

1 Answers1

1

You can move the tick locations for both twin axes to the bottom using

ax.xaxis.set_ticks_position("bottom")

The same needs to be applied to the axis label so that you don't have to specify the labelpad values manually.

I removed the following line from your code

ax.get_xaxis().set_tick_params(direction='out',length=6, width=2, colors='r')

Relevant part of the code

for ax, color in zip(axes, colors):
    intAxNo += 1
    i +=1
    x[i]=np.linspace(0.,4*i,100)

    ax.plot(x[i], np.sin(i*x[i]) , linestyle='-', color=color)
    if (intAxNo > 1):
        ax.xaxis.set_ticks_position("bottom") # Added this line
        ax.xaxis.set_label_position("bottom") # Added this line
        if (intAxNo == 2):
            ax.set_xlabel('%s Thing' % color, color=color) # Removed labelpad
            ax.tick_params(axis='x', colors=color, labelbottom=True)
        elif (intAxNo == 3):
            ax.set_xlabel('%s Thing' % color, color=color) # Removed labelpad
            ax.tick_params(axis='x', colors=color, labelbottom=True, length=10)
    else:
        ax.set_xlabel('%s Thing' % color, color=color, labelpad = +0 )

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71