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

Cfd plot fix #2323

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS.md
Expand Up @@ -253,7 +253,7 @@
- Viresh Gupta <https://github.com/virresh>
- Ondřej Cífka <https://github.com/cifkao>
- Iris X. Zhou <https://github.com/irisxzhou>

- Josh Bell <https://github.com/jbbe>

## Others whose work we've taken and included in NLTK, but who didn't directly contribute it:
### Contributors to the Porter Stemmer
Expand Down
15 changes: 7 additions & 8 deletions nltk/probability.py
Expand Up @@ -1894,7 +1894,7 @@ def plot(self, *args, **kwargs):
"""
Plot the given samples from the conditional frequency distribution.
For a cumulative plot, specify cumulative=True.
(Requires Matplotlib to be installed.)
(Requires matplotlib to be installed.)

:param samples: The samples to plot
:type samples: list
Expand All @@ -1904,25 +1904,24 @@ def plot(self, *args, **kwargs):
:type conditions: list
"""
try:
from matplotlib import plt
import matplotlib.pyplot as plt
except ImportError:
raise ValueError(
'The plot function requires matplotlib to be installed.'
'See http://matplotlib.org/'
)

cumulative = _get_kwarg(kwargs, 'cumulative', False)
percents = _get_kwarg(kwargs, 'percents', False)
conditions = _get_kwarg(kwargs, 'conditions', sorted(self.conditions()))
title = _get_kwarg(kwargs, 'title', '')
samples = _get_kwarg(
kwargs, 'samples', sorted(set(v for c in conditions
if v in self
for v in self[c]))
) # this computation could be wasted
for v in self[c]))
) # this computation could be wasted
if "linewidth" not in kwargs:
kwargs["linewidth"] = 2

ax = plt.gca()
for condition in conditions:
if cumulative:
freqs = list(self[condition]._cumulative_frequencies(samples))
Expand All @@ -1942,9 +1941,9 @@ def plot(self, *args, **kwargs):

ax.legend(loc=legend_loc)
ax.grid(True, color="silver")
ax.set_xticks(range(len(samples)), [text_type(s) for s in samples], rotation=90)
plt.xticks(range(len(samples)), [text_type(s) for s in samples])
if title:
ax.set_title(title)
ax.title(title)
ax.set_xlabel("Samples")
ax.set_ylabel(ylabel)
plt.show()
Expand Down