|
6 | 6 | One common cause for unexpected tick behavior is passing a list of strings
|
7 | 7 | instead of numbers or datetime objects. This can easily happen without notice
|
8 | 8 | when reading in a comma-delimited text file. Matplotlib treats lists of strings
|
9 |
| -as "categorical" variables |
| 9 | +as *categorical* variables |
10 | 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 |
| 11 | +puts one tick per category, and plots them in the order in which they are |
12 | 12 | supplied. If this is not desired, the solution is to convert the strings to
|
13 | 13 | a numeric type as in the following examples.
|
14 | 14 |
|
15 | 15 | """
|
16 | 16 |
|
| 17 | +############################################################################ |
| 18 | +# Example 1: Strings can lead to an unexpected order of number ticks |
| 19 | +# ------------------------------------------------------------------ |
| 20 | + |
17 | 21 | import matplotlib.pyplot as plt
|
18 | 22 | import numpy as np
|
19 | 23 |
|
|
32 | 36 | ax[1].set_title('Ticks as expected')
|
33 | 37 |
|
34 | 38 | ############################################################################
|
| 39 | +# Example 2: Strings can lead to very many ticks |
| 40 | +# ---------------------------------------------- |
35 | 41 | # If *x* has 100 elements, all strings, then we would have 100 (unreadable)
|
36 | 42 | # ticks, and again the solution is to convert the strings to floats:
|
37 | 43 |
|
|
48 | 54 | ax[1].set_xlabel('Floats')
|
49 | 55 |
|
50 | 56 | ############################################################################
|
| 57 | +# Example 3: Strings can lead to an unexpected order of datetime ticks |
| 58 | +# -------------------------------------------------------------------- |
51 | 59 | # A common case is when dates are read from a CSV file, they need to be
|
52 | 60 | # converted from strings to datetime objects to get the proper date locators
|
53 | 61 | # and formatters.
|
|
0 commit comments