8000 Categorical support for NumPy string arrays. by QuLogic · Pull Request #7241 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Categorical support for NumPy string arrays. #7241

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

Merged
merged 3 commits into from
Oct 14, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions lib/matplotlib/tests/test_category.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,65 @@ def test_plot_1d_missing(self):

self.axis_test(ax.yaxis, self.dmticks, self.dmlabels, self.dmunit_data)

@cleanup
@pytest.mark.usefixtures("data")
def test_plot_bytes(self):
counts = np.array([4, 6, 5, 1])
fig, ax = plt.subplots(ncols=3)

ax[0].bar(self.d, counts)

types = [v.encode('ascii') for v in self.d]
Copy link
Member

Choose a reason for hiding this comment

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

kinda confused by this since self.d is ['a', 'c', 'c', 'd']...what's the point of the reencoding

Copy link
Member Author

Choose a reason for hiding this comment

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

Those are strings; this tests bytes.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, maybe this should just be explicit then? [b'a', b'c', b'c', b'd'] I think I'm advocating against using the fixture since it's not really used here.

ax[1].bar(types, counts)

types = np.array(types)
ax[2].bar(types, counts)

fig.canvas.draw()

# All three plots should look like the string one.
self.axis_test(ax[1].xaxis,
ax[0].xaxis.get_majorticklocs(),
lt(ax[0].xaxis.get_majorticklabels()),
ax[0].xaxis.unit_data)
self.axis_test(ax[2].xaxis,
ax[0].xaxis.get_majorticklocs(),
lt(ax[0].xaxis.get_majorticklabels()),
ax[0].xaxis.unit_data)

@cleanup
def test_plot_numlike(self):
Copy link
Member

Choose a reason for hiding this comment

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

I prefer paramterized (or subtests) to subplots so that it's easier to isolate which exact case failed

Copy link
Member Author

Choose a reason for hiding this comment

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

I went with subplots so I could pull the correct values from the string version (which should be working based on other tests); I didn't really know what the tests were doing initially, so I wasn't sure what the correct values were in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, so the way to do that if you don't want to write the fixed values (though the expected values is probably better testing practice*) is probably to just always create an axis 0 and then parameterize out axis 1 and axis 2.

  • ticklocs is the fixed assignment, in this case with 3 values ['a', 'c', 'c', 'd'] becomes ['a;, 'c', 'd'] since they're analogous to x values, the ticklocs are [0, 1, 2] and the labels are ['a', 'c', 'd'], which also creates a mismatch between the x and counts and so ['a', 'b', 'c', 'd'] should probably be used explicitely.

counts = np.array([4, 6, 5, 1])
Copy link
Member

Choose a reason for hiding this comment

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

What does 4 counts for 3 bins do?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe I should have named it bars instead of bins... The bars just overlap each other. Actually, it's probably not necessary; I just used the categories from the original issue.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, if it works...but yeah might be better to test the simplest case here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I just noticed this is similar to the data fixture, where the last element is a again.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but that was for plot where I think it's more important to test repeating x. I dunno if that inherently makes sense with bar, but it likely doesn't matter much either way.

fig, ax = plt.subplots(ncols=4)

types = ['1', '11', '3', '1']
ax[0].bar(types, counts)

types = np.array(types)
ax[1].bar(types, counts)

types = [b'1', b'11', b'3', b'1']
ax[2].bar(types, counts)

types = np.array(types)
ax[3].bar(types, counts)

fig.canvas.draw()

# All four plots should look like the string one.
self.axis_test(ax[1].xaxis,
Copy link
Member
@story645 story645 Oct 9, 2016

Choose a reason for hiding this comment

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

same as above, the expected ticklocs and ticklabels are [0, 1, 2, 3] and ['1', '11', '3', '1'] respectively, The axis unit data should be mocked, as seen in line 141.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it really go up to 3, when the '1' is repeated?

Copy link
Member

Choose a reason for hiding this comment

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

Oh sorry, didn't notice that the 1 was repeated. Then it's [0, 1, 2, 0]

ax[0].xaxis.get_majorticklocs(),
lt(ax[0].xaxis.get_majorticklabels()),
ax[0].xaxis.unit_data)
self.axis_test(ax[2].xaxis,
ax[0].xaxis.get_majorticklocs(),
lt(ax[0].xaxis.get_majorticklabels()),
ax[0].xaxis.unit_data)
self.axis_test(ax[3].xaxis,
ax[0].xaxis.get_majorticklocs(),
lt(ax[0].xaxis.get_majorticklabels()),
ax[0].xaxis.unit_data)

@cleanup
@pytest.mark.usefixtures("data", "missing_data")
def test_plot_2d(self):
Expand Down
0