pybinding cannot import name allow_rasterization

1.6k Views Asked by At

I just install pybinding and I'm trying to run the first example that is proposed in the documentation of this library.

import pybinding as pb
import numpy as np
import matplotlib.pyplot as plt
import pybinding as pb

d = 0.2  # [nm] unit cell length
t = 1    # [eV] hopping energy

# create a simple 2D lattice with vectors a1 and a2
lattice = pb.Lattice(a1=[d, 0], a2=[0, d])
lattice.add_sublattices(
    ('A', [0, 0])  # add an atom called 'A' at position [0, 0]
)
lattice.add_hoppings(
    # (relative_index, from_sublattice, to_sublattice, energy)
    ([0, 1], 'A', 'A', t),
    ([1, 0], 'A', 'A', t)
)

lattice.plot()
plt.show() 

I have already installed what is required in the documentation (for Windows OS) and the sicrpt run pretty well until it has to do the lattice.plot() throwing the following error

Traceback (most recent call last):
  File "prueba.py", line 25, in <module>
    lattice.plot()
  File "C:\xampp7\Python\lib\site-packages\pybinding\lattice.py", line 463, in plot
    axes=axes))
  File "C:\xampp7\Python\lib\site-packages\pybinding\results.py", line 598, in plot
    plot_sites(self.positions, self.sublattices, **props['site'])
  File "C:\xampp7\Python\lib\site-packages\pybinding\system.py", line 285, in plot_sites
    from pybinding.support.collections import CircleCollection
  File "C:\xampp7\Python\lib\site-packages\pybinding\support\collections.py", line 2, in <module>
    from matplotlib.collections import Collection, allow_rasterization
ImportError: cannot import name 'allow_rasterization' 

I have already check and matplotlib is correctly installed (I try some plots recommended in the documentation of matplotlib and worked pretty well). Also a looked for the file collections.py in the pybinding library and the mistake is in the second line

import numpy as np
from matplotlib.collections import Collection, allow_rasterization

And looking at collections.py of the matplolib and searching for 'allow_rasterization' I found the duplicated 6 time the following function

@artist.allow_rasterization
def draw(self, renderer):

I am pretty new at python so I'm don't know if I'm looking at what I should. Thanks in advance

4

There are 4 best solutions below

0
On BEST ANSWER

I uninstalled version 2.2.2 of matplotlib and installed version 1.1.2 of marplotlib. I am now able to do put.show()

2
On

go into the file "C:\xampp7\Python\lib\site-packages\pybinding\support\collections.py" and modify the command line as: "from matplotlib.collections import Collection#, allow_rasterization from matplotlib.artist import allow_rasterization"

0
On

Go into the directory: ~/miniconda3/lib/python3.7/site-packages/pybinding/support

In the file: collections.py change line 2:

from matplotlib.collections import Collection, allow_rasterization

to

from matplotlib.collections import Collection 
from matplotlib.artist import allow_rasterization
1
On

I got the same issue. I'm using Linux and the package pybinding are installed on

/usr/lib/python3.6/site-packages/pybinding/support/

You can correct the error changing the import modules of allow_rasterization module, changing the file colletion.py

/usr/lib/python3.6/site-packages/pybinding/support/colletion.py

the firsts lines from

import numpy as np
from matplotlib.collections import Collection, allow_rasterization

to

import numpy as np
from matplotlib.collections import Collection
from matplotlib.artist import allow_rasterization

You can correct the plot issue related to previous versions on matplotlib 1.1.2, because now the pybinding packages use matplotlib 2.2.2.