|
| 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