Related
I need help to make an optimization graph in 3 dimensions with manim. I am having difficulty understanding how to use the projection methods and to represent the axes and data in three-dimensional space. If anyone has experience with manim and can help me understand how to do it, I would greatly appreciate it. Thank you
Note: I am trying to graph the schewefel function below I attach part of the code that I have been working on
%%manim -qm -v WARNING AckleyAnimation
class Schewefel(ThreeDScene):
def func(self,u,v):
return x * np.sin(np.sqrt(np.abs(x))) + y * np.sin(np.sqrt(np.abs(y)))
def construct(self):
cielo = '#C4DDFF'
azul = '#001D6E'
rojo = "#B20600"
axes = ThreeDAxes()
self.set_camera_orientation(phi=65*DEGREES,theta=60*DEGREES)
# self.add(axes,x,y)
name_function = Tex(r"Schwefel function." ,font_size=38).to_corner(UL)
self.add_fixed_in_frame_mobjects(name_function)
funcion = MathTex(r"f(x) = 418.9829d - \sum_{i=1}^{d} x_i \sin \sqrt{|x_i|}", font_size=30).to_corner(UL)
self.play(Write(name_function))
self.play(FadeOut(name_function))
self.add_fixed_in_frame_mobjects(funcion)
self.play(Write(funcion))
surface_plane = Surface(
lambda u, v: np.array([u, v, self.func(u, v)]),
u_range=[-100,100],
v_range=[-100,100],
resolution=(50, 50))
self.play(Write(surface_plane))
Graphics
function Schewefel
Correction:
So far I modified the script and I have the following
%%manim -qm -v WARNING PlotSurfaceExample
class PlotSurfaceExample(ThreeDScene):
def construct(self):
resolution_fa = 16
self.set_camera_orientation(phi=75 * DEGREES, theta=-60 * DEGREES)
axes = ThreeDAxes(x_range=(-500, 500, 1800), y_range=(-500, 500, 1800), z_range=(-500, 500, 1800))
def param_trig(u, v):
x = u
y = v
z = x * np.sin(np.sqrt(np.abs(x))) + y * np.sin(np.sqrt(np.abs(y)))
return z
trig_plane = axes.plot_surface(
param_trig,
resolution=(resolution_fa, resolution_fa),
u_range = (-500, 500),
v_range = (-500, 500),
colorscale = [BLUE, GREEN, YELLOW, ORANGE, RED],
)
self.add(axes)
self.play(Write(trig_plane))
but it gives me this result
but nevertheless I hope this
I pretty much deleted the last code and started new. I added a new class called Object which is the replacement for the lists called body_1 and body_2. Also all the calculations are now done from within the Object class. Most previous existing issues were resolved through this process but there is still one that presists. I believe its inside the StartVelocity() function which creates the v1/2 needed to start the Leapfrog algorithm. This should give me a geostationary Orbit but as clearly visible the Satelite escapes very quickly after zooming through earth.
Codes are:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from object import Object
import numpy as np
class Simulation:
def __init__(self):
# Index: 0 Name, 1 Position, 2 Velocity, 3 Mass
body_1 = Object("Earth", "g", "r",
np.array([[0.0], [0.0], [0.0]]),
np.array([[0.0], [0.0], [0.0]]),
5.9722 * 10**24)
body_2 = Object("Satelite", "b", "r",
np.array([[42164.0], [0.0], [0.0]]),
np.array([[0.0], [3075.4], [0.0]]),
5000.0)
self.bodies = [body_1, body_2]
def ComputePath(self, time_limit, time_step):
time_range = np.arange(0, time_limit, time_step)
for body in self.bodies:
body.StartVelocity(self.bodies, time_step)
for T in time_range:
for body in self.bodies:
body.Leapfrog(self.bodies, time_step)
def PlotObrit(self):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for body in self.bodies:
body.ReshapePath()
X, Y, Z = [], [], []
for position in body.path:
X.append(position[0])
Y.append(position[1])
Z.append(position[2])
ax.plot(X, Y, Z, f"{body.linecolor}--")
for body in self.bodies:
last_pos = body.path[-1]
ax.plot(last_pos[0], last_pos[1], last_pos[2], f"{body.bodycolor}o", label=body.name)
ax.set_xlabel("x-Axis")
ax.set_ylabel("y-Axis")
ax.set_zlabel("z-Axis")
ax.legend()
fig.savefig("Leapfrog.png")
if __name__ == "__main__":
sim = Simulation()
sim.ComputePath(0.5, 0.01)
sim.PlotObrit()
import numpy as np
class Object:
def __init__(self, name, bodycolor, linecolor, pos_0, vel_0, mass):
self.name = name
self.bodycolor = bodycolor
self.linecolor = linecolor
self.position = pos_0
self.velocity = vel_0
self.mass = mass
self.path = []
def StartVelocity(self, other_bodies, time_step):
force = self.GetForce(other_bodies)
self.velocity += (force / self.mass) * time_step * 0.5
def Leapfrog(self, other_bodies, time_step):
self.position += self.velocity * time_step
self.velocity += (self.GetForce(other_bodies) / self.mass) * time_step
self.path.append(self.position.copy())
def GetForce(self, other_bodies):
force = 0
for other_body in other_bodies:
if other_body != self:
force += self.Force(other_body)
return force
def Force(self, other_body):
G = 6.673 * 10**-11
dis_vec = other_body.position - self.position
dis_mag = np.linalg.norm(dis_vec)
dir_vec = dis_vec / dis_mag
for_mag = G * (self.mass * other_body.mass) / dis_mag**2
for_vec = for_mag * dir_vec
return for_vec
def ReshapePath(self):
for index, position in enumerate(self.path):
self.path[index] = position.reshape(3).tolist()
Im aware that Body 2's position has to be multiplied by 1000 to get meters but it would just fly in a straight line if i would do that and there would be no signs of gravitational forces what so ever.
The constant G is in kg-m-sec units. The radius of the satellite orbit however only makes sense in km, else the orbit would be inside the Earth core. Then the speed in m/sec gives a near circular orbit with negligible eccentricity. (Code from a math.SE question on Kepler law quantities)
import math as m
G = 6.673e-11*1e-9 # km^3 s^-2 kg^-1
M_E = 5.9722e24 # kg
R_E = 6378.137 # km
R_sat = 42164.0 # km from Earth center
V_sat = 3075.4/1000 # km/s
theta = 0
r0 = R_sat
dotr0 = V_sat*m.sin(theta)
dotphi0 = -V_sat/r0*m.cos(theta)
R = (r0*V_sat*m.cos(theta))**2/(G*M_E)
wx = R/r0-1; wy = -dotr0*(R/(G*M_E))**0.5
E = (wx*wx+wy*wy)**0.5; psi = m.atan2(wy,wx)
T = m.pi/(G*M_E)**0.5*(R/(1-E*E))**1.5
print(f"orbit constants R={R} km, E={E}, psi={psi} rad")
print(f"above ground: min={R/(1+E)-R_E} km, max={R/(1-E)-R_E} km")
print(f"T={2*T} sec, {T/1800} h")
with output
orbit constants R=42192.12133271948 km, E=0.0006669512550867562, psi=-0.0 rad
above ground: min=35785.863 km, max=35842.14320159004 km
T=86258.0162673565 sec, 23.960560074265697 h
for r(phi)=R/(1+E*cos(phi-psi))
Implementing these changes in your code and calling with
sim.ComputePath(86e+3, 600.0)
gives a nice circular orbit
What I try is to plot output of the gfs weather model with matplotlib using pygrib to save the data, which is saved in grib files. Nearly everything works fine, the output looks like this:
It appears that the program isn't closing the gap between 359.5 and 360 degress by using the data of 0 degress. If the data would be in a regular list or something I would use the data of 0° and save it for 360° too by appending the list. I've seen people having the same problem with non-pygrib data.
If you know how to change the pygrib data (regular operations don't work on pygrib data unfortunately) or how to make matplotlib close the gap, you would really help me out of this problem. Maybe the function "addcyclic" could help, but I don't know how.
EDIT: I solved the problem, see my answer.
So here is the code producing the problem:
#!/usr/bin/python3
import os, sys, datetime, string
from abc import ABCMeta, abstractmethod
import numpy as np
import numpy.ma as ma
from scipy.ndimage.filters import minimum_filter, maximum_filter
import pygrib
from netCDF4 import Dataset
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid
import laplaceFilter
import mpl_util
class Plot(Basemap):
def __init__(self, basemapParams):
super().__init__(**basemapParams)
self.layers = []
def addLayer(self, layer):
self.layers.append(layer)
def plot(self, data):
for layer in self.layers:
layer.plot(self, data)
plt.title('Plot')
plt.show()
class Layer(metaclass=ABCMeta):
def __init__(self):
pass
#abstractmethod
def plot(self, plot, data):
return NotImplemented
class BackgroundLayer(Layer):
def __init__(self, bgtype, coords):
#possible bgtype values: borders, topo, both
self.bgtype = bgtype
self.lonStart = coords[0]
self.lonEnd = coords[1]
self.latStart = coords[2]
self.latEnd = coords[3]
def plot(self, plot, data):
[...]
def findSubsetIndices(self,min_lat,max_lat,min_lon,max_lon,lats,lons):
[...]
class LegendLayer(Layer):
def __init__(self):
pass
class GribDataLayer(Layer, metaclass=ABCMeta):
def __init__(self, varname, level, clevs, cmap, factor):
self.varname = varname
self.level = level
self.clevs = clevs
self.cmap = cmap
self.factor = factor
def plot(self, plot, data):
#depending on the height we want to use, we have to change the index
indexes = {1000:0, 2000:1, 3000:2, 5000:3, 7000:4, 10000:5, 15000:6, 20000:7, 25000:8, 30000:9,
35000:10, 40000:11, 45000:12, 50000:13, 55000:14, 60000:15, 65000:16, 70000:17,
75000:18, 80000:19, 85000:20, 90000:21, 92500:22, 95000:23, 97500:24, 100000:25, 0:0}
selecteddata = data.select(name = self.varname)[indexes[self.level]]
lats, lons = selecteddata.latlons()
layerdata = selecteddata.values*self.factor
x, y = plot(lons, lats) # compute map proj coordinates.
self.fillLayer(plot, x, y, layerdata, self.clevs, self.cmap)
#abstractmethod
def fillLayer(self, plot, x, y, layerdata, clevs, cmap):
return NotImplemented
class ContourLayer(GribDataLayer):
def __init__(self, varname, level, clevs, cmap, factor, linewidth=1.5, fontsize=15,
fmt="%3.1f", inline=0,labelcolor = 'k'):
self.linewidth = linewidth
self.fontsize = fontsize
self.fmt = fmt
self.inline = inline
self.labelcolor = labelcolor
super().__init__(varname, level, clevs, cmap, factor)
def fillLayer(self, plot, x, y, layerdata, clevs, cmap):
# contour data over the map.
cs = plot.contour(x,y,layerdata,clevs,colors = cmap,linewidths = self.linewidth)
plt.clabel(cs, clevs, fontsize = self.fontsize, fmt = self.fmt,
inline = self.inline, colors = self.labelcolor)
if self.varname == "Pressure reduced to MSL":
self.plotHighsLows(plot,layerdata,x,y)
def plotHighsLows(self,plot,layerdata,x,y):
[...]
class ContourFilledLayer(GribDataLayer):
def __init__(self, varname, level, clevs, cmap, factor, extend="both"):
self.extend = extend
super().__init__(varname, level, clevs, cmap, factor)
def fillLayer(self, plot, x, y, layerdata, clevs, cmap):
# contourfilled data over the map.
cs = plot.contourf(x,y,layerdata,levels=clevs,cmap=cmap,extend=self.extend)
#cbar = plot.colorbar.ColorbarBase(cs)
[...]
ger_coords = [4.,17.,46.,56.]
eu_coords = [-25.,57.,22.,70.]
### Choose Data
data = pygrib.open('gfs.t12z.mastergrb2f03')
### 500hPa Europe
coords = eu_coords
plot1 = Plot({"projection":"lcc","resolution":"h","rsphere":(6378137.00,6356752.3142), "area_thresh": 1000.,
"llcrnrlon":coords[0],"llcrnrlat":coords[2],"urcrnrlon":coords[1],"urcrnrlat":coords[3],
"lon_0":(coords[0]+coords[1])/2.,"lat_0":(coords[2]+coords[3])/2.})
clevs = range(480,600,4)
cmap = plt.cm.nipy_spectral
factor = .1
extend = "both"
level = 50000
layer1 = ContourFilledLayer('Geopotential Height', level, clevs, cmap, factor, extend)
clevs = [480.,552.,600.]
linewidth = 2.
fontsize = 14
fmt = "%d"
inline = 0
labelcolor = 'k'
layer2 = ContourLayer('Geopotential Height', level, clevs, 'k', factor, linewidth, fontsize, fmt, inline, labelcolor)
level = 0
clevs = range(800,1100,5)
factor = .01
linewidth = 1.5
inline = 0
labelcolor = 'k'
layer3 = ContourLayer('Pressure reduced to MSL', level, clevs, 'w', factor, linewidth, fontsize, fmt, inline, labelcolor)
plot1.addLayer(BackgroundLayer('borders', coords))
plot1.addLayer(layer1)
plot1.addLayer(layer2)
plot1.addLayer(layer3)
plot1.plot(data)
I solved it myself 2 months later:
Matplotlib doesn't fill the area if your longitude range is from 0 to 359.75 because it ends there from matplotlibs point of view. I solved it by dividing up the data and then stacking it.
selecteddata_all = data.select(name = "Temperature")[0]
selecteddata1, lats1, lons1 = selecteddata_all.data(lat1=20,lat2=60,lon1=335,lon2=360)
selecteddata2, lats2, lons2 = selecteddata_all.data(lat1=20,lat2=60,lon1=0,lon2=30)
lons = np.hstack((lons1,lons2))
lats = np.hstack((lats1,lats2))
selecteddata = np.hstack((selecteddata1,selecteddata2))
No white area left of 0° anymore.
I don't know whether there is a fix if you wanna plot a whole hemisphere (0 to 359.75 deg).
I've run in to this myself a few times, and the addcyclic function of the basemap module actually works pretty well. The basemap docs lay out the syntax and use pretty well.
In terms of the variables in your code you can add the cyclic point either before or after you multiply by self.factor in your GribDataLayer class:
layerdata, lons = addcyclic(layerdata, lons)
You can also use np.append and write your own function to accomplish this same task. It would look something like this:
layerdata = np.append(layerdata,layerdata[...,0,None],axis=-1)
If your input data are 2D then the syntax above is equivalent to selecting all the data in the first longitude band (i.e. layerdata[:,0])
layerdata = np.append(layerdata,layerdata[:,0,None],axis=-1)
Hope this helps!
I'm trying to make an animation with a sequence of datafiles in mayavi. Unfortunately i have noticed that camera doesn't lock (it is zooming and zooming out). I think it is happening because the Z componrnt of my mesh is changing and mayavi is trying to recalculate scales.
How can I fix it?
import numpy
from mayavi import mlab
mlab.figure(size = (1024,768),bgcolor = (1,1,1))
mlab.view(azimuth=45, elevation=60, distance=0.01, focalpoint=(0,0,0))
#mlab.move(forward=23, right=32, up=12)
for i in range(8240,8243):
n=numpy.arange(10,400,20)
k=numpy.arange(10,400,20)
[x,y] = numpy.meshgrid(k,n)
z=numpy.zeros((20,20))
z[:] = 5
M = numpy.loadtxt('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\1disk_j9.5xyz\\'+'{0:05}'.format(i)+'.txt')
Mx = M[:,0]; My = M[:,1]; Mz = M[:,2]
Mx = Mx.reshape(20,20); My = My.reshape(20,20); Mz = Mz.reshape(20,20);
s = mlab.quiver3d(x,y,z,Mx, My, -Mz, mode="cone",resolution=40,scale_factor=0.016,color = (0.8,0.8,0.01))
Mz = numpy.loadtxt('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\Mzi\\' + '{0:05}'.format(i) + '.txt')
n=numpy.arange(2.5,400,2)
k=numpy.arange(2.5,400,2)
[x,y] = numpy.meshgrid(k,n)
f = mlab.mesh(x, y, -Mz/1.5,representation = 'wireframe',opacity=0.3,line_width=1)
mlab.savefig('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\figs\\'+'{0:05}'.format(i)+'.png')
mlab.clf()
#mlab.savefig('B:\\Dropbox\\Master.Diploma\\figures\\vortex.png')
print(i)
mlab.show()
for anyone still interested in this, you could try wrapping whatever work you're doing in this context, which will disable rendering and return the disable_render value and camera views to their original states after the context exits.
with constant_camera_view():
do_stuff()
Here's the class:
class constant_camera_view(object):
def __init__(self):
pass
def __enter__(self):
self.orig_no_render = mlab.gcf().scene.disable_render
if not self.orig_no_render:
mlab.gcf().scene.disable_render = True
cc = mlab.gcf().scene.camera
self.orig_pos = cc.position
self.orig_fp = cc.focal_point
self.orig_view_angle = cc.view_angle
self.orig_view_up = cc.view_up
self.orig_clipping_range = cc.clipping_range
def __exit__(self, t, val, trace):
cc = mlab.gcf().scene.camera
cc.position = self.orig_pos
cc.focal_point = self.orig_fp
cc.view_angle = self.orig_view_angle
cc.view_up = self.orig_view_up
cc.clipping_range = self.orig_clipping_range
if not self.orig_no_render:
mlab.gcf().scene.disable_render = False
if t != None:
print t, val, trace
ipdb.post_mortem(trace)
I do not really see the problem in your plot but to reset the view after each plotting instance insert your view point:
mlab.view(azimuth=45, elevation=60, distance=0.01, focalpoint=(0,0,0))
directly above your mlab.savefig callwithin your for loop .
You could just use the vmin and vmax function in your mesh command, if u do so the scale will not change with your data and your camera should stay where it is.
Like this:
f = mlab.mesh(x, y, -Mz/1.5,representation = 'wireframe',vmin='''some value''',vmax='''some value''',opacity=0.3,line_width=1)
I am trying to solve two independent variables varying geometrically over a given domain. I want to plot their variance in a single viewer display. How can I get two different contour plots one each for the independent variable in single viewer box? I have used the following code for double contour but cannot get different contours for both the variables (phasegamma and phasesigma in my case). Please suggest how it can be corrected or any other possible way to get two contours in one plot.
import pylab
class PhaseViewer(Matplotlib2DGridViewer):
def __init__(self, phasesigma, phasegamma, title = None, limits ={}, **kwlimits):
self.phasesigma = phasesigma
self.contour1 = None
self.phasegamma = phasegamma
self.contour2 = None
Matplotlib2DGridViewer.__init__(self, vars=(1-phasegamma-phasesigma),title=title,cmap=pylab.cm.hot,limits ={}, **kwlimits)
def _plot(self):
Matplotlib2DGridViewer._plot(self)
if self.contour1 is not None or self.contour2 is not None:
for Ccr in self.contour1.collections:
Ccr.remove()
for Cni in self.contour1.collections:
Cni.remove()
mesh = self.phasesigma.getMesh()
mesh2 = self.phasegamma.getMesh()
shape = mesh.getShape()
shape2 = mesh2.getShape()
x, y = mesh.getCellCenters()
z = self.phasesigma.getValue()
x, y, z = [a.reshape(shape, order="FORTRAN") for a in (x, y, z)]
self.contour1 = pylab.contour(x, y, z, (0.5,))
l, m = mesh1.getCellCenters()
w = self.phasegamma.getValue()
l, m, w = [b.reshape(shape, order ="FORTRAN") for b in (l, m, w)]
self.contour2 = pylab.contour(l, m, w, (0.5,))
raw_input("check2")
viewer = PhaseViewer(phasesigma=phasesigma, phasegamma=phasegamma,\
title = r"%s & %s" % (phasegamma.name, phasesigma.name), datamin=0., datamax=1.)
except ImportError:
viewer = MultiViewer(viewers=(Viewer(vars=phasesigma,datamin=0.,datamax=1),Viewer(vars=phasegamma,datamin=0.,datamax=1.)))
I just saw this, so hopefully it's still useful to you. I'm not sure why your version didn't work, although I generally find that pylab works at too high a level and does too many things automatically.
I based the following on Matplotlib2DContourViewer and it seems to do what you want:
class PhaseViewer(Matplotlib2DGridViewer):
def __init__(self, phasesigma, phasegamma, title = None, limits ={}, **kwlimits):
self.phasesigma = phasesigma
self.contour1 = None
self.phasegamma = phasegamma
self.contour2 = None
self.number = 10
self.levels = None
Matplotlib2DGridViewer.__init__(self, vars=(1-phasegamma-phasesigma),title=title,cmap=pylab.cm.hot,limits ={}, **kwlimits)
def _plot(self):
Matplotlib2DGridViewer._plot(self)
if hasattr(self, "_contourSet"):
for countourSet in self._contourSet:
for collection in ccontourSet.collections:
try:
ix = self.axes.collections.index(collection)
except ValueError, e:
ix = None
if ix is not None:
del self.axes.collections[ix]
self._contourSet = []
for var in (self.phasesigma, self.phasegamma):
mesh = var.mesh
x, y = mesh.cellCenters
z = var.value
xmin, ymin = mesh.extents['min']
xmax, ymax = mesh.extents['max']
from matplotlib.mlab import griddata
xi = fp.numerix.linspace(xmin, xmax, 1000)
yi = fp.numerix.linspace(ymin, ymax, 1000)
# grid the data.
zi = griddata(x, y, z, xi, yi, interp='linear')
zmin, zmax = self._autoscale(vars=[var],
datamin=self._getLimit(('datamin', 'zmin')),
datamax=self._getLimit(('datamax', 'zmax')))
self.norm.vmin = zmin
self.norm.vmax = zmax
if self.levels is not None:
levels = self.levels
else:
levels = fp.numerix.arange(self.number + 1) * (zmax - zmin) / self.number + zmin
self._contourSet.append(self.axes.contour(xi, yi, zi, levels=levels, cmap=self.cmap))
self.axes.set_xlim(xmin=self._getLimit('xmin'),
xmax=self._getLimit('xmax'))
self.axes.set_ylim(ymin=self._getLimit('ymin'),
ymax=self._getLimit('ymax'))
if self.colorbar is not None:
self.colorbar.plot()