Closed
Description
Bug report
Bug summary
set_xticks
does not work as expected when x data is of type str. It does not skip labels.
Code for reproduction
# Paste your code here
#
#
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x = ['a', 'b', 'c']
y = [1, 2, 3]
_, ax = plt.subplots()
ax.plot(x, y)
# Tick labels are ['a', 'b', 'c']
# This is correct.
print('xticklabels', list(ax.get_xticklabels())) # xticklabels [Text(0,0,''), Text(0,0,''), Text(0,0,'')]
print('xticks', list(ax.get_xticks())) # xticks [0, 1, 2]
_, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks([1, 2])
print('xticklabels', list(ax.get_xticklabels())) # xticklabels [Text(0,0,''), Text(0,0,'')]
# Tick labels are [None, 'a', 'b']
# This is wrong. Expect [None, 'b', 'c']
print('xticks', list(ax.get_xticks())) # xticks [1.0, 2.0]
_, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks(['b', 'c'])
print('xticklabels', list(ax.get_xticklabels())) # xticklabels [Text(0,0,''), Text(0,0,'')]
# Tick labels are [None, 'a', 'b']
# This is wrong. Expect [None, 'b', 'c']
print('xticks', list(ax.get_xticks())) # xticks [1.0, 2.0]
plt.show()
Actual outcome
# If applicable, paste the console output here
#
#
xticklabels [Text(0,0,''), Text(0,0,''), Text(0,0,'')]
xticks [0, 1, 2]
xticklabels [Text(0,0,''), Text(0,0,'')]
xticks [1.0, 2.0]
xticklabels [Text(0,0,''), Text(0,0,'')]
xticks [1.0, 2.0]
Expected outcome
xlabels:
Figure 1: a b c
Figure 1: - b c instead of - a b
Figure 1: - b c instead of - a b
Matplotlib version
- Operating system:
- Matplotlib version:
- Matplotlib backend (
print(matplotlib.get_backend())
): - Python version:
- Jupyter version (if applicable):
- Other libraries:
installed from anaconda
$ pip show matplotlib
Name: matplotlib
Version: 2.1.2
Summary: Python plotting package
Home-page: http://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: BSD
Location: /opt/anaconda/lib/python3.6/site-packages
Requires: numpy, six, python-dateutil, pytz, cycler, pyparsing
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
$ py "print(matplotlib.get_backend())"
Qt5Agg
Found because of:
pandas-dev/pandas#18726