Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

None as tick label converted to empty string #15630

Open
wesbarnett opened this issue Nov 7, 2019 · 2 comments

Comments

@wesbarnett
Copy link

@wesbarnett wesbarnett commented Nov 7, 2019

Bug report

I was surprised that when None is a tick label it gets converted to an empty string instead of NaN or simply None. Maybe this is a weird cornercase, but I actually have a case where one category is None and another category is "" so when they are used as labels, they show up as the same thing.

Code for reproduction

labels = ["A", "B", "C", "", None]
values = [1.0, 2.0, 3.0, 4.0, 5.0]
plt.barh(np.arange(len(values)), values, tick_label=labels)

In contrast and as a counterexample from another package, when used as index values, pandas actually converts None to NaN. For example, run:

pd.DataFrame(values, index=labels)

Actual outcome

image

Expected outcome

image

Alternatively, if I plot using pandas

pd.DataFrame(values, index=labels).plot.barh()

I get the following plot:

image

Matplotlib version

  • Operating system: macOS Mojave (10.14.5)
  • Matplotlib version: 3.1.1
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.4
  • Jupyter version (if applicable): 1.0.0
  • Other libraries:

I installed matplotlib via pip in a virtual environment.

@wesbarnett wesbarnett changed the title None as xtic label converted to empty string None as tick label converted to empty string Nov 7, 2019
@timhoffm

This comment has been minimized.

Copy link
Member

@timhoffm timhoffm commented Nov 7, 2019

None resulting in no label is the intended way of working in Matplotlib. Casting None to a string would be a bit of an assumption. I can't speak for pandas, but None -> NaN conversion seems like a type coercion (may happen if you force a list containing non-numeric data into a numeric column).

If you want the string "None", you should pass it in as such [str(l) for l in labels], or map(str, labels).

@anntzer

This comment has been minimized.

Copy link
Contributor

@anntzer anntzer commented Nov 8, 2019

fwiw I think the situation with labels is pretty awkward: we convert everything to strings, except None (and that change is quite recent (depending on the specific place) -- #10392, #14660). If someone wants to write robust, generic code, they can't rely on that autoconversion unless they know for sure the label can't be None (basically, it's the OP's issue).
I would support deprecating passing parameters other than str or None. (cf. plot(["1", "2"]) which used to mean plot([1, 2]) before the implementation of categoricals -- well I don't know whether that really supports doing the change given the havoc that the change caused, but I see these as being the same kind of change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.