CoCalc Public Filespython_runge_kutta.ipynbOpen with one click!
Author: Tim Tomaich
Views : 47
Description: Jupyter notebook python_runge_kutta.ipynb
Compute Environment: Ubuntu 18.04 (Deprecated)
In [1]:
import matplotlib.pyplot as plt from numpy.random import rand for color in ['red', 'green', 'blue']: n = 750 x, y = rand(2, n) scale = 200.0 * rand(n) plt.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none') plt.legend() plt.grid(True) plt.show()
In [2]:
from scipy.integrate import ode
In [3]:
import numpy as np from scipy.integrate import ode import matplotlib.pyplot as plt import warnings def logistic(t, y, r): return r * y * (1.0 - y) r = .01 t0 = 0 y0 = 1e-5 t1 = 5000.0 #backend = 'vode' backend = 'dopri5' #backend = 'dop853' solver = ode(logistic).set_integrator(backend, nsteps=1) solver.set_initial_value(y0, t0).set_f_params(r) # suppress Fortran-printed warning solver._integrator.iwork[2] = -1 sol = [] warnings.filterwarnings("ignore", category=UserWarning) while solver.t < t1: solver.integrate(t1, step=True) sol.append([solver.t, solver.y]) warnings.resetwarnings() sol = np.array(sol) plt.plot(sol[:,0], sol[:,1], 'b.-') plt.show()
In [4]:
import matplotlib as mpl import matplotlib.pyplot as plt cmap = mpl.cm.get_cmap('bwr') norm = mpl.colors.Normalize(vmin=0,vmax=1.) ind = range(0,5) vals = [1.5,0.45,3.34,2.78,1.2] pval = [1,1,0.4,1,.1] cols = [cmap(norm(x)) for x in pval] plt.bar(ind,vals,1.0,color=cols) plt.text(0,.5,'3030',rotation='vertical',horizontalalignment='center') plt.text(1,.5,'3050',rotation='vertical',horizontalalignment='center') plt.text(2,.5,'4050',rotation='vertical',horizontalalignment='center') plt.xticks(ind,5*['']) cbar = mpl.colorbar.ColorbarBase(plt.gca(),cmap=cmap, norm=norm) plt.show() plt.colorbar(cbar) plt.show() print(cols)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-db226518434a> in <module>() 16 cbar = mpl.colorbar.ColorbarBase(plt.gca(),cmap=cmap, norm=norm) 17 plt.show() ---> 18 plt.colorbar(cbar) 19 plt.show() 20 print(cols) /projects/anaconda3/lib/python3.5/site-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw) 2260 ax = gca() 2261 -> 2262 ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw) 2263 return ret 2264 colorbar.__doc__ = matplotlib.colorbar.colorbar_doc /projects/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw) 1600 cax, kw = cbar.make_axes(ax, **kw) 1601 cax._hold = True -> 1602 cb = cbar.colorbar_factory(cax, mappable, **kw) 1603 1604 self.sca(current_ax) /projects/anaconda3/lib/python3.5/site-packages/matplotlib/colorbar.py in colorbar_factory(cax, mappable, **kwargs) 1345 cb = ColorbarPatch(cax, mappable, **kwargs) 1346 else: -> 1347 cb = Colorbar(cax, mappable, **kwargs) 1348 1349 cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed) /projects/anaconda3/lib/python3.5/site-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kw) 899 # Ensure the given mappable's norm has appropriate vmin and vmax set 900 # even if mappable.draw has not yet been called. --> 901 mappable.autoscale_None() 902 903 self.mappable = mappable /projects/anaconda3/lib/python3.5/site-packages/matplotlib/cm.py in autoscale_None(self) 345 """ 346 if self._A is None: --> 347 raise TypeError('You must first set_array for mappable') 348 self.norm.autoscale_None(self._A) 349 self.changed() TypeError: You must first set_array for mappable
In [ ]:
norm
In [ ]:
norm(0)
In [ ]: