Skip to content

[Bug]: spines and ticklabels #20791

Closed
@issimo007

Description

@issimo007

Bug summary

Hello everyone, I am not sure if this is a bug or just how the spines function (https://matplotlib.org/stable/api/spines_api.html) is supposed to work. Basically, if I set the spines command "after" the tick_params specs, the "labelrotation" commands is not followed but everything else is. Any suggestions? Seems kind of weird that only some of the tick_params are executed and not others. Thank you!

Key aspect of code below:
#ax1.spines["top"].set_position(("axes", 1.05))
ax1.tick_params(axis='x', colors="green", grid_color='g',labelsize='small', labelrotation = 45)
#ax1.spines["top"].set_position(("axes", 1.05))

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

X = np.arange(100)
Y = X**2+3

fig1, ax = plt.subplots()
plt.subplots_adjust(top=0.9)

ax1 = plt.subplot2grid((1,2), (0,0), rowspan=1, colspan = 1)                 # Track 1
ax2 = plt.subplot2grid((1,2), (0,1), rowspan=1, colspan = 1, sharey = ax1)   # Track 2

ax11 = ax1.twiny()
ax11.xaxis.set_visible(False)
ax12 = ax2.twiny()
ax12.xaxis.set_visible(False)

ax1.plot(X,Y)
ax1.set_xlabel("X",fontsize='small')
ax1.set_ylabel("Y")
#ax1.spines["top"].set_position(("axes", 1.05))
ax1.tick_params(axis='x', colors="green", grid_color='g',labelsize='small', labelrotation = 45)
#ax1.spines["top"].set_position(("axes", 1.05))
ax1.set_ylim(max(Y), min(Y))

ax2.plot(X,Y)
ax2.set_xlabel("X",fontsize='small')
ax2.set_ylabel("Y")
ax2.tick_params(axis='x', colors="green", grid_color='g',labelsize='small', labelrotation = 45)
ax2.set_ylim(max(Y), min(Y))
ax2.yaxis.set_label_position("right")
ax2.yaxis.tick_right()

for ax in [ax1, ax2]:
    #ax.spines["top"].set_position(("axes", 1.05))
    ax.xaxis.set_ticks_position("top")
    ax.xaxis.set_label_position("top")
    ax.grid(b = True, which='both', axis = 'both', color='gainsboro',
            linestyle='-')
    #ax.tick_params(axis='x', labelrotation = 45)

plt.tight_layout()
fig1.subplots_adjust(wspace = 0.15)

Actual outcome

See code above.

Expected outcome

I would expect the labels to be rotated regardless of where the spine command is placed.

Operating system

Windows or MacOs

Matplotlib Version

3.4.2

Matplotlib Backend

No response

Python version

3.9.6

Jupyter version

No response

Other libraries

No response

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions