Skip to content
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

Set style for figure or axes in object oriented way. #20131

Open
y9c opened this issue May 1, 2021 · 4 comments
Open

Set style for figure or axes in object oriented way. #20131

y9c opened this issue May 1, 2021 · 4 comments

Comments

@y9c
Copy link
Contributor

y9c commented May 1, 2021

Problem

  • I would like to known it is possible to set style for figure or axes after it is created. Especially in object oriented way.

Proposed Solution

  • Implant ax.set_style() and fig.set_style() method. Then we can customize styles in the way below.
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot()
ax1.set_style("ggplot")
ax2.plot()
ax2.set_style("default")
  • ax.style.use()
  • ax.style.context()
@y9c y9c added the New feature label May 1, 2021
@jklymak
Copy link
Member

jklymak commented May 1, 2021

That won't work as you expect. Most styles are applied as defaults to artists at creation, so your plot will already have the old style. It's not possible to retroactively change the styles of artists after they have been added to the figure, and trying to make it possible would be needlessly complicated.

@y9c
Copy link
Contributor Author

y9c commented May 1, 2021

Thank you for your explanation, @jklymak. I know matplotlib artists are set at the creation, and this is why I ask for a 'new featue'. There is no easy solutions for this, but I wish matplotlib will able to do this in the 4.0 version. I will also look into the code and think about the solution.

@jklymak
Copy link
Member

jklymak commented May 1, 2021

I guess my point is this is a new architecture that, I agree, there is sometimes tension to adopt. I think doing so would be a mistake however, because it is much less explicit and we will be chasing down edge cases ad-naseum. Matplotlib is based off Matlab, and the paradigm was very explicit. You put an artist on the figure, and if you want to change it, you take the handle to the artist and you change it there. The desire for a more implicit paradigm, where rcParams only get resolved at draw time, comes up all the time, but it just leads to confusion.

i.e. suppose I do

fig, (ax1, ax2) = plt.subplots(1, 2)
pl = ax1.plot(x, y, color='g')
ax1.set_style("ggplot")
...

should I then change the artist pl to have whatever ggplot defines as the first color or should it keep the color the user assigned? How will Matplotlib know if the user assigned it? We'd need to track everything the user might have set on an artist, versus what is default. Sure its possible, but I don't know what the objection is to simply redrawing the figure.

You have a second request, which is to make the style sheet apply separately to different axes. I think that works already with context managers? Its a little awkward, but it is a little strange to change styles in the middle of a figure:

import matplotlib.pyplot as plt 

fig = plt.figure()
gs = fig.add_gridspec(1,2)

with plt.style.context('ggplot'):
    ax1 = fig.add_subplot(gs[0,0])
    ax1.plot(range(10))
ax2 = fig.add_subplot(gs[0,1])
ax2.plot(range(10))

plt.show()

@dstansby
Copy link
Member

Just wanted to quickly say I think this would be a great feature. My use case is setting the style on a specific figure. Perhaps setting the style when a figure is created (ie. not modifying it after creation) would be a good and easier first step to take in this direction.

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

No branches or pull requests

3 participants