Skip to content

bugfix for plot for string x values #18726

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

Closed
wants to merge 6 commits into from

Conversation

boeddeker
Copy link

@boeddeker boeddeker commented Dec 11, 2017

Matplotlib can handle string data, so _get_xticks does not need to convert strings to int

@@ -573,6 +573,8 @@ def _get_xticks(self, convert_period=False):
self.data = self.data[notna(self.data.index)]
self.data = self.data.sort_index()
x = self.data.index._mpl_repr()
elif all(isinstance(val, str) for val in index):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the general preference is to use pandas.compat.string_types for isinstance checks against strings. Looks like string_types is already imported, so just switching to isinstance(val, string_types) should be fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I changed the line

@@ -573,6 +573,8 @@ def _get_xticks(self, convert_period=False):
self.data = self.data[notna(self.data.index)]
self.data = self.data.sort_index()
x = self.data.index._mpl_repr()
elif all(isinstance(val, string_types) for val in index):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be slow for large indexes. Try index.is_object().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is_object equal to is_str? The reason for the generator is that in my example the datatype of the index was wrong (i.e. object and not str) and the generator is only slow when there are str objects. My assumption was that in the case that the index contains str, the index size is relative small.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do
index.inferred_type in ['string', 'unicode'] is pretty performant

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I changed the code to use index.inferred_type

@jreback
Copy link
Contributor

jreback commented Dec 12, 2017

can you add a whatsnew note in 0.22
can you add a test for this

@pep8speaks
Copy link

pep8speaks commented Dec 12, 2017

Hello @boeddeker! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on February 11, 2018 at 11:29 Hours UTC

@boeddeker
Copy link
Author

I add a whatsnew note and a test


line1, line2 = ax.lines
# xdata should not be touched (Earlier it was [0, 1])
np.testing.assert_equal(line1.get_xdata(), x1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use np.testing, instead use tm.assert_numpy_array_equal

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tm.assert_numpy_array_equal does not work and tm.assert_array_equal does not exsist (The comparison is between numpy and a list).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure it does
turn the list into a numpy array
you can not compare a list and an array in our testing by default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I made that change

ax = df2.plot(ax=ax)

line1, line2 = ax.lines
# xdata should not be touched (Earlier it was [0, 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line before comments

@@ -292,7 +292,7 @@ Plotting
^^^^^^^^

- :func: `DataFrame.plot` now raises a ``ValueError`` when the ``x`` or ``y`` argument is improperly formed (:issue:`18671`)
-
- Bug in :func: `DataFrame.plot` where the ``xtickvalues`` are overwritten (:issue:`#18687`)
-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space after :func:, can you add a bit more text on what a user would want to know here

issue is: (:issue:`18687`) (no #)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line above mine has also a whitespace after :func:

@jreback
Copy link
Contributor

jreback commented Feb 10, 2018

pls rebase

@boeddeker
Copy link
Author

Ok, I did a rebase with master.
But I was not able to merge my changes in whats new, so I dropped my note there.

- Bug in :func:`DataFrame.plot` the second call writing in the same figure where both have ``x`` values of type string overwrote the ``xticklabels`` of the first call (:issue:`18687`)
-

@TomAugspurger
Copy link
Contributor

Can you add another commit with the release note? Those need to be included in the docs.

It looks like this changes is failing other tests: https://travis-ci.org/pandas-dev/pandas/jobs/340101818#L2288. Do those fail for you locally?

@boeddeker
Copy link
Author

Where should I add a release note? The file where I originally added the note, has completely changed.

Regarding the test, the test also fails local on my machine.
I think the reason is that there is a bug in matplotlib, because the example below does not return the expected plot. get_xticklabels is empty (i.e. all values are Text(0,0,'')), while the plot contains labels.
So maybe a bugfix in matplotlib is recommented, before this PR is merged into pandas.

_, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks([1, 2])  # -> x labels: ['', 'a', 'b']
print('xticklabels', list(ax.get_xticklabels()))  # xticklabels [Text(0,0,''), Text(0,0,'')]
print('xticks', list(ax.get_xticks()))  # xticks [1.0, 2.0]


_, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks(['b', 'c'])  # -> x labels: ['', 'a', 'b']
print('xticklabels', list(ax.get_xticklabels()))  # xticklabels [Text(0,0,''), Text(0,0,'')]
print('xticks', list(ax.get_xticks()))  # xticks [1.0, 2.0]

@jreback
Copy link
Contributor

jreback commented Feb 21, 2018

note goes in whatsnew for 0.23.0 / bugfixes / plotting

@boeddeker
Copy link
Author

According to matplotlib/matplotlib#10547 matplotlib will change in version 2.2 (currently it is 2.2.rc1) the categorical code, so in my mind, this PR could only be merged, when pandas set the minimum required version of matplotlib to version 2.2.

I do not know, what pandas convention is for requiring a minimum version of a library, especially in the case of matplotlib (The install_requires in setup.py does not contain matplotlib)

@TomAugspurger
Copy link
Contributor

Our minimum matplotlib is pretty old. We usually work around bugs.

@boeddeker does your test pass on matplotlib 2.2? I think the best thing to do is get pandas passing on matplotlib 2.2 (#19702) and skip this test on MPL older than 2.2.

Could you also post images of the output on MPL 2.1 and MPL 2.2?

@boeddeker
Copy link
Author

I haven't installed matplotlib 2.2 because my cases work. When Version 2.2 is released, I will investigate how there categorical plot work and further work on this PR.

@jreback
Copy link
Contributor

jreback commented Jul 7, 2018

@TomAugspurger should we fix / close this?

@TomAugspurger
Copy link
Contributor

We should get this passing and merged.

@boeddeker can you track down why the CI tests failed?

@boeddeker
Copy link
Author

@TomAugspurger Currently I am busy, but in two weeks I have time. I will take care of this PR in two weeks.

@jreback
Copy link
Contributor

jreback commented Oct 11, 2018

closing as stale, if you want to continue working, pls ping and we can re-open. you will need to merge master.

@jreback jreback closed this Oct 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: plot two lines, unordered xlabels with type str
5 participants