Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: consistent behaviour for stem #23127

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

Closed
Atcold opened this issue May 24, 2022 · 7 comments
Closed

[ENH]: consistent behaviour for stem #23127

Atcold opened this issue May 24, 2022 · 7 comments

Comments

@Atcold
Copy link

Atcold commented May 24, 2022

Problem

stem and plot should be interchangeable.

from matplotlib.pylab import *
style.use(['bmh', 'dark_background'])
a = randn(5)
b = randn(5)

Plotting two lines

plot(a)
plot(b)

image

Choosing the colours

plot(a, 'r')
plot(b, 'b')

image

Now with stem

stem(a)
stem(b)

image

Same colour? Why? Which one is which???

Okay, let me choose the colours

stem(a, 'r')
stem(b, 'b')

ERROR!

Proposed solution

Share the same basic syntax across functions visualising the same type of data?

@timhoffm
Copy link
Member

timhoffm commented May 24, 2022

Thank you for the feedback.

stem has more elements and thus the styling is more complex: You can set the format for line marker and baseline independently: stem([locs,] heads, linefmt=None, markerfmt=None, basefmt=None). It's not canonically clear what format parts stem(a, 'r') should affect. Analogy does not extend to formats 1:1.

The second aspect is default coloring. Stem does not participate in the color cycle. One can argue that it should and that it's a design flaw.

However, please note that the proposed changes would change the look of existing plots. We are very defensive of such changes. Not breaking our users plots has often a higher priority than making the API a bit better. We have a long-term list for future style changes to be considered for a breaking major release (#14331). I've added the color-cycle participation there.

@jklymak
Copy link
Member

jklymak commented May 25, 2022

I would guess that most folks calling stem multiple times would prefer it cycle, and most current plots calling it multiple times would manually set the style or color. So I think participating in the colorcycle is a reasonable new feature that won't break very many people. I'm not sure this needs to wait like some of the rest of #14331.

@Atcold
Copy link
Author

Atcold commented May 27, 2022

@jklymak nailed the point regarding the waitlist.

stem has more elements and thus the styling is more complex: You can set the format for line marker and baseline independently: stem([locs,] heads, linefmt=None, markerfmt=None, basefmt=None). It's not canonically clear what format parts stem(a, 'r') should affect. Analogy does not extend to formats 1:1.

stem(a, 'r') should be a shortcut for stem(a, linefmt='r-', markerfmt='ro'), meaning the default colour (C0) should now be red (r). It doesn't sound to me too convoluted.

if linefmt is None:
linefmt = args[0] if len(args) > 0 else "C0-"
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
if markerfmt is None:
markerfmt = args[1] if len(args) > 1 else "C0o"

More precisely, a new attribute colour = 'C0' should replace the string 'C0' above, and such attribute can be modified when passing an array-like and a string as arguments to stem.

@timhoffm
Copy link
Member

The danger of people calling stem multiple times and relying that it does not change color is relatively low. But if stem takes part in the color cycle, it would advance the cycle, so combinations of stem and plot would also change from same color to separate colors. We can discuss in the next dev call whether we dare change that.

Concerning a fmt parameter. We already have an overloaded stem([locs,] heads, ...). Making that stem([locs,] heads, [fmt], ... is a nightmare API-wise. You would need to support stem(locs, heads) and stem(heads, fmt), which means you have to introspect the variables and guess from them what the user could have meant. I strongly suggest not going there. We have some version of that in plot, but it results in quite complicated code, and sometimes we have to cope with real ambiguity.

@Atcold
Copy link
Author

Atcold commented Jun 9, 2022

My point is that, given that

We have some version of that in plot

such behaviour should be extended for uniformity.
Currently, matplotlib fights me when I try switching chart types.
The API should be consistent.

That's it.

@timhoffm
Copy link
Member

timhoffm commented Jun 9, 2022

Currently, matplotlib fights me when I try switching chart types.

I recognize and respect that.

Uniformity is important, but it's only one among multiple aspects to consider when defining the API. We're not making it better by spreading problematic API through the library. If I could design from scratch, I would do a number of things differently. But in reality we have to compromise somewhere between backward-compatibility, consistency, and clean design.

That said, I believe we can balance the aspects here better.

  1. plt.stem(y, 'r') is currently valid API and should work (it never did) - fix: Fix passing stem markerfmt positionally when locs are not given #23232

  2. In 3.5 we deprecated kwarg usage of all formatting parameters. In hindsight that is a bit too restrictive. plt.stem(x, y, 'r') should be allowed, in analogy to `plt.plot(x, y, 'r'). - also addressed in Fix passing stem markerfmt positionally when locs are not given #23232

  3. Change the markerfmt - see Default stem marker color follows the linecolor #23233

    Default: 'C0o', i.e. filled circles with the first color of the color cycle.

    to

    If markerfmt does not contain a color, take the color from linefmt. If markerfmt does not contain a marker, take 'o'.

    Technically, this is a breaking change. But ihink it's rare that people would want to switch the line color, but implicitly keep the markercolor defaulting to 'C0'.

@Atcold
Copy link
Author

Atcold commented Jun 21, 2022

Awesome, thanks. ❤️

@Atcold Atcold closed this as completed Jun 21, 2022
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

4 participants