powerlaw icon indicating copy to clipboard operation
powerlaw copied to clipboard

Can not pass 'bins' keyword to `plot_pdf`

Open henrymartin1 opened this issue 2 years ago • 2 comments

Hi, I am trying to use the Fit().plot_pdf function with custom bins by passing the bins keyword. However this leads to an error (see error message below). I have investigated the error and I think the problem is in the namespace management. The bins argument gets passed correctly down to the function pdf and gets read there correctly (line 1972. However, popping the keyword in within the function pdf does not delete it from the kwargs dictionary in the outer function plot_pdf that calls pdf in line 2083. Therefore the dictionary kwargs containing bins keyword gets passed on to matplotlib in line 2092 which causes the error. I don't know what would be the most elegant solution but as the function pdf only accepts the bins keyword, you could pop the keyword outside in the plot_pdf function.

import numpy as np
import powerlaw
import matplotlib.pyplot as plt

data = 1./np.random.power(4., 1000)

fit = powerlaw.Fit(data)
plt.figure()
fit.plot_pdf(marker="*")

bins = 5
fit.plot_pdf(marker="*", bins=bins)

This creates the following error message:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-30-a95eeb74b4b5>", line 2, in <module>
    fit.plot_pdf(marker="*", bins=bins)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\powerlaw.py", line 525, in plot_pdf
    return plot_pdf(data, ax=ax, linear_bins=linear_bins, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\powerlaw.py", line 2079, in plot_pdf
    plt.plot(bin_centers, hist, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot
    return gca().plot(
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_base.py", line 539, in _plot_args
    return [l[0] for l in result]
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_base.py", line 539, in <listcomp>
    return [l[0] for l in result]
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_base.py", line 532, in <genexpr>
    result = (make_artist(x[:, j % ncx], y[:, j % ncy], kw,
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes\_base.py", line 354, in _makeline
    seg = mlines.Line2D(x, y, **kw)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\lines.py", line 397, in __init__
    self.update(kwargs)
  File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\artist.py", line 1062, in update
    raise AttributeError(f"{type(self).__name__!r} object "
AttributeError: 'Line2D' object has no property 'bins'

henrymartin1 avatar Aug 19 '22 09:08 henrymartin1

Part of the issue here is that bins was not an advertised feature of pdf (or plot_pdf) to begin with; it's not docstring, for example. I'm hesitant to enable/call attention/maintain that functionality, since bin manipulation is perhaps the #1 way that people lie to themselves and others when evaluating the shape of distributions. With that said, if there is user demand, I'll support putting it in. Could you edit your pull request with an updated docstring and doublechecking if there are any other places that bins should be referenced in docstrings or elsewhere? Thanks!

jeffalstott avatar Aug 29 '22 14:08 jeffalstott

Thanks for using powerlaw! Sorry there's an issue. If you submit a backwards-compatible pull request I'll definitely integrate it!

On Fri, Aug 19, 2022 at 2:29 AM Henry Martin @.***> wrote:

Hi, I am trying to use the Fit().plot_pdf function with custom bins by passing the bins keyword. However this leads to an error (see error message below). I have investigated the error and I think the problem is in the namespace management. The bins argument gets passed correctly down to the function pdf and gets read there correctly (line 1972 https://github.com/jeffalstott/powerlaw/blob/6732699d790edbe27c2790bf22c3ef7355d2b07e/powerlaw.py#L1972. However, popping the keyword in within the function pdf does not delete it from the kwargs dictionary in the outer function plot_pdf that calls pdf in line 2083 https://github.com/jeffalstott/powerlaw/blob/6732699d790edbe27c2790bf22c3ef7355d2b07e/powerlaw.py#L2083. Therefore the dictionary kwargs containing bins keyword gets passed on to matplotlib in line 2092 https://github.com/jeffalstott/powerlaw/blob/6732699d790edbe27c2790bf22c3ef7355d2b07e/powerlaw.py#L2092 which causes the error. I don't know what would be the most elegant solution but as the function pdf only accepts the bins keyword, you could pop the keyword outside in the plot_pdf function.

import numpy as npimport powerlawimport matplotlib.pyplot as plt data = 1./np.random.power(4., 1000) fit = powerlaw.Fit(data)plt.figure()fit.plot_pdf(marker="") bins = 5fit.plot_pdf(marker="", bins=bins)

This creates the following error message:

Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 2, in fit.plot_pdf(marker="*", bins=bins) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\powerlaw.py", line 525, in plot_pdf return plot_pdf(data, ax=ax, linear_bins=linear_bins, **kwargs) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\powerlaw.py", line 2079, in plot_pdf plt.plot(bin_centers, hist, **kwargs) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot return gca().plot( File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_axes.py", line 1605, in plot lines = [*self._get_lines(*args, data=data, **kwargs)] File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_base.py", line 315, in call yield from self._plot_args(this, kwargs) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_base.py", line 539, in _plot_args return [l[0] for l in result] File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_base.py", line 539, in return [l[0] for l in result] File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_base.py", line 532, in result = (make_artist(x[:, j % ncx], y[:, j % ncy], kw, File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\axes_base.py", line 354, in _makeline seg = mlines.Line2D(x, y, **kw) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\lines.py", line 397, in init self.update(kwargs) File "C:\ProgramData\Anaconda3\envs\graphentropy\lib\site-packages\matplotlib\artist.py", line 1062, in update raise AttributeError(f"{type(self).name!r} object " AttributeError: 'Line2D' object has no property 'bins'

— Reply to this email directly, view it on GitHub https://github.com/jeffalstott/powerlaw/issues/99, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHL7L4SFKST65ZKH6MVDL3VZ5HXBANCNFSM57ACIGSA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jeffalstott avatar Oct 11 '22 09:10 jeffalstott