diff --git a/kwant/tests/test_plotter.py b/kwant/tests/test_plotter.py
index 4bf6a510ac41f4937bec8275330ee51a6af3383f..fa8cd5a9ff75e1afb780ac8bfc7dab6cf7c86dc1 100644
--- a/kwant/tests/test_plotter.py
+++ b/kwant/tests/test_plotter.py
@@ -31,10 +31,10 @@ def test_importable_without_matplotlib():
         suffix = 'py'
     assert suffix == 'py'
     fname = sep.join((prefix, suffix))
-    with open(fname) as f:
+    with open(fname, 'rb') as f:
         code = f.read()
-    code = code.replace('from . import', 'from kwant import')
-    code = code.replace('matplotlib', 'totalblimp')
+    code = code.replace(b'from . import', b'from kwant import')
+    code = code.replace(b'matplotlib', b'totalblimp')
 
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")