Python matplotlibグラフの背景色を変えるサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
1.pyplotライブラリをインポートします
import matplotlib.pyplot as plt

2.pyplotからaxes()を呼び出します
ax = plt.axes()

3.set_facecolor()を呼び出します
ax.set_facecolor(背景色)

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 7]
y = [9, 3, 5, 18, 16, 56]
ax = plt.axes()
ax.set_facecolor('green')
plt.plot(x, y)
plt.show()
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 7] y = [9, 3, 5, 18, 16, 56] ax = plt.axes() ax.set_facecolor('green') plt.plot(x, y) plt.show()
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 7]
y = [9, 3, 5, 18, 16, 56]

ax = plt.axes()

ax.set_facecolor('green')

plt.plot(x, y)

plt.show()

 

Python

Posted by arkgame