MCPcopy
hub / github.com/nltk/nltk / plot

Method plot

nltk/probability.py:247–309  ·  view source on GitHub ↗

Plot samples from the frequency distribution displaying the most frequent sample first. If an integer parameter is supplied, stop after this many samples have been plotted. For a cumulative plot, specify cumulative=True. Additional ``**kwargs`` are passed t

(
        self, *args, title="", cumulative=False, percents=False, show=False, **kwargs
    )

Source from the content-addressed store, hash-verified

245 return self.most_common(1)[0][0]
246
247 def plot(
248 self, *args, title="", cumulative=False, percents=False, show=False, **kwargs
249 ):
250 """
251 Plot samples from the frequency distribution
252 displaying the most frequent sample first. If an integer
253 parameter is supplied, stop after this many samples have been
254 plotted. For a cumulative plot, specify cumulative=True. Additional
255 ``**kwargs`` are passed to matplotlib's plot function.
256 (Requires Matplotlib to be installed.)
257
258 :param title: The title for the graph.
259 :type title: str
260 :param cumulative: Whether the plot is cumulative. (default = False)
261 :type cumulative: bool
262 :param percents: Whether the plot uses percents instead of counts. (default = False)
263 :type percents: bool
264 :param show: Whether to show the plot, or only return the ax.
265 :type show: bool
266 """
267 try:
268 import matplotlib.pyplot as plt
269 except ImportError as e:
270 raise ValueError(
271 "The plot function requires matplotlib to be installed."
272 "See https://matplotlib.org/"
273 ) from e
274
275 if len(args) == 0:
276 args = [len(self)]
277 samples = [item for item, _ in self.most_common(*args)]
278
279 if cumulative:
280 freqs = list(self._cumulative_frequencies(samples))
281 ylabel = "Cumulative "
282 else:
283 freqs = [self[sample] for sample in samples]
284 ylabel = ""
285
286 if percents:
287 freqs = [f / self.N() * 100 for f in freqs]
288 ylabel += "Percents"
289 else:
290 ylabel += "Counts"
291
292 ax = plt.gca()
293 ax.grid(True, color="silver")
294
295 if "linewidth" not in kwargs:
296 kwargs["linewidth"] = 2
297 if title:
298 ax.set_title(title)
299
300 ax.plot(freqs, **kwargs)
301 ax.set_xticks(range(len(samples)))
302 ax.set_xticklabels([str(s) for s in samples], rotation=90)
303 ax.set_xlabel("Samples")
304 ax.set_ylabel(ylabel)

Callers 1

plotMethod · 0.45

Calls 4

NMethod · 0.95
gridMethod · 0.45
showMethod · 0.45

Tested by

no test coverage detected