wilberox
wilberox

Reputation: 193

Change colour of axes/add in axes for mathplotlib

I am making a plot using mathplotlib, here is the code

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()

However, I get this error:

AttributeError: Unknown property axis_bgcolor

If I remove axis_bgcolor='#dddddd' from the offending line and use

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

instead, I get the correct plot but there are no axes lines. How can I add these in?

Upvotes: 0

Views: 226

Answers (1)

redmonkey
redmonkey

Reputation: 86

perhaps try the following:

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()

Note that I have included the following 2 lines before plt.show()

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

This will set the bg_colour of the axes in question; further to this i added some additional gridlines incase this is something you'd like to have

Upvotes: 1

Related Questions