Skip to content

Commit d3e0025

Browse files
committed
FIX: streamline examples
1 parent a8d278d commit d3e0025

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

doc/users/faq/howto_faq.rst

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ How-to
1010
:backlinks: none
1111

1212

13-
_how-to-too-many-ticks
13+
.. _how-to-too-many-ticks:
1414

1515
Why do I have so many ticks, and/or why are they out of order?
1616
--------------------------------------------------------------
@@ -25,50 +25,66 @@ supplied.
2525

2626
In the example below, the upper row plots are plotted using strings for *x*;
2727
note that each string gets a tick, and they are in the order of the list passed
28-
to Matplotlib. In the lower row the data is converted to either floats or
29-
datetime64; note that the ticks are now ordered and spaced numerically.
28+
to Matplotlib. If this is not desired, we need to change *x* to an array of
29+
numbers.
3030

3131
.. plot::
3232
:include-source:
3333
:align: center
3434

35-
import matplotlib.pyplot as plt
36-
import numpy as np
37-
38-
fig, ax = plt.subplots(2, 2, constrained_layout=True, figsize=(6, 6))
35+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.5))
3936
x = ['1', '5', '2', '3']
4037
y = [1, 4, 2, 3]
41-
ax[0, 0].plot(x, y, 'd')
42-
ax[0, 0].set_xlabel('Categories')
38+
ax[0].plot(x, y, 'd')
39+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
40+
ax[0].set_xlabel('Categories')
41+
ax[0].set_title('Ticks seem out of order / misplaced')
42+
4343
# convert to numbers:
4444
x = np.asarray(x, dtype='float')
45-
ax[1, 0].plot(x, y, 'd')
46-
ax[1, 0].set_xlabel('Floats')
47-
48-
x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-10-04']
49-
y = [0, 2, 3, 1]
50-
ax[0, 1].plot(x, y, 'd')
51-
ax[0, 1].tick_params(axis='x', labelrotation=45)
52-
# convert to datetime64
53-
x = np.asarray(x, dtype='datetime64[s]')
54-
ax[1, 1].plot(x, y, 'd')
55-
ax[1, 1].tick_params(axis='x', labelrotation=45)
45+
ax[1].plot(x, y, 'd')
46+
ax[1].set_xlabel('Floats')
47+
ax[1].set_title('Ticks as expected')
5648

5749
If *x* has 100 elements, all strings, then we would have 100 (unreadable)
58-
ticks:
50+
ticks, and again the solution is to convert the strings to floats:
5951

6052
.. plot::
6153
:include-source:
6254
:align: center
6355

64-
import matplotlib.pyplot as plt
65-
import numpy as np
66-
67-
fig, ax = plt.subplots(figsize=(6, 2.5))
56+
fig, ax = plt.subplots(1, 2, figsize=(6, 2.5))
6857
x = [f'{xx}' for xx in np.arange(100)]
6958
y = np.arange(100)
70-
ax.plot(x, y)
59+
ax[0].plot(x, y)
60+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
61+
ax[0].set_title('Too many ticks')
62+
ax[0].set_xlabel('Categories')
7163

64+
ax[1].plot(np.asarray(x, float), y)
65+
ax[1].set_title('x converted to numbers')
66+
ax[1].set_xlabel('Floats')
67+
68+
A common case is when dates are read from a CSV file, they need to be
69+
converted from strings to datetime objects to get the proper date locators
70+
and formatters.
71+
72+
.. plot::
73+
:include-source:
74+
:align: center
75+
76+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 3.5))
77+
x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-10-04']
78+
y = [0, 2, 3, 1]
79+
ax[0].plot(x, y, 'd')
80+
ax[0].tick_params(axis='x', labelrotation=90, color='r', labelcolor='r')
81+
ax[0].set_title('Dates out of order')
82+
83+
# convert to datetime64
84+
x = np.asarray(x, dtype='datetime64[s]')
85+
ax[1].plot(x, y, 'd')
86+
ax[1].tick_params(axis='x', labelrotation=90)
87+
ax[1].set_title('x converted to datetimes')
7288

7389
.. _howto-determine-artist-extent:
7490

0 commit comments

Comments
 (0)