janpipek / physt

Python histogram library - histograms as updateable, fully semantic objects with visualization tools. [P]ython [HYST]ograms.
MIT License
129 stars 15 forks source link

python 2.7 plotting is not working #34

Closed romange closed 7 years ago

romange commented 7 years ago

When runnin plot() function I get the error below even though matplotlib is installed. Also the algorithm is pretty slow when running on something bigger than toy example.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 137, in __call__
    return plot(self.histogram, kind=kind, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 91, in plot
    backend_name, backend = _get_backend(backend)
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 70, in _get_backend
    raise RuntimeError("No plotting backend available. Please, install matplotlib (preferred) or bokeh (limited).")
RuntimeError: No plotting backend available. Please, install matplotlib (preferred) or bokeh (limited).
janpipek commented 7 years ago

Hi romange,

thanks for reporting. Actually, you are reporting two issues, so I will split this into:

janpipek commented 7 years ago

Now, back to the plotting. The problem of Python 2.7 is that I don't use it regularly and although I am on purpose not using any Python3-specific features, it's difficult for me to check everything. A couple of minutes ago, I posted a new version (0.3.28.1) that corrects some misbehaviour with 2.7.

However, I don't see (even in the old version) any problem with importing matplotlib. Please, can you check this for me:

from physt.plotting import matplotlib as mpl_backend

Or perhaps:

from physt.examples import normal_h1
normal_h1().plot()

Does either report any problem? Thanks, Jan

romange commented 7 years ago

Hi Jan,

I updated the package to the latest but the problem persists (ubuntu 16.04):

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from physt.examples import normal_h1
>>> normal_h1().plot()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 137, in __call__
    return plot(self.histogram, kind=kind, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 91, in plot
    backend_name, backend = _get_backend(backend)
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/__init__.py", line 70, in _get_backend
    raise RuntimeError("No plotting backend available. Please, install matplotlib (preferred) or bokeh (limited).")
RuntimeError: No plotting backend available. Please, install matplotlib (preferred) or bokeh (limited).
romange commented 7 years ago

What's this python-tk package?

>>> from physt.plotting import matplotlib as mpl_backend
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/physt/plotting/matplotlib.py", line 31, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package
romange commented 7 years ago

Ok, installing python-tk solves the problem.

romange commented 7 years ago

However it still does not work :( I run the script below with the following input:

echo -e "2 5\n 3 4" | ./hist.py

#!/usr/bin/env python

import sys, os
import numpy as np
from physt import h1

def main(argv):
    a = np.loadtxt(sys.stdin, dtype=np.int)
    print a
    h = h1(None, "fixed_width", bin_width=10, name="Histogram", adaptive=True)
    for cnt, val in a:
        h.fill(val, cnt)
    print "finished"

    h.plot()

if __name__ == "__main__":
    main(sys.argv[1:])
janpipek commented 7 years ago

You mean, nothing is showed? Ok, here, it's not exactly clear what physt should do. Therefore, it adopts the behaviour of matplotlib.

The plot() creates the graphical representation of the histogram (more specifically, the Axes object). Depending on the final use, you may want to be displayed immediately, or you may want to play more with the figure and display later. Perhaps you just want to save the plot as an image. In such case, nothing gets displayed at all. This is exactly what matplotlib does (in the end, physt just calls it), well described here: https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.show .

In short, for console use (in IPython notebooks, this is somewhat different), you typically have to call the show function:

import matplotlib.pyplot as plt
...
h.plot()
# Some more tweeks to the plot?
plt.show()

I am considering to have some kind of show argument for the plot() methods, but perhaps it may only cause more confusion for users that are used to matplotlib behaviour.

Is this explanation ok for you?

romange commented 7 years ago

Thanks Jan. I am not familiar with maplotlib, in fact having a textual output would suffice for me. For example something like the table below. In any case I suggest to improve your README to include something about python-tk and to have an example that includes .show() as well .

Count: 120 Average: 2736.4167  StdDev: 2960.54
Min: 0.0000  Median: 1533.3333  Max: 9801.0000
------------------------------------------------------
[       0,       1 ) 1   0.833%   0.833% 
[       1,       2 ) 21  17.500%  18.333% ####
[       4,       5 ) 1   0.833%  19.167% 
[       9,      10 ) 1   0.833%  20.000% 
[      16,      18 ) 1   0.833%  20.833% 
[      25,      30 ) 1   0.833%  21.667% 
[      35,      40 ) 1   0.833%  22.500% 
[      45,      50 ) 1   0.833%  23.333% 
[      60,      70 ) 1   0.833%  24.167% 
[      80,      90 ) 1   0.833%  25.000% 
[     100,     120 ) 1   0.833%  25.833% 
[     120,     140 ) 1   0.833%  26.667% 
[     140,     160 ) 1   0.833%  27.500% 
[     160,     180 ) 1   0.833%  28.333% 
[     180,     200 ) 1   0.833%  29.167% 
[     200,     250 ) 1   0.833%  30.000% 
[     250,     300 ) 2   1.667%  31.667% 
[     300,     350 ) 1   0.833%  32.500% 
[     350,     400 ) 1   0.833%  33.333% 
[     400,     450 ) 2   1.667%  35.000% 
[     450,     500 ) 1   0.833%  35.833% 
[     500,     600 ) 2   1.667%  37.500% 
[     600,     700 ) 2   1.667%  39.167% 
[     700,     800 ) 2   1.667%  40.833% 
[     800,     900 ) 1   0.833%  41.667% 
[     900,    1000 ) 2   1.667%  43.333% 
[    1000,    1200 ) 3   2.500%  45.833% #
[    1200,    1400 ) 3   2.500%  48.333% #
[    1400,    1600 ) 2   1.667%  50.000% 
[    1600,    1800 ) 3   2.500%  52.500% #
[    1800,    2000 ) 2   1.667%  54.167% 
[    2000,    2500 ) 5   4.167%  58.333% #
[    2500,    3000 ) 5   4.167%  62.500% #
[    3000,    3500 ) 5   4.167%  66.667% #
[    3500,    4000 ) 4   3.333%  70.000% #
[    4000,    4500 ) 4   3.333%  73.333% #
[    4500,    5000 ) 3   2.500%  75.833% #
[    5000,    6000 ) 7   5.833%  81.667% #
[    6000,    7000 ) 6   5.000%  86.667% #
[    7000,    8000 ) 6   5.000%  91.667% #
[    8000,    9000 ) 5   4.167%  95.833% #
[    9000,   10000 ) 5   4.167% 100.000% #
janpipek commented 7 years ago

Is this is textual output from some standard statistics tool (i.e. its name?)? If yes, I might introduce something similar.

romange commented 7 years ago

It's not a standard tool but here is the code of this adaptive histogram. It's not very advanced but personally for me it served as a go-to tool for 99% of the cases.

https://github.com/google/leveldb/blob/master/util/histogram.h

On Sun, Apr 16, 2017 at 10:43 AM, Jan Pipek notifications@github.com wrote:

Is this is textual output from some standard statistics tool (i.e. its name?)? If yes, I might introduce something similar.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/janpipek/physt/issues/34#issuecomment-294338637, or mute the thread https://github.com/notifications/unsubscribe-auth/ADgSiPL_na7WZ7RCw9NHfGFlXG8topTJks5rwcamgaJpZM4M-UjF .

-- Best regards, Roman

janpipek commented 7 years ago

Actually, if you have pandas library (http://pandas.pydata.org) installed, you may export the histogram to a DataFrame (by adding the following line to your script):

print(h.to_dataframe())

However, the purpose of the to_dataframe method is not to provide textual / human-friendly output, so it might not be what you want. A proper ASCII table+plot would be nice to have, I agree.

janpipek commented 7 years ago

I am putting that aside for future development: #37

romange commented 7 years ago

Thanks! And I will check to_dataframe method.

On Sun, Apr 16, 2017 at 11:26 AM, Jan Pipek notifications@github.com wrote:

I am putting that aside for future development: #37 https://github.com/janpipek/physt/issues/37

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/janpipek/physt/issues/34#issuecomment-294340144, or mute the thread https://github.com/notifications/unsubscribe-auth/ADgSiEJ0FSDinEgRG9YrC51JV9FEXLOTks5rwdDJgaJpZM4M-UjF .

-- Best regards, Roman