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

Skip to content

Commit 5b2bcab

Browse files
authored
Merge pull request #21943 from jklymak/doc-explain-too-many
DOC: explain too many ticks
2 parents 78ab3cb + 2fc3394 commit 5b2bcab

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

doc/users/faq/howto_faq.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,59 @@ How-to
99
.. contents::
1010
:backlinks: none
1111

12+
13+
.. _how-to-too-many-ticks:
14+
15+
Why do I have so many ticks, and/or why are they out of order?
16+
--------------------------------------------------------------
17+
18+
One common cause for unexpected tick behavior is passing a *list of strings
19+
instead of numbers or datetime objects*. This can easily happen without notice
20+
when reading in a comma-delimited text file. Matplotlib treats lists of strings
21+
as *categorical* variables
22+
(:doc:`/gallery/lines_bars_and_markers/categorical_variables`), and by default
23+
puts one tick per category, and plots them in the order in which they are
24+
supplied.
25+
26+
.. plot::
27+
:include-source:
28+
:align: center
29+
30+
import matplotlib.pyplot as plt
31+
import numpy as np
32+
33+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2))
34+
35+
ax[0].set_title('Ticks seem out of order / misplaced')
36+
x = ['5', '20', '1', '9'] # strings
37+
y = [5, 20, 1, 9]
38+
ax[0].plot(x, y, 'd')
39+
ax[0].tick_params(axis='x', labelcolor='red', labelsize=14)
40+
41+
ax[1].set_title('Many ticks')
42+
x = [str(xx) for xx in np.arange(100)] # strings
43+
y = np.arange(100)
44+
ax[1].plot(x, y)
45+
ax[1].tick_params(axis='x', labelcolor='red', labelsize=14)
46+
47+
The solution is to convert the list of strings to numbers or
48+
datetime objects (often ``np.asarray(numeric_strings, dtype='float')`` or
49+
``np.asarray(datetime_strings, dtype='datetime64[s]')``).
50+
51+
For more information see :doc:`/gallery/ticks/ticks_too_many`.
52+
53+
.. _howto-determine-artist-extent:
54+
55+
Determine the extent of Artists in the Figure
56+
---------------------------------------------
57+
58+
Sometimes we want to know the extent of an Artist. Matplotlib `.Artist` objects
59+
have a method `.Artist.get_window_extent` that will usually return the extent of
60+
the artist in pixels. However, some artists, in particular text, must be
61+
rendered at least once before their extent is known. Matplotlib supplies
62+
`.Figure.draw_without_rendering`, which should be called before calling
63+
``get_window_extent``.
64+
1265
.. _howto-figure-empty:
1366

1467
Check whether a figure is empty

examples/ticks/ticks_too_many.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
=====================
3+
Fixing too many ticks
4+
=====================
5+
6+
One common cause for unexpected tick behavior is passing a list of strings
7+
instead of numbers or datetime objects. This can easily happen without notice
8+
when reading in a comma-delimited text file. Matplotlib treats lists of strings
9+
as *categorical* variables
10+
(:doc:`/gallery/lines_bars_and_markers/categorical_variables`), and by default
11+
puts one tick per category, and plots them in the order in which they are
12+
supplied. If this is not desired, the solution is to convert the strings to
13+
a numeric type as in the following examples.
14+
15+
"""
16+
17+
############################################################################
18+
# Example 1: Strings can lead to an unexpected order of number ticks
19+
# ------------------------------------------------------------------
20+
21+
import matplotlib.pyplot as plt
22+
import numpy as np
23+
24+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.5))
25+
x = ['1', '5', '2', '3']
26+
y = [1, 4, 2, 3]
27+
ax[0].plot(x, y, 'd')
28+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
29+
ax[0].set_xlabel('Categories')
30+
ax[0].set_title('Ticks seem out of order / misplaced')
31+
32+
# convert to numbers:
33+
x = np.asarray(x, dtype='float')
34+
ax[1].plot(x, y, 'd')
35+
ax[1].set_xlabel('Floats')
36+
ax[1].set_title('Ticks as expected')
37+
38+
############################################################################
39+
# Example 2: Strings can lead to very many ticks
40+
# ----------------------------------------------
41+
# If *x* has 100 elements, all strings, then we would have 100 (unreadable)
42+
# ticks, and again the solution is to convert the strings to floats:
43+
44+
fig, ax = plt.subplots(1, 2, figsize=(6, 2.5))
45+
x = [f'{xx}' for xx in np.arange(100)]
46+
y = np.arange(100)
47+
ax[0].plot(x, y)
48+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
49+
ax[0].set_title('Too many ticks')
50+
ax[0].set_xlabel('Categories')
51+
52+
ax[1].plot(np.asarray(x, float), y)
53+
ax[1].set_title('x converted to numbers')
54+
ax[1].set_xlabel('Floats')
55+
56+
############################################################################
57+
# Example 3: Strings can lead to an unexpected order of datetime ticks
58+
# --------------------------------------------------------------------
59+
# A common case is when dates are read from a CSV file, they need to be
60+
# converted from strings to datetime objects to get the proper date locators
61+
# and formatters.
62+
63+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.75))
64+
x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-09-01']
65+
y = [0, 2, 3, 1]
66+
ax[0].plot(x, y, 'd')
67+
ax[0].tick_params(axis='x', labelrotation=90, color='r', labelcolor='r')
68+
ax[0].set_title('Dates out of order')
69+
70+
# convert to datetime64
71+
x = np.asarray(x, dtype='datetime64[s]')
72+
ax[1].plot(x, y, 'd')
73+
ax[1].tick_params(axis='x', labelrotation=90)
74+
ax[1].set_title('x converted to datetimes')
75+
76+
plt.show()

0 commit comments

Comments
 (0)