[SciPy-User] Matplotlib axis label spacing

Barrett B barrett.n.b at gmail.com
Thu Jul 31 17:22:42 EDT 2014


(Not sure if seeking help for matplotlib is appropriate here but I thought
I would give it a try; if not, please direct me to somewhere I can get that
assistance. Anyway...)

Alright, I'm trying to run the code listed below. It results in the
following color plot:

http://s14.postimg.org/m16s2mmox/figure_1.png

I need help resolving the x-axis labels, which are the right number but in
the wrong location. I need them to align evenly with the columns, not be
scaled proportionally to their actual value. I.e., if I were to plot 0.5,
0.6, 0.9, and 2.0 on a real number line from 0 to 4, that is exactly where
they would land, but I don't want that. How do I get them to space evenly?

Also, once I scale this up to a much, much finer resolution (i.e., many
more rows and columns), which I will do once I've debugged this, it's not
going to try to plot every single value on the x-axis, will it? If so, how
would I fix that to print, say, every 10th value, or a total of only 5
values?

Here is the code. Portions of this simulator not used for this run have
been removed for brevity:

##SIMULATOR

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

#Network constants
tau = 20; tau_S = 10000
g_Ca = 3.6; g_K = 10; g_S = 4 #Siemens
E_Ca = 25; E_K = -75; E_S = -75 #mV
E_half_m = -20; E_half_n = -16; E_half_S = -35.245 #mV
E_exc = 0; E_inh = -75 #mV
k_m = 12; k_n = 5.6; k_S = 10 #(unitless)
Theta_syn = -40 #mV

#Initial conditions
V0_0 = -60
n0 = np.array([0.6, 0.6])
S0 = np.array([0.1, 0.1])

#Simulation constants
stopTime = 30000/100; timeStep = 4 #ms
tStartTrack = 2000/100 #Time when Lyapunov exponent calculation begins.
nStartTrack = int(tStartTrack/timeStep) #init step for Lyap expo

#Plotting constants
doNorm = True; V1VsV2 = False; nVsV = False; timeSeries = True
pigments = ['r', 'g', 'b'] #Plotting colors.
toPlot = [0, 1] #Indeces of the neurons to plot, starting at 0.

#Heaviside function. Currently approximated; can be optimised.
#Continuous equation: Gamma(x) = 1.0/(1 + np.exp(-100*(x - Theta_syn)))
def Heaviside(x):
    mult = -100.0; diff = mult*(x - Theta_syn);
    output = np.ones_like(x) #Initialized to 1's.
    output[x < Theta_syn] = 0 #0 <= x < Theta_syn.
    output[abs(diff) < 50] = 1.0/(1 + np.exp(diff[abs(diff) < 50]))
    return output

#ODE for Sherman model (Belykh and Reimbayev)
def f(X, t, params):
    N = len(X)/3
    V = X[:N]; n = X[N:2*N]; S = X[2*N:3*N]

    m_inf_E = 1/(1 + np.exp((E_half_m - V)/k_m))
    n_inf_E = 1/(1 + np.exp((E_half_n - V)/k_n))
    S_inf_E = 1/(1 + np.exp((E_half_S - V)/k_S))

    I_Ca = g_Ca * m_inf_E * (V - E_Ca)
    I_K = g_K * n * (V - E_K)
    I_S = g_S * S * (V - E_S)

    dV = (-I_Ca - I_K - I_S)/tau +\
        g_inh*(E_inh - V)*Heaviside(np.dot(V, connex))/tau
    dn = (n_inf_E - n)/tau
    dS = (S_inf_E - S)/tau_S
    return np.concatenate((dV, dn, dS))
#    else: #if not useForLoops
#        g_inhibitory = params[0]; V1 = params[1]

#Connection matrix. Simulator starts here.
connex = np.array([[-1,1],
                    [1,-1]])

inhibID = 7 #Used for below to avoid commenting out code.
#List of the inhibitory conductivities (g_inh) to track:
if inhibID == 7:
    inhibConductivity = np.array([0.5, 0.6, 0.9, 2.0])

voltageID = 5 #Used for below to avoid commenting out code.
#Starting V1:
if voltageID == 5:
    startingVoltage1 = np.arange(V0_0 + 10, V0_0 + 30, 5)
Vsize = startingVoltage1.size

#Whether the g_inh and V1 arrays have just one element:
justOneRun = (Vsize == 1 and inhibConductivity.size == 1)
t = np.arange(0, stopTime, timeStep)

#useForLoops = True
#if useForLoops:
normVsVSpread = np.zeros((Vsize, inhibConductivity.size))
for i in range(inhibConductivity.size):
    g_inh = inhibConductivity[i]
    for j in range(Vsize):
        pctComplete = 100*((1.*i + j/Vsize)/inhibConductivity.size)
        #Integration and initial conditions
        V0 = np.array([V0_0, startingVoltage1[j]]); N = len(V0)

        #Solver
        soln = odeint(f, np.concatenate((V0, n0, S0)), t, args=(None,))
        E = np.transpose(soln[:, :N])
        n = np.transpose(soln[:, N:N*2])

        VArray = E[:2, nStartTrack:]
        maxVSpread = np.amax(VArray) - np.amin(VArray)
        maxNorm = np.amax(abs(VArray[1] - VArray[0]))
        normVsVSpread[j, i] = maxNorm/maxVSpread
#else: #DO NOT USE YET
#    gInhVsV1 = np.meshgrid(inhibConductivity, startingVoltage1)
#    V0 = np.array([V0_0, None]) #V1 will be dealt with inside the ODE
#    soln = odeint(f, np.concatenate((V0, n0, S0)), t, args=(gInhVsV1,))

#Single-run plotting
strParameters = 'V0_0 = ' + str(V0[0]) + ' mV; n0 = ' + str(n0) +\
    '; S0 = ' + str(S0)
mpl.rcParams['image.cmap'] = 'gist_heat'
fig = plt.subplots()
plt.title('|V1 - V0|/(Vmax - Vmin).\n' + strParameters)
plt.xlabel('g_inh (Siemens)')
plt.ylabel('V1 (mV)')
#    xticks = np.linspace(np.amin(inhibConductivity),\
#        np.amax(inhibConductivity), num = 4)
#    plt.xticks(xticks)
plt.xticks(inhibConductivity)
#    plt.yticks(startingVoltage1)
plt.pcolormesh(normVsVSpread)
plt.colorbar(shrink=0.8)
plt.show()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140731/97542a49/attachment.html>


More information about the SciPy-User mailing list