Skip to content

Instantly share code, notes, and snippets.

@ven-kyoshiro
Created September 5, 2018 12:08
Show Gist options
  • Save ven-kyoshiro/331c9239d1e69ee7963e3f6b2564e42e to your computer and use it in GitHub Desktop.
Save ven-kyoshiro/331c9239d1e69ee7963e3f6b2564e42e to your computer and use it in GitHub Desktop.
openai gym's render cannot work with matplotlib
import matplotlib
# matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import gym
import numpy as np
def main():
env = gym.make('CartPole-v0')
env.reset()
_ = env.render(mode='rgb_array')
left = np.array([1, 2, 3, 4, 5])
height = np.array([100, 300, 200, 500, 400])
plt.plot(left, height)
if __name__ == '__main__':
main()
@ven-kyoshiro
Copy link
Author

(myenv) i18-mac-31:cartpole ven$ python error.py
/Users/ven/.pyenv/versions/myenv/lib/python3.6/site-packages/gym/__init__.py:22: UserWarning: DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.spaces. Please run "import gym.spaces" to load gym.spaces on your own. This warning will turn into an error in a future version of gym.
  warnings.warn('DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.spaces. Please run "import gym.spaces" to load gym.spaces on your own. This warning will turn into an error in a future version of gym.')
WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
2018-09-05 21:06:16.482 python[89960:5604678] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f9cd59484e0
2018-09-05 21:06:16.486 python[89960:5604678] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f9cd59484e0'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff2d5872db __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fff54724c76 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff2d61fdb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fff2d4fd820 ___forwarding___ + 1456
	4   CoreFoundation                      0x00007fff2d4fd1e8 _CF_forwarding_prep_0 + 120
	5   Tk                                  0x00007fff39c64318 TkpInit + 467
	6   Tk                                  0x00007fff39be3252 Tk_Init + 1710
	7   _tkinter.cpython-36m-darwin.so      0x0000000112062dc4 Tcl_AppInit + 84
	8   _tkinter.cpython-36m-darwin.so      0x000000011205da0f _tkinter_create + 1071
	9   python                              0x0000000108cd6fe6 _PyCFunction_FastCallDict + 166
	10  python                              0x0000000108d659fc call_function + 588
	11  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	12  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	13  python                              0x0000000108d6717e _PyFunction_FastCallDict + 606
	14  python                              0x0000000108c88b9f _PyObject_FastCallDict + 191
	15  python                              0x0000000108c88d7c _PyObject_Call_Prepend + 156
	16  python                              0x0000000108c88995 PyObject_Call + 101
	17  python                              0x0000000108cf04ae slot_tp_init + 158
	18  python                              0x0000000108cec554 type_call + 308
	19  python                              0x0000000108c88c62 _PyObject_FastCallDict + 386
	20  python                              0x0000000108c89065 _PyObject_FastCallKeywords + 197
	21  python                              0x0000000108d6596b call_function + 443
	22  python                              0x0000000108d5df4e _PyEval_EvalFrameDefault + 5358
	23  python                              0x0000000108d66e26 fast_function + 566
	24  python                              0x0000000108d659cc call_function + 540
	25  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	26  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	27  python                              0x0000000108d6717e _PyFunction_FastCallDict + 606
	28  python                              0x0000000108c88b9f _PyObject_FastCallDict + 191
	29  python                              0x0000000108c88d7c _PyObject_Call_Prepend + 156
	30  python                              0x0000000108c88995 PyObject_Call + 101
	31  python                              0x0000000108d5e1b9 _PyEval_EvalFrameDefault + 5977
	32  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	33  python                              0x0000000108d66d8c fast_function + 412
	34  python                              0x0000000108d659cc call_function + 540
	35  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	36  python                              0x0000000108d66e26 fast_function + 566
	37  python                              0x0000000108d659cc call_function + 540
	38  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	39  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	40  python                              0x0000000108d66d8c fast_function + 412
	41  python                              0x0000000108d659cc call_function + 540
	42  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	43  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	44  python                              0x0000000108d66d8c fast_function + 412
	45  python                              0x0000000108d659cc call_function + 540
	46  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	47  python                              0x0000000108d66e26 fast_function + 566
	48  python                              0x0000000108d659cc call_function + 540
	49  python                              0x0000000108d5dec3 _PyEval_EvalFrameDefault + 5219
	50  python                              0x0000000108d665fb _PyEval_EvalCodeWithName + 2907
	51  python                              0x0000000108d5c9d0 PyEval_EvalCode + 48
	52  python                              0x0000000108d9417e PyRun_FileExFlags + 174
	53  python                              0x0000000108d9366b PyRun_SimpleFileExFlags + 283
	54  python                              0x0000000108daed56 Py_Main + 3446
	55  python                              0x0000000108c7df38 main + 248
	56  libdyld.dylib                       0x00007fff5533e015 start + 1
	57  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

@ven-kyoshiro
Copy link
Author

@ven-kyoshiro
Copy link
Author

This is the answer !!!!!!!!!!

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import gym
import numpy as np

def main():
    env = gym.make('CartPole-v0')
    env.reset()
    _ = env.render(mode='rgb_array')
    env.close()
    left = np.array([1, 2, 3, 4, 5])
    height = np.array([100, 300, 200, 500, 400])
    plt.plot(left, height)

if __name__ == '__main__':
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment