Python 更改mathplotlib的轴/附加轴的颜色

Python 更改mathplotlib的轴/附加轴的颜色,python,matplotlib,Python,Matplotlib,我正在使用mathplotlib进行绘图,下面是代码 import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt N = 600 I0 = 1 R0 = 3 S0 = N - I0 - R0 gamma = 0.07142857 R0 = 3.5 beta = gamma * R0 # Contact rate, beta, and mean recovery rate, gam

我正在使用mathplotlib进行绘图,下面是代码

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

N = 600
I0 = 1
R0 = 3
S0 = N - I0 - R0 
gamma = 0.07142857
R0 = 3.5
beta = gamma * R0

# Contact rate, beta, and mean recovery rate, gamma, (in 1/days).
beta, gamma = 0.2, 1./10 
# A grid of time points (in days)
t = np.linspace(0, 365, 365)

# The SIR model differential equations.
def deriv(y, t, N, beta, gamma):
    S, I, R = y
    dSdt = -beta * S * I / N
    dIdt = beta * S * I / N - gamma * I
    dRdt = gamma * I
    return dSdt, dIdt, dRdt

# Initial conditions vector
y0 = S0, I0, R0
# Integrate the SIR equations over the time grid, t.
ret = odeint(deriv, y0, t, args=(N, beta, gamma))
S, I, R = ret.T

# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor='w')
ax = fig.add_subplot(111, axis_bgcolor='#dddddd', axisbelow=True)
ax.plot(t, S, 'b', alpha=0.5, lw=2, label='Susceptible')
ax.plot(t, I, 'm', alpha=0.5, lw=2, label='Infected')
ax.plot(t, R, 'g', alpha=0.5, lw=2, label='Recovered with immunity')
ax.spines['bottom'].set_color('k')
ax.set_xlabel('Time (days)')
ax.set_ylabel('Number of individuals')
ax.set_ylim(0, (N+N/10))
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which='major', c='w', lw=2, ls='-')
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ('top', 'right', 'bottom', 'left'):
    ax.spines[spine].set_visible(False)
plt.show()
但是,我得到了这个错误:

AttributeError: Unknown property axis_bgcolor
如果我从有问题的行中删除axis_bgcolor='dddddddd'并使用

ax = fig.add_subplot(111, axisbelow=True)

相反,我得到了正确的绘图,但没有轴线。如何添加这些内容?

请尝试以下操作:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

N = 600
I0 = 1
R0 = 3
S0 = N - I0 - R0 
gamma = 0.07142857
R0 = 3.5
beta = gamma * R0

# Contact rate, beta, and mean recovery rate, gamma, (in 1/days).
beta, gamma = 0.2, 1./10 
# A grid of time points (in days)
t = np.linspace(0, 365, 365)

# The SIR model differential equations.
def deriv(y, t, N, beta, gamma):
    S, I, R = y
    dSdt = -beta * S * I / N
    dIdt = beta * S * I / N - gamma * I
    dRdt = gamma * I
    return dSdt, dIdt, dRdt

# Initial conditions vector
y0 = S0, I0, R0
# Integrate the SIR equations over the time grid, t.
ret = odeint(deriv, y0, t, args=(N, beta, gamma))
S, I, R = ret.T

# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor='w')
ax = fig.add_subplot(111, axisbelow=True)

ax.plot(t, S, 'b', alpha=0.5, lw=2, label='Susceptible')
ax.plot(t, I, 'm', alpha=0.5, lw=2, label='Infected')
ax.plot(t, R, 'g', alpha=0.5, lw=2, label='Recovered with immunity')
ax.spines['bottom'].set_color('k')
ax.set_xlabel('Time (days)')
ax.set_ylabel('Number of individuals')
ax.set_ylim(0, (N+N/10))
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which='major', c='w', lw=2, ls='-')
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ('top', 'right', 'bottom', 'left'):
    ax.spines[spine].set_visible(False)
#added two lines here setting the face colour of the axes as well as display gridlines
ax.set_facecolor(color='whitesmoke')
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.show()
请注意,在plt.show之前,我已经包括了以下两行

ax.set_facecolor(color='whitesmoke')
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)

这将设置相关轴的bg_颜色;除此之外,我还添加了一些额外的网格线,以防这是您想要的

没有轴线,因为您在最后几行的代码中将它们设置为不可见。