2

I am trying to plot some .wav data with librosa, but I am having some unusual problems that I couldn't find much on googling "python librosa error creating graphics ctxt object"

def plot_waves(sound_names, raw_sounds):
    i = 1
    fig = plt.figure(figsize=(25, 60), dpi=900)
    for n, f in zip(sound_names, raw_sounds):
        print "plot_waves:", i
        plt.subplot(10, 1, i)
        librosa.display.waveplot(np.array(f), sr=22050)
        plt.title(n.title())
        i += 1
    print "plot_waves_loop_end"


    plt.suptitle("Figure 1: Waveplot", x=0.5, y=0.915, fontsize=18)

    #THIS LINE CAUSES THE ERROR
    plt.show()

Possibly macOS related.

I've tried in virtualenv and outside, same error for both cases.

pip install {matplotlib, librosa, numpy} all show as no updates needed, in virtualenv and outside

error message (keeps going beyond this and locks up python - have to force quit)

2017-03-24 22:15:03.393 python[62870:25289530] _initWithWindowNumber: error creating graphics ctxt object for ctxt:0x3653b, window:0x9754
2017-03-24 22:15:03.393 python[62870:25289530] _initWithWindowNumber: error creating graphics ctxt object for ctxt:0x3653b, window:0x9754
Mar 24 22:15:03  python[62870] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextGetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextGetCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextGetDefaultUserSpaceToDeviceSpaceTransform: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextConcatCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Mar 24 22:15:03  python[62870] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • 1
    I found this line was the culprit: `fig = plt.figure(figsize=(25, 60), dpi=900)` Once removed, the program runs correctly. It may be specific to macOS, not sure. – SwimBikeRun Mar 27 '17 at 17:35

1 Answers1

0

Rather than removing the line (as per your comment), I think the issues is with the dpi setting being too high (900 is probably overkill!). Try reducing that (for example, to 30), and it should work.

def plot_waves(sound_names, raw_sounds):
    i = 1
    fig = plt.figure(figsize=(25, 60), dpi=30)  ## changed from 900 to 30
    for n, f in zip(sound_names, raw_sounds):
        print "plot_waves:", i
        plt.subplot(10, 1, i)
        librosa.display.waveplot(np.array(f), sr=22050)
        plt.title(n.title())
        i += 1
    print "plot_waves_loop_end"


    plt.suptitle("Figure 1: Waveplot", x=0.5, y=0.915, fontsize=18)

    plt.show()
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139