Skip to content

Commit 8f3b029

Browse files
authored
Cleanup converter docs and StrCategoryConverter behavior (matplotlib#29059)
- Use getter in docs - Use a single instance of StrCategoryConverter for all types - avoids warning since some methods convert to `np.str_` and others are just `str`, which previously got separate instances - added test
1 parent 9b8f11b commit 8f3b029

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

galleries/users_explain/axes/axes_units.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
x = np.arange(100)
237237
ax = axs[0]
238238
ax.plot(x, x)
239-
label = f'Converter: {ax.xaxis.converter}\n '
239+
label = f'Converter: {ax.xaxis.get_converter()}\n '
240240
label += f'Locator: {ax.xaxis.get_major_locator()}\n'
241241
label += f'Formatter: {ax.xaxis.get_major_formatter()}\n'
242242
ax.set_xlabel(label)
@@ -245,7 +245,7 @@
245245
time = np.arange('1980-01-01', '1980-06-25', dtype='datetime64[D]')
246246
x = np.arange(len(time))
247247
ax.plot(time, x)
248-
label = f'Converter: {ax.xaxis.converter}\n '
248+
label = f'Converter: {ax.xaxis.get_converter()}\n '
249249
label += f'Locator: {ax.xaxis.get_major_locator()}\n'
250250
label += f'Formatter: {ax.xaxis.get_major_formatter()}\n'
251251
ax.set_xlabel(label)
@@ -255,7 +255,7 @@
255255
names = list(data.keys())
256256
values = list(data.values())
257257
ax.plot(names, values)
258-
label = f'Converter: {ax.xaxis.converter}\n '
258+
label = f'Converter: {ax.xaxis.get_converter()}\n '
259259
label += f'Locator: {ax.xaxis.get_major_locator()}\n'
260260
label += f'Formatter: {ax.xaxis.get_major_formatter()}\n'
261261
ax.set_xlabel(label)

lib/matplotlib/category.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ def update(self, data):
227227

228228

229229
# Register the converter with Matplotlib's unit framework
230-
units.registry[str] = StrCategoryConverter()
231-
units.registry[np.str_] = StrCategoryConverter()
232-
units.registry[bytes] = StrCategoryConverter()
233-
units.registry[np.bytes_] = StrCategoryConverter()
230+
# Intentionally set to a single instance
231+
units.registry[str] = \
232+
units.registry[np.str_] = \
233+
units.registry[bytes] = \
234+
units.registry[np.bytes_] = StrCategoryConverter()

lib/matplotlib/tests/test_category.py

+8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ def test_update_plot(self, plotter):
246246
axis_test(ax.xaxis, ['a', 'b', 'd', 'c'])
247247
axis_test(ax.yaxis, ['e', 'g', 'f', 'a', 'b', 'd'])
248248

249+
def test_update_plot_heterogenous_plotter(self):
250+
ax = plt.figure().subplots()
251+
ax.scatter(['a', 'b'], ['e', 'g'])
252+
ax.plot(['a', 'b', 'd'], ['f', 'a', 'b'])
253+
ax.bar(['b', 'c', 'd'], ['g', 'e', 'd'])
254+
axis_test(ax.xaxis, ['a', 'b', 'd', 'c'])
255+
axis_test(ax.yaxis, ['e', 'g', 'f', 'a', 'b', 'd'])
256+
249257
failing_test_cases = [("mixed", ['A', 3.14]),
250258
("number integer", ['1', 1]),
251259
("string integer", ['42', 42]),

0 commit comments

Comments
 (0)