Python not showing the [matplotlib] window with openCV results
Question: I'm working on OpenCV 2.4.9 . Below is my code -

    
    
    import cv2
    import numpy as np
    from   matplotlib import pyplot as plt
    
    BLUE = [255,0,0]
    
    img1 = cv2.imread( 'sachin.png' )
    
    replicate  = cv2.copyMakeBorder( img1, 10, 10, 10, 10, cv2.BORDER_REPLICATE )
    reflect    = cv2.copyMakeBorder( img1, 10, 10, 10, 10, cv2.BORDER_REFLECT )
    reflect101 = cv2.copyMakeBorder( img1, 10, 10, 10, 10, cv2.BORDER_REFLECT_101 )
    wrap       = cv2.copyMakeBorder( img1, 10, 10, 10, 10, cv2.BORDER_WRAP )
    constant   = cv2.copyMakeBorder( img1, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value = BLUE )
    
    plt.subplot(231), plt.imshow( img1,       'gray' ), plt.title( 'ORIGINAL' )
    plt.subplot(232), plt.imshow( replicate,  'gray' ), plt.title( 'REPLICATE')
    plt.subplot(233), plt.imshow( reflect,    'gray' ), plt.title( 'REFLECT' )
    plt.subplot(234), plt.imshow( reflect101, 'gray' ), plt.title( 'REFLECT_101' )
    plt.subplot(235), plt.imshow( wrap,       'gray' ), plt.title( 'WRAP' )
    plt.subplot(236), plt.imshow( constant,   'gray' ), plt.title( 'CONSTANT' )
    
    plt.show()
    

When I run the above code I got the following error -

    
    
    Traceback (most recent call last):
      File "border.py", line 3, in <module>
        from matplotlib import pyplot as plt
    ImportError: No module named matplotlib
    

After googling, I found that Matplotlib module was missing then I installed it
from the following commands - **sudo python get-pip.py** to install **pip**.
**sudo python get-pip.py** to install **matplotlib**. After when I run the
code no error was reported and the code ran successfully but no windows are
displayed. Can anyone tell me where I'm going wrong?


Answer: ## Step 0: test if `openCV` part is fully working

This shall provide you a reasonable feeling the `CV2` works independently of
`matplotlib` well

Try a simple scenario like this. It opens a new **`cv2`** -window **silently**
which need not get O/S-focus, so find it in your TaskBar and pick it manually.

This simple **`GUI_openCV()`** test shall proof your `cv2` installation is
working :

    
    
    import numpy
    import cv2
    
    def nothing_asCallback( x ):
        pass
    
    def GUI_openCV():
        # Create a black image, a window
       img = numpy.zeros( ( 300, 512, 3 ), numpy.uint8 )
    
       cv2.namedWindow( 'cv2-image' )
    # ----------------------------------------------------------------GUI-<state>
       s = 1
       r = 0
       g = 0
       b = 0
       # -------------------------------------------------------------GUI-<ACTOR>-s
       # create trackbars for color change
       cv2.createTrackbar( 'R',        'cv2-image', 0, 255, nothing_asCallback )
       cv2.createTrackbar( 'G',        'cv2-image', 0, 255, nothing_asCallback )
       cv2.createTrackbar( 'B',        'cv2-image', 0, 255, nothing_asCallback )
       # create switch for ON/OFF functionality
       switch = '0 : OFF \n1 : ON'
       cv2.createTrackbar( switch,     'cv2-image', 0,   1, nothing_asCallback )
       # --------------------------------------------------------------------
       print " ------------------------------------------------- press [ESC] to exit "
       while( 1 ):                                                # GUI-mainloop()
           cv2.imshow( 'cv2-image', img )
    
           k = cv2.waitKey( 1 ) & 0xFF
    
           if k == 27:
               break
    
           # get current positions of four trackbars ----------- # GUI-<vars>-DETECT >>>>>>>>>>>>>>>> can be done "internally" ...cv2.createTrackbar( 'R', 'windowName', aGuiStateVariable_R, 255, nothing_asCallback )
           new_r = cv2.getTrackbarPos( 'R',    'cv2-image' )
           new_g = cv2.getTrackbarPos( 'G',    'cv2-image' )
           new_b = cv2.getTrackbarPos( 'B',    'cv2-image' )
           new_s = cv2.getTrackbarPos( switch, 'cv2-image' )
    
           if ( new_s != s or new_r != r or new_g != g or new_b != b ):
               #------------------------------------------------ # DUMB-<state>-UPDATE
               s = new_s
               r = new_r
               g = new_g
               b = new_b
               #------- ---------------------------------------- # DUMB-ACTOR 
               if s == 0:
                   img[:] = 0
               else:
                   img[:] = [b,g,r]
               pass
           else:
               pass
       pass
       # -----------------------------------------------------------------
       cv2.destroyAllWindows()
    

## Step 1: test if `matplotlib` ad-hoc installation works

Next comes your ad-hoc **`matplotlib`** installation.

Use whatever, trivial in construction, to validate the **`matplotlib`** part:

    
    
    """
    Simple demo of the fill function.
    """
    import numpy
    import matplotlib.pyplot as plt
    
    x = numpy.linspace( 0, 1 )
    y = numpy.sin( 4 * numpy.pi * x ) * numpy.exp( -5 * x )
    
    plt.fill( x, y, 'b' )
    plt.grid( True )
    plt.show()
    

Or run any matplotlib demo from documentation to see, if your module was
installed correctly.

## Step 2: test `openCV` / `matplotlib` post-processing integration

For successfull integration, follow the documentation about different
RGB(A)-slicing indices in each part of your code. This and colourmap-datatype
mis-alignments account for the most troubles on prototyping.

Do not hesitate to use as many **`cv2.imshow( CV2WINDOW, interimPhaseDATA )`**
as comfortable during your ComputeVision projects.

Do not hesitate to **split your code into smallest
possible`{syntax|processing}-bug-tracking` sprints**, which you step-after-
step cross-validate in both **`openCV`** and **`matplotlib`** visualisations (
`cv2.imshow()` and `plt.show()` )

This is a common bug-hunting practice called _"isolation"_

