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

Skip to content

Commit 6355d1b

Browse files
committed
DOC: add new example
1 parent d3e0025 commit 6355d1b

File tree

2 files changed

+85
-51
lines changed

2 files changed

+85
-51
lines changed

doc/users/faq/howto_faq.rst

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,34 @@ as "categorical" variables
2323
puts one tick per "category", and plots them in the order in which they are
2424
supplied.
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

examples/ticks/ticks_too_many.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
import matplotlib.pyplot as plt
18+
import numpy as np
19+
20+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.5))
21+
x = ['1', '5', '2', '3']
22+
y = [1, 4, 2, 3]
23+
ax[0].plot(x, y, 'd')
24+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
25+
ax[0].set_xlabel('Categories')
26+
ax[0].set_title('Ticks seem out of order / misplaced')
27+
28+
# convert to numbers:
29+
x = np.asarray(x, dtype='float')
30+
ax[1].plot(x, y, 'd')
31+
ax[1].set_xlabel('Floats')
32+
ax[1].set_title('Ticks as expected')
33+
34+
############################################################################
35+
# If *x* has 100 elements, all strings, then we would have 100 (unreadable)
36+
# ticks, and again the solution is to convert the strings to floats:
37+
38+
fig, ax = plt.subplots(1, 2, figsize=(6, 2.5))
39+
x = [f'{xx}' for xx in np.arange(100)]
40+
y = np.arange(100)
41+
ax[0].plot(x, y)
42+
ax[0].tick_params(axis='x', color='r', labelcolor='r')
43+
ax[0].set_title('Too many ticks')
44+
ax[0].set_xlabel('Categories')
45+
46+
ax[1].plot(np.asarray(x, float), y)
47+
ax[1].set_title('x converted to numbers')
48+
ax[1].set_xlabel('Floats')
49+
50+
############################################################################
51+
# A common case is when dates are read from a CSV file, they need to be
52+
# converted from strings to datetime objects to get the proper date locators
53+
# and formatters.
54+
55+
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.75))
56+
x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-09-01']
57+
y = [0, 2, 3, 1]
58+
ax[0].plot(x, y, 'd')
59+
ax[0].tick_params(axis='x', labelrotation=90, color='r', labelcolor='r')
60+
ax[0].set_title('Dates out of order')
61+
62+
# convert to datetime64
63+
x = np.asarray(x, dtype='datetime64[s]')
64+
ax[1].plot(x, y, 'd')
65+
ax[1].tick_params(axis='x', labelrotation=90)
66+
ax[1].set_title('x converted to datetimes')
67+
68+
plt.show()

0 commit comments

Comments
 (0)