Skip to content

DOC: multilevel tick example #27411

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

Merged
merged 2 commits into from
Dec 2, 2023
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Dec 1, 2023

This adds an example showing one method of adding multilevel ticks to a plot.

multilevel

Not a substitute for #6321, but shows that it's possible with minimal fussing. Also partially addresses #27394, or at least gives an example of how to achieve the date axis effect discussed there

@jklymak jklymak force-pushed the doc-multilevel-ticks branch 2 times, most recently from e52f7d4 to c39134b Compare December 1, 2023 17:04
Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. I particularly like the progression from simple to complicated example, so the reader can see how it builds up.

My comments are all very picky, so please take or leave any of them.

# label the months:
sec = ax.secondary_xaxis(location=-0.075)
sec.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=1))
sec.xaxis.set_major_formatter(mdates.DateFormatter(' %b'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you add the spaces before the %b to improve the alignment with the tick. I just spent longer than I care to admit trying to figure out a way to set the horizontal alignment of the labels, and now I think I understand why you want to overhaul Axis!

For the record, this works, but I'm not sure it would be an improvement.

fig.draw_without_rendering()  # Otherwise only one label exists to loop through.  Is there a cheaper way to force tick generation?
for label in sec.xaxis.get_ticklabels():
    label.set_horizontalalignment('left')

Is it worth a comment to explain the spaces though?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's all pretty annoying. And of course that alignment only holds for ticks that presently exist - if you pan off to right or left the new ticks won't get the alignment.

Copy link
Member

@story645 story645 Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aI think lighter lift to improve this would be to add alignment to tick_params #20644

# This example also differs from the above, in that we placed it at a
# location below the main axes ``location=-0.075`` and then we hide the spine
# by setting the line width to zero. That means that our formatter no longer
# needs the carriage returns of the previous two examples.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a sentence near the top somewhere to explain why we do add the "\n" in those examples.


ax.plot(['cats', 'dogs', 'pigs', 'snakes', 'lizards', 'chickens',
'eagles', 'herons', 'buzzards'],
rng.normal(size=9))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rng.normal(size=9))
rng.normal(size=9), 'o')

I know it doesn't matter a jot for the purpose of the example, but lines between categorical data don't make sense to me.

sec.set_xticks([5, 15, 25], labels=['\nOughts', '\nTeens', '\nTwenties'])

# %%
# This example adds a second level of annotation to a categorical axis. Note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This example adds a second level of annotation to a categorical axis. Note
# The second example adds a second level of annotation to a categorical axis. Note

To make it really obvious to the reader that we are moving on.

Comment on lines 36 to 37
# This example adds a second level of annotation to a categorical axis. Note
# that here we need to note that each animal (category) is assigned an integer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This example adds a second level of annotation to a categorical axis. Note
# that here we need to note that each animal (category) is assigned an integer,
# This example adds a second level of annotation to a categorical axis.
# Here we need to note that each animal (category) is assigned an integer,

Scans a little better for me.


# %%
# Dates are another common place where we may want to have a second level of tick
# labels. Here we take advantage of the ability to add an automatic locator and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# labels. Here we take advantage of the ability to add an automatic locator and
# labels. Next we take advantage of the ability to add an automatic locator and

Moving on again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"In this last example,"

@jklymak jklymak force-pushed the doc-multilevel-ticks branch from c39134b to 8405388 Compare December 2, 2023 16:59
`development version <https://matplotlib.org/devdocs/devel/index.html>`_
of this document as it will have the most up to date installation
instructions, workflow process, and contributing guidelines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this come from? I didn't see it before and it doesn't show in the "force-push" diff 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had that happen for this PR a couple of times where changes on main get shown as diffs in this PR? Not sure why, but a rebase on main fixes it

@jklymak jklymak force-pushed the doc-multilevel-ticks branch from 8405388 to f962e7a Compare December 2, 2023 17:10
fix typo

Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
@jklymak jklymak merged commit d8e272f into matplotlib:main Dec 2, 2023
@jklymak jklymak deleted the doc-multilevel-ticks branch December 2, 2023 22:36
@rcomer rcomer added this to the v3.8-doc milestone Dec 3, 2023
@rcomer
Copy link
Member

rcomer commented Dec 3, 2023

@meeseeksdev backport to v3.8.x

@rcomer
Copy link
Member

rcomer commented Dec 3, 2023

@meeseeksdev backport to v3.8.2-doc

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 3, 2023
rcomer added a commit that referenced this pull request Dec 3, 2023
…411-on-v3.8.2-doc

Backport PR #27411 on branch v3.8.2-doc (DOC: multilevel tick example)
rcomer added a commit that referenced this pull request Dec 3, 2023
…411-on-v3.8.x

Backport PR #27411 on branch v3.8.x (DOC: multilevel tick example)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants