Repository: Qiskit/qiskit

Issue:
Matplotlib circuit barrier and long name tests cannot be satisfied on OSX
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: master
- **Python version**:
- **Operating system**: OSX

### What is the current behavior?
The tests `test_long_name` and `test_plot_barriers` can never pass locally on OSX.  I can update the reference images to those returned by my setup, but the tests still fail saying that the images are different by >0.1%.  I have no idea why this is since they should be exactly the same.

This was blocking #2505, until I skipped the tests given that I can never satisfy them.

![matplotlib_barriers_ref](https://user-images.githubusercontent.com/1249193/61738277-f3780580-ad57-11e9-8b0c-d55c7fe7859a.png)

![matplotlib_long_name_ref](https://user-images.githubusercontent.com/1249193/61738262-eeb35180-ad57-11e9-95f1-2eccc69b599e.png)




### Steps to reproduce the problem



### What is the expected behavior?



### Suggested solutions





Discussion:
I think this is an issue with the python version. It seems to pass with 3.5 but fail with 3.6 for me running on OSX. This is an issue we seem to run into a lot around different python versions adding very slightly different amounts of whitespace around images. A possible solution would be to skip the tests based on the python version, I don't think this is ideal but it is preferential to always skipping them.

Resolving patch:
diff --git a/qiskit/visualization/gate_map.py b/qiskit/visualization/gate_map.py
--- a/qiskit/visualization/gate_map.py
+++ b/qiskit/visualization/gate_map.py
@@ -160,7 +160,7 @@ def plot_gate_map(backend, figsize=None,
     if ax is None:
         fig, ax = plt.subplots(figsize=figsize)  # pylint: disable=invalid-name
         ax.axis('off')
-        fig.tight_layout()
+        fig.set_tight_layout(True)
 
     # set coloring
     if qubit_color is None:


Test patch:
diff --git a/test/python/visualization/references/20_plot_circuit_layout.png b/test/python/visualization/references/20_plot_circuit_layout.png
Binary files a/test/python/visualization/references/20_plot_circuit_layout.png and b/test/python/visualization/references/20_plot_circuit_layout.png differ
diff --git a/test/python/visualization/references/matplotlib_barriers_ref.png b/test/python/visualization/references/matplotlib_barriers_ref.png
Binary files a/test/python/visualization/references/matplotlib_barriers_ref.png and b/test/python/visualization/references/matplotlib_barriers_ref.png differ
diff --git a/test/python/visualization/references/matplotlib_conditional_ref.png b/test/python/visualization/references/matplotlib_conditional_ref.png
Binary files a/test/python/visualization/references/matplotlib_conditional_ref.png and b/test/python/visualization/references/matplotlib_conditional_ref.png differ
diff --git a/test/python/visualization/references/matplotlib_long_name_ref.png b/test/python/visualization/references/matplotlib_long_name_ref.png
deleted file mode 100644
Binary files a/test/python/visualization/references/matplotlib_long_name_ref.png and /dev/null differ
diff --git a/test/python/visualization/references/nt_long_name_matplotlib.png b/test/python/visualization/references/nt_long_name_matplotlib.png
new file mode 100644
Binary files /dev/null and b/test/python/visualization/references/nt_long_name_matplotlib.png differ
diff --git a/test/python/visualization/references/posix_long_name_matplotlib.png b/test/python/visualization/references/posix_long_name_matplotlib.png
new file mode 100644
Binary files /dev/null and b/test/python/visualization/references/posix_long_name_matplotlib.png differ
diff --git a/test/python/visualization/test_circuit_matplotlib_drawer.py b/test/python/visualization/test_circuit_matplotlib_drawer.py
--- a/test/python/visualization/test_circuit_matplotlib_drawer.py
+++ b/test/python/visualization/test_circuit_matplotlib_drawer.py
@@ -48,8 +48,7 @@ def _expected_empty(self):
         expected.set_size_inches(2.508333333333333, 0.2508333333333333)
         return expected
 
-    @unittest.skipIf(not visualization.HAS_MATPLOTLIB,
-                     'matplotlib not available.')
+    @unittest.skipIf(not visualization.HAS_MATPLOTLIB, 'matplotlib not available.')
     def test_empty_circuit(self):
         qc = QuantumCircuit()
         filename = self._get_resource_path('current_pulse_matplotlib_ref.png')
@@ -63,9 +62,7 @@ def test_empty_circuit(self):
 
         self.assertImagesAreEqual(filename, expected_filename)
 
-    @unittest.skipIf(not visualization.HAS_MATPLOTLIB,
-                     'matplotlib not available.')
-    @unittest.skip('Test is unreliable on some platforms')
+    @unittest.skipIf(not visualization.HAS_MATPLOTLIB, 'matplotlib not available.')
     def test_plot_barriers(self):
         """Test to see that plotting barriers works.
         If it is set to False, no blank columns are introduced"""
@@ -117,8 +114,6 @@ def test_plot_barriers(self):
 
     @unittest.skipIf(not visualization.HAS_MATPLOTLIB,
                      'matplotlib not available.')
-    @unittest.skip('Test is unreliable on some platforms')
-    @unittest.skipIf(os.name == 'nt', 'Rendered image differs on windows')
     def test_long_name(self):
         """Test to see that long register names can be seen completely
         As reported in #2605
@@ -135,20 +130,17 @@ def test_long_name(self):
         circuit.h(qr)
         circuit.h(qr)
 
-        long_name_filename = self._get_resource_path('current_long_name_matplotlib_ref.png')
-        visualization.circuit_drawer(circuit, output='mpl',
-                                     filename=long_name_filename)
-        self.addCleanup(os.remove, long_name_filename)
+        filename = self._get_resource_path('current_%s_long_name_matplotlib.png' % os.name)
+        visualization.circuit_drawer(circuit, output='mpl', filename=filename)
+        # self.addCleanup(os.remove, filename)
 
         ref_filename = self._get_resource_path(
-            'visualization/references/matplotlib_long_name_ref.png')
+            'visualization/references/%s_long_name_matplotlib.png' % os.name)
 
-        self.assertImagesAreEqual(ref_filename, long_name_filename)
+        self.assertImagesAreEqual(ref_filename, filename)
 
     @unittest.skipIf(not visualization.HAS_MATPLOTLIB,
                      'matplotlib not available.')
-    @unittest.skip('Test is unreliable on some platforms')
-    @unittest.skipIf(os.name == 'nt', 'Rendered image differs on windows')
     def test_conditional(self):
         """Test that circuits with conditionals draw correctly
         """
diff --git a/test/python/visualization/test_gate_map.py b/test/python/visualization/test_gate_map.py
--- a/test/python/visualization/test_gate_map.py
+++ b/test/python/visualization/test_gate_map.py
@@ -15,15 +15,19 @@
 """A test for visualizing device coupling maps"""
 import unittest
 import os
-import matplotlib.pyplot as plt
+
 from ddt import ddt, data
 from qiskit.test.mock import FakeProvider
 from qiskit.test import QiskitTestCase
 from qiskit.visualization.gate_map import _GraphDist, plot_gate_map, plot_circuit_layout
+from qiskit.tools.visualization import HAS_MATPLOTLIB
 from qiskit import QuantumRegister, QuantumCircuit
 from qiskit.transpiler import Layout
 from .visualization import path_to_diagram_reference, QiskitVisualizationTestCase
 
+if HAS_MATPLOTLIB:
+    import matplotlib.pyplot as plt
+
 
 @ddt
 class TestGateMap(QiskitVisualizationTestCase):
@@ -32,18 +36,20 @@ class TestGateMap(QiskitVisualizationTestCase):
                            not (x.configuration().simulator or x.configuration().n_qubits == 2),
                            FakeProvider().backends()))
 
-    def test_plot_gate_map(self):
+    @data(*backends)
+    @unittest.skipIf(not HAS_MATPLOTLIB, 'matplotlib not available.')
+    def test_plot_gate_map(self, backend):
         """ tests plotting of gate map of a device (20 qubit, 16 qubit, 14 qubit and 5 qubit)"""
-        for backend in self.backends:
-            n = backend.configuration().n_qubits
-            img_ref = path_to_diagram_reference(str(n) + "bit_quantum_computer.png")
-            filename = "temp.png"
-            fig = plot_gate_map(backend)
-            fig.savefig(filename, bbox_inches="tight")
-            self.assertImagesAreEqual(filename, img_ref, 0.2)
-            os.remove(filename)
+        n = backend.configuration().n_qubits
+        img_ref = path_to_diagram_reference(str(n) + "bit_quantum_computer.png")
+        filename = "temp.png"
+        fig = plot_gate_map(backend)
+        fig.savefig(filename)
+        self.assertImagesAreEqual(filename, img_ref, 0.2)
+        os.remove(filename)
 
     @data(*backends)
+    @unittest.skipIf(not HAS_MATPLOTLIB, 'matplotlib not available.')
     def test_plot_circuit_layout(self, backend):
         """ tests plot_circuit_layout for each device"""
         layout_length = int(backend._configuration.n_qubits / 2)
@@ -54,11 +60,12 @@ def test_plot_circuit_layout(self, backend):
         img_ref = path_to_diagram_reference(str(n) + "_plot_circuit_layout.png")
         filename = str(n) + "_plot_circuit_layout_result.png"
         fig = plot_circuit_layout(circuit, backend)
-        fig.savefig(filename, bbox_inches="tight")
+        fig.savefig(filename)
         self.assertImagesAreEqual(filename, img_ref, 0.1)
         os.remove(filename)
 
 
+@unittest.skipIf(not HAS_MATPLOTLIB, 'matplotlib not available.')
 class TestGraphDist(QiskitTestCase):
     """ tests _GraphdDist functions """
 
diff --git a/test/python/visualization/visualization.py b/test/python/visualization/visualization.py
--- a/test/python/visualization/visualization.py
+++ b/test/python/visualization/visualization.py
@@ -19,9 +19,12 @@
 
 import os
 import unittest
+import matplotlib
 
 from qiskit.test import QiskitTestCase
 
+matplotlib.use('ps')
+
 
 def path_to_diagram_reference(filename):
     return os.path.join(_this_directory(), 'references', filename)