Skip to content

[Bug]: set_position function of spine will affect the color of ticklabels #22672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
y9c opened this issue Mar 20, 2022 · 7 comments
Open

[Bug]: set_position function of spine will affect the color of ticklabels #22672

y9c opened this issue Mar 20, 2022 · 7 comments

Comments

@y9c
Copy link
Contributor

y9c commented Mar 20, 2022

Bug summary

When .set_position() function is applied to the plot, color of the ticklabels will change.

Code for reproduction

Block A

fig, ax = plt.subplots()
ax.plot([1, 2, 3])
for lab in ax.get_yticklabels():
    lab.set_color("r")
fig.show()


Block B

```python
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
for lab in ax.get_yticklabels():
    lab.set_color("r")
ax.spines.left.set_position(("outward", 10))
fig.show()

Actual outcome

When apply set_position function, the color will be reset.

Expected outcome

Color of yticklables should be red.

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

No response

@y9c y9c changed the title [Bug]: set_position f unction of spine will affect the color of ticklabels [Bug]: set_position function of spine will affect the color of ticklabels Mar 20, 2022
@oscargus
Copy link
Member

It seems to come from this:

if self.axis is not None:
self.axis.reset_ticks()

See #2941 for a bit of background why that happens.

@timhoffm
Copy link
Member

Note: It's almost never a good idea to iterate over and modify individual ticks. Ticks are volatile and can change throughout the lifecycle of a figure. The correct approach to aboves code is to use tick_params:

fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.tick_params(axis='y', labelcolor='r')
ax.spines.left.set_position(("outward", 10))
plt.show()

More details are discussed in #22856.

@y9c
Copy link
Contributor Author

y9c commented Apr 18, 2022

Thank @timhoffm . But tick_params can not change only part of the ticks, correct?

@timhoffm
Copy link
Member

@y9c If you mean "some of the ticks", yes, tick_params changes the global defaults for ticks. But that's the point. Ticks can change and if you set them individually, you have always the risk that your individual settings get lost. If you need to operate on individual ticks, I advise to do this as late as possible in your figure creation.

@y9c
Copy link
Contributor Author

y9c commented Jun 3, 2022

Hi @timhoffm , I tried to set tick label color after all plotting function, but still don't work. It seem that once set_position function is called, the color will always be reset.

@timhoffm
Copy link
Member

timhoffm commented Jun 3, 2022

fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.spines.left.set_position(("outward", 10))
for lab in ax.get_yticklabels():
    lab.set_color("r")
plt.show()

works for me:
image

@oscargus
Copy link
Member

In https://matplotlib.org/stable/tutorials/intermediate/artists.html it is shown that one can iterate over the ticks and tick labels. This should probably be updated.

Also, the approach there will not affect the tick color as expected (not at all...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants