@@ -23,68 +23,34 @@ as "categorical" variables
2323puts one tick per "category", and plots them in the order in which they are
2424supplied.
2525
26- In the example below, the upper row plots are plotted using strings for *x *;
27- note that each string gets a tick, and they are in the order of the list passed
28- to Matplotlib. If this is not desired, we need to change *x * to an array of
29- numbers.
30-
3126.. plot ::
3227 :include-source:
3328 :align: center
3429
35- fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.5))
36- x = ['1', '5', '2', '3']
37- y = [1, 4, 2, 3]
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-
43- # convert to numbers:
44- x = np.asarray(x, dtype='float')
45- ax[1].plot(x, y, 'd')
46- ax[1].set_xlabel('Floats')
47- ax[1].set_title('Ticks as expected')
30+ import matplotlib.pyplot as plt
31+ import numpy as np
4832
49- If *x * has 100 elements, all strings, then we would have 100 (unreadable)
50- ticks, and again the solution is to convert the strings to floats:
33+ fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2))
5134
52- .. plot ::
53- :include-source:
54- :align: center
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)
5540
56- fig, ax = plt.subplots(1, 2, figsize=(6, 2.5) )
57- x = [f'{xx}' for xx in np.arange(100)]
41+ ax[1].set_title('Many ticks' )
42+ x = [str(xx) for xx in np.arange(100)] # strings
5843 y = np.arange(100)
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')
63-
64- ax[1].plot(np.asarray(x, float), y)
65- ax[1].set_title('x converted to numbers')
66- ax[1].set_xlabel('Floats')
44+ ax[1].plot(x, y)
45+ ax[1].tick_params(axis='x', labelcolor='red', labelsize=14)
6746
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.
47+ The solution is to convert the list of strings to numbers or
48+ datetime objects (often ``np.asarray(['2', '5', '1'], dtype='float') `` or::
7149
72- .. plot ::
73- :include-source:
74- :align: center
50+ np.asarray(['2021-10-01', '2021-11-02', '2021-12-03'],
51+ dtype='datetime64[s]')
7552
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')
53+ For more information see :doc: `/gallery/ticks/ticks_too_many `.
8854
8955.. _howto-determine-artist-extent :
9056
0 commit comments