Pythonã§ãã°ã©ãããç»åãã¢ãã¡ã¼ã·ã§ã³ãåºåããã¨ããmatplotlibã使ãå ´åãå¤ãã¨æãã¾ãã
ããããmatplotlibã¯æå¤ã«é£ããã§ãã
Webæ¤ç´¢ããã¨ãããããããããã¦ã大å¤ãããããã®ã§ãããç¥ããããã¨ãå
¨ã¦æ¸ããã¦ããã¨ã¯å°ãªããã ãããã¯è¤æ°ã®ãµã¤ããè¦ããã¨ã«ãªãã¾ãã
ããã§ãããã§ã¯ãç°¡åãªãã¨ã¯æ¸ãããè¤æ°ã®ãµã¤ããè¦ãªããã°ãªããªããªã£ããã®ã ãæ¸ãã¦ãããã¨æãã¾ãã
åèæç®
åèãµã¤ã
åèæç®ãææ¨ã»ç¹å¾´éã®è¨è¨ããå§ãããã¼ã¿å¯è¦åå¦å
¥éãã®ä»é²ã³ã¼ãã®GitHub
github.com
åºæ¬çãªãã¨
matplotlibãã¨ã£ã¤ãã«ããã®ã¯ãã°ã©ããæ¸ãã¨ããäºéãã®æ¸ãæ¹ãããããã§ãã
ä¸ã¤ã¯ç°¡æçãªæ¸ãæ¹ã§ãplt.xxx()
ã ãã§ããããæ¹æ³ï¼pyplotã¤ã³ã¿ãã§ã¼ã¹ã使ãæ¹æ³ï¼ã§ãã
ããä¸ã¤ã¯ãfig, ax = plt.subplots()
ãªã©ã§ãFigureãªãã¸ã§ã¯ãã¨Axesãªãã¸ã§ã¯ããé©åã«ä½¿ã£ã¦ããæ¹æ³ï¼ãªãã¸ã§ã¯ãæåã¤ã³ã¿ãã§ã¼ã¹ã使ãæ¹æ³ï¼ã§ãã
åèæç®ã®ãææ¨ã»ç¹å¾´éã®è¨è¨ããå§ãããã¼ã¿å¯è¦åå¦å
¥éãã®ä»é²ã³ã¼ãã§ä¾ã示ãã¾ãã
pyplotã¤ã³ã¿ãã§ã¼ã¹ã使ãæ¹æ³
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]
plt.plot(x, y)
plt.title("Using plt.plot()")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
ãªãã¸ã§ã¯ãæåã¤ã³ã¿ãã§ã¼ã¹ã使ãæ¹æ³
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title("Using ax.plot()")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
åè
ã¯ãå°ãªãå®è£
ã§å®ç¾ã§ãããããç´°ããè¦æ ãã«ãã ãããªãã¨ãã«éå®ãã¾ãã
å¾è
ã¯ãç´°ããè¦æ ãã調æ´ã§ããã®ã§ãä»ã®æ¹ã«è¦ããå¿
è¦ãããå ´åã«ä½¿ãã¨ããã¨æãã¾ãã
è¨å®
matplotlibã®è¨å®ã¨ã¯ãmatplotlibrcã®ãã¨ã§ãã
matplotlibrcã®å ´æãç¥ãæ¹æ³
Linuxãªããsite-packagesã®å ´æã¯ãä½ã¨ããã©ãçããã®ã§ããã®ä¸ã®matplotlib/mpl-data/matplotlibrcãè¦ã¤ãããã¨ãåºæ¥ã¾ãããWindowsã®å ´åã¯ããã£ã±ãåããã¾ããã
matplotlibrcã®å ´æãç´æ¥è¡¨ç¤ºããæ¹æ³
ãã®ããæ¹ã¯åãã¦ç¥ã£ãã®ã§ã¡ã¢ãã¦ããã¾ãã
import matplotlib
print( matplotlib.matplotlib_fname() )
表示çµæï¼ããã¾ã§ä¸ä¾ã§ãï¼
'C:\\Users\\xxx\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_xxxxxxxx\\LocalCache\\local-packages\\Python311\\site-packages\\matplotlib\\mpl-data\\matplotlibrc'
sys.pathããè¦ã¤ããæ¹æ³
Pythonã®ã¢ã¸ã¥ã¼ã«æ¤ç´¢ãã¹ã表示ãã¦ããã¾ãã
ä»åã®ç¨é以å¤ã«ãããã使ãæ±ç¨çãªæ¹æ³ã§ããè¦ãããªããã£ã¡ã®æ¹ãããã¨æãã¾ãã
ããããåºåãããå ´åãããã¾ãããsite-packagesãæ¢ãã°OKã§ãã
import sys
print( sys.path )
表示çµæï¼ä¸ä¾ï¼
['', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__xxxxxxxx\\python311.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__xxxxxxxx\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__xxxxxxxx\\Lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__xxxxxxxx', 'C:\\Users\\muni0\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_xxxxxxxx\\LocalCache\\local-packages\\Python311\\site-packages', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__xxxxxxxx\\Lib\\site-packages']
ããã¯ã¨ã³ãã®è¨å®
ããã©ã«ãã¯ä»¥ä¸ã®ããã«ãã³ã¡ã³ãã¢ã¦ããããç¶æ
ã§ããã
## backend: Agg
ãã®ç¶æ
ã ã¨ã以ä¸ã®ããã«ã¤ã³ã¿ã©ã¯ãã£ãã¢ã¼ãï¼ã°ã©ããªã©ãã¦ã£ã³ãã¦ã§è¡¨ç¤ºãããã¢ã¼ãï¼ã«ãªãã¾ãã
Backend TkAgg is interactive backend. Turning interactive mode on.
ä¸æ¹ãã³ã¡ã³ãã¢ã¦ããå¤ãã¦ã¿ã¾ãã
backend: Agg
ã¤ã³ã¿ã©ã¯ãã£ãã¢ã¼ãã§ã¯ãªããªããã°ã©ããªã©ã®ã¦ã£ã³ãã¦ã¯è¡¨ç¤ºãããªããªãã¾ãã
è¤æ°ã®ã°ã©ããç»åãåºåããã¨ãããããã¦ã£ã³ãã¦ãåºã¦ãã¾ãã®ã§ãã¤ã³ã¿ã©ã¯ãã£ãã¢ã¼ãã¯ãªãã«ãã¦ãããå¿
è¦ã«å¿ãã¦ãsavefig()ãã¦ããã¡ã¤ã«ä¿åããããæ¹ã«ãã¦ããã¾ãã
ã±ã¼ã¹ã¹ã¿ãã£
ArtistAnimationã§ãç»åãæ½°ããæãã«ãªã£ã¦ãã¾ã
ã¾ããæ£å¸¸ãªå ´åã¯ä»¥ä¸ã§ãã
åé¡ã®æ½°ããæãã¨ã¯ã以ä¸ã®ããã«ãªãã¾ãã
ArtistAnimationã«æ¸¡ãArtistsã®ä¸èº«ã確èªãã
ã¾ããæ£å¸¸ãªå ´åã¯ä»¥ä¸ã®ããã«ãArtistsãªã¹ãã®ä¸ã«ã5æã®ç»åããããããæ§æè¦ç´ ããªã¹ãã§æã£ã¦ããç¶æ
ã§ãã
[[Text(0, 3.1, 'step=0'), <matplotlib.collections.QuadMesh object at 0x00000147E301ECD0>, Text(0.4, 2.85, ' 0.03'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.10'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=1'), <matplotlib.collections.QuadMesh object at 0x00000147E30925D0>, Text(0.4, 2.85, ' 0.81'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.90'), Text(1.55, 2.5, 'â'), Text(2.4, 2.85, ' 1.00'), Text(2.5500000000000003, 2.5, 'â'), Text(3.1, 2.1, 'R 1.0 (GOAL)'), ...],
[Text(0, 3.1, 'step=2'), <matplotlib.collections.QuadMesh object at 0x00000147E30C5E50>, Text(0.4, 2.85, ' 0.81'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.90'), Text(1.55, 2.5, 'â'), Text(2.4, 2.85, ' 1.00'), Text(2.5500000000000003, 2.5, 'â'), Text(3.1, 2.1, 'R 1.0 (GOAL)'), ...],
[Text(0, 3.1, 'step=3'), <matplotlib.collections.QuadMesh object at 0x00000147E30F1290>, Text(0.4, 2.85, ' 0.81'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.90'), Text(1.55, 2.5, 'â'), Text(2.4, 2.85, ' 1.00'), Text(2.5500000000000003, 2.5, 'â'), Text(3.1, 2.1, 'R 1.0 (GOAL)'), ...],
[Text(0, 3.1, 'step=4'), <matplotlib.collections.QuadMesh object at 0x00000147E511FE50>, Text(0.4, 2.85, ' 0.81'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.90'), Text(1.55, 2.5, 'â'), Text(2.4, 2.85, ' 1.00'), Text(2.5500000000000003, 2.5, 'â'), Text(3.1, 2.1, 'R 1.0 (GOAL)'), ...]]
ä¸æ¹ãåé¡ã®æ½°ããæãã§ã¯ã
[[Text(0, 3.1, 'step=0 phase=0 state=(0, 0)'), <matplotlib.collections.QuadMesh object at 0x000002155B716C10>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(0, 1)'), <matplotlib.collections.QuadMesh object at 0x000002155B785CD0>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(0, 2)'), <matplotlib.collections.QuadMesh object at 0x000002155B799450>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(0, 3)'), <matplotlib.collections.QuadMesh object at 0x000002155D8356D0>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(1, 0)'), <matplotlib.collections.QuadMesh object at 0x000002155D8B7950>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(1, 1)'), <matplotlib.collections.QuadMesh object at 0x000002155B786910>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(1, 2)'), <matplotlib.collections.QuadMesh object at 0x000002155D979450>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(1, 3)'), <matplotlib.collections.QuadMesh object at 0x000002155B714ED0>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(2, 0)'), <matplotlib.collections.QuadMesh object at 0x000002155DC4F350>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(2, 1)'), <matplotlib.collections.QuadMesh object at 0x000002155DC4FD50>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(2, 2)'), <matplotlib.collections.QuadMesh object at 0x000002155DD28A10>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...],
[Text(0, 3.1, 'step=0 phase=0 state=(2, 3)'), <matplotlib.collections.QuadMesh object at 0x000002155DD6DA10>, Text(0.4, 2.85, ' 0.00'), Text(0.45, 2.6, 'â'), Text(0.45, 2.4, 'â'), Text(0.35, 2.5, 'â'), Text(0.55, 2.5, 'â'), Text(1.4, 2.85, ' 0.00'), Text(1.45, 2.6, 'â'), ...]]
ç¹ã«åé¡ãªãããã§ãã
æ£å¸¸ãªæ¹ã¨ãã¬ã¼ã æ°ãåããã¦ã¿ã
æ£å¸¸ãªæ¹ã¯ãstepãã¨ã«ãè¨5æã®ãã¬ã¼ã ã§ã¢ãã¡ã¼ã·ã§ã³åãã¦ãã¾ãã
ä¸æ¹ãç°å¸¸ãªæ¹ã¯ãstateãã¨ã«ãè¨12æã®ãã¬ã¼ã ã§ã¢ãã¡ã¼ã·ã§ã³åãã¦ãã¾ãã
ã§ã¯ãç°å¸¸ãªæ¹ã5æã®ãã¬ã¼ã ã§ã¢ãã¡ã¼ã·ã§ã³åãã¦ã¿ã¾ãã
å
·ä½çã«ã¯ãVisual Source Codeã§ãããã°ãã5æãã¾ã£ãæç¹ã§ããããã°ã³ã³ã½ã¼ã«ã使ã£ã¦ãã¢ãã¡ã¼ã·ã§ã³åãã¾ãã
以ä¸ã¯ä½æ¥ã¤ã¡ã¼ã¸ã§ãã
ããã¨ã5æã®æç¹ã§ã¯æ£å¸¸ãªã¢ãã¡ã¼ã·ã§ã³ç»åã«ãªã£ããææ°ã®åé¡ã¨ãããã¨ã§ããããï¼
以ä¸ãæ£å¸¸ãªã¢ãã¡ã¼ã·ã§ã³ã«ãªã£ãç»åã§ãã
次ã¯8æã§ãã£ã¦ã¿ãããåæ§ã«æ£å¸¸ãªã¢ãã¡ã¼ã·ã§ã³ã«ãªã£ããããã«12æã§ãã£ã¦ãæ£å¸¸ãªã¢ãã¡ã¼ã·ã§ã³ã«ãªã£ãã
ããã¾ã§ãã£ã¦ããã£ã¨åããã¾ããã12æã®ãã¬ã¼ã ããã¾ã£ãæç¹ã§ã¢ãã¡ã¼ã·ã§ã³åããã°åé¡ãªããããã®å¾ã«ãstepãã¨ã®æç»ãå
¥ã£ã¦ãããããã«ãããç»åãæ··ãã£ããããªæãã«ãªã£ãã¨ãããã¨ã ã§ããï¼ãã ã®ãã°ã§ããï¼ã
以ä¸ããæ£å¸¸ã«å
¨stepãå
¨stateã®è¨384ãã¬ã¼ã ã§ã¢ãã¡ã¼ã·ã§ã³åãããã®ã§ãã