Problem:
I have an array terrain of shape (N+2,N+2,4). In the last dimension, we have either 1's or -1's, -1 if the arrow in that direction is pointing TO the point in question and 1 if the arrow points FROM the point in that direction. The order is north, south, east, west. So, 1,-1,1,1 would correspond to:
The code I'm trying to work with is the following. But that doesn't work since they are all plotted FROM the point. I couldn't find a way to easily plot from or to the point in the desired direction based on the value of the position in terrain[i,j] that corresponds to the direction in question.
import numpy as np
import matplotlib.pyplot as plt
N=3
terrain = np.zeros((N+2,N+2,4))
for i in range(1, N+1):
for j in range(1, N+1):
terrain[i,j] = np.random.choice([-1,1],4)
for i in range(N+2):
for j in range(N+2):
iterr.append([i,j])
iterr = np.array(iterr)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.quiver(iterr[:,0], iterr[:,1], terrain[:,:,0].reshape(((N+2)**2,1)), np.zeros(((N+2)**2,1)), color='k')
plt.quiver(iterr[:,0], iterr[:,1], terrain[:,:,1].reshape(((N+2)**2,1)), np.zeros(((N+2)**2,1)), color='yellow')
plt.quiver(iterr[:,0], iterr[:,1], np.zeros(((N+2)**2,1)), terrain[:,:,2].reshape(((N+2)**2,1)), color='blue')
plt.quiver(iterr[:,0], iterr[:,1], np.zeros(((N+2)**2,1)), terrain[:,:,3].reshape(((N+2)**2,1)), color='red')
Probably plt.quiver(X, Y, U, V, C, **kw) is not the easiest method to use in your case. The quiver method will always plot an arrow vector (u,v) from (x,y).
It is probably easier to iterate over the data and use plt.arrow(x, y, dx, dy, **kwargs) to draw the arrows.
Using your data structures and plt.arrow I wrote this, where I skipped all the 0 values in terrain:
import numpy as np
import matplotlib.pyplot as plt
N=3
terrain = np.zeros((N+2,N+2,4))
for i in range(1, N+1):
for j in range(1, N+1):
terrain[i,j] = np.random.choice([-1,1],4)
iterr = []
for i in range(N+2):
for j in range(N+2):
iterr.append([i,j])
iterr = np.array(iterr)
#-------------------------------------------------
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
X = iterr[:,0]
Y = iterr[:,1]
cardinal_vectors = [(0,1), (0,-1), (1,0), (-1,0)] # north, south, east, west
cardinal_colors = ['black', 'green', 'blue', 'red']
# plot all points
plt.plot(X,Y,'k.')
for x, y, arrow_directions in zip(X, Y, terrain.reshape(((N+2)**2,4))):
for direction, vector, color in zip(arrow_directions,
cardinal_vectors,
cardinal_colors):
arrow_size = 0.5
head_size = 0.2
arrow_args = {'length_includes_head' : True,
'head_width' : head_size,
'head_length' : head_size,
'width' : 0.02,
'fc' : color,
'ec' : color}
dx = vector[0] * arrow_size
dy = vector[1] * arrow_size
if direction == 1:
# arrow FROM (x,y)
plt.arrow(x, y, dx, dy, **arrow_args)
if direction == -1:
# arrow TO (x,y)
plt.arrow(x + dx, y + dy, -dx, -dy, **arrow_args)
plt.show()
Related
I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?
In general, the arrows are of length length as described in the Quiver documentation and are auto-calculated by the matplotlib. I don't know which kwarg may help to return the length.
Another approach could be to define the exact position by scaling the plot with the help of scale=1, units='xy'.
import numpy as np
import matplotlib.pyplot as plt
# define arrow
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.3
v[5,5] = 0.3
plt.quiver(x, y, u, v, scale=1, units='xy')
plt.axis('equal')
plt.xlim(0,1)
plt.ylim(0,1)
plt.show()
Color arrows that end at a specific point
Applying the principles above could result in:
import numpy as np
import matplotlib.pyplot as plt
import random
n = 11
cx = 0.7 #x-position of specific end point
cy = 0.5 #y-position of specific end point
# define random arrows
x = np.linspace(0,1,n)
y = np.linspace(0,1,n)
u = np.zeros((n,n))
v = np.zeros((n,n))
# color everything black
colors = [(0, 0, 0)]*n*n
# make sure at least some points end at the same point
u[5][5] = 0.2
u[5][8] = -0.1
v[2][7] = 0.3
# search for specific point
for i in range(len(x)):
for j in range(len(y)):
endPosX = x[i] + u[j][i]
endPosY = y[j] + v[j][i]
if np.isclose(endPosX, cx) and np.isclose(endPosY, cy):
#found specific point -> color it red
colors[j*n+i] = (1,0,0)
# plot data
plt.quiver(x, y, u, v, color=colors, scale=1, units='xy')
plt.axis('equal')
plt.show()
So I have an equation lets say: x^2 + y^2
Currently, I can make an array that defines the equation, calculates the array based off input parameters and and prints out an array:
def equation(x,y):
return x**2 + y**2
def calculate(x, y, xmin, ymin):
out = []
for i in range(x_min, xs):
row = []
for j in range(y_min, ys):
row.append(equation(i, j))
out.append(row)
return out
The output array calculates the values based off the indicies
output such that (0,0) is upper left. Given an array with length and width, how can I calculate the equation so that the (0,0) is centered and follows a cartesian plane?
To center your data around 0,0,0 and plot the result, you could do something like the following:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def equation(x,y):
return x**2 + y**2
x = [(i-50)/10 for i in range(0,100,1)]
y = x
z = [equation(i, j) for i, j in zip(x, y)]
# plot the function
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# rotate the plot so we can see the 3 dimensions
for angle in range(0, 360):
ax.view_init(30, angle)
plt.draw()
plt.pause(.001)
Result:
Basically you would just need to redefine what you are iterating over to put (0, 0) in the middle. I would suggest that you use a library like numpy though and take advantage of vectorized functions to speed up (and simplify) your code. For example:
import numpy as np
x = np.linspace(-1, 1, 11) # create 11 points arranged from -1 to 1
X, Y = np.meshgrid(x, x) # this produces the input values you'd get out of your double loop
result = np.square(X) + np.square(Y) # your equation, applied to everything at once
I made an odd number of points centered at 0 so that we would actually have an input value of (0, 0) right in the center. We can plot the result with the following:
from matplotlib import pyplot as plt
plt.imshow(result)
Note that the axis ticks are wrong here because imshow doesn't care what our original inputs were, but that dark spot in the center is your (0, 0) input point.
Using: [python] [numpy] [matplotlib]
So I have a 3D array to create a scatter plot making a n * n * n cube. Those points have different values of potential represented by colors.
size = 11
z = y = x = size
potential = np.zeros((z, y, x))
Positive = 10
Negative = -10
""" ------- Positive Polo --------- """
polox = poloy = poloz = [1,2]
polos=[polox,poloy,poloz]
polop = [list(x) for x in np.stack(np.meshgrid(*polos)).T.reshape(-1,len(polos))] # Positive polos list
for coord in polop:
potential[coord] = Positive
""" ------- Negative Polo --------- """
polo2x = polo2y = polo2z = [size-3,size-2]
polos2=[polo2x,polo2y,polo2z]
polon = [list(x) for x in np.stack(np.meshgrid(*polos2)).T.reshape(-1,len(polos2))] # Negative polos list
for coord in polon:
potential[coord] = Negative
I have 2 polos of values -10 and 10 at the start and the rest of the points are calculated like this: (the mean of the surrounding points, no diagonals):
for z in range(1,size):
for y in range(1,size):
for x in range(1,size):
if [z,y,x] in polop:
potential[z,y,x] = Positive # If positive polo, keeps potential
elif [z,y,x] in polon:
potential[z,y,x] = Negative # If negative polo, keeps potential
elif z!=size-1 and y!=size-1 and x!=size-1: # Sets the potential to the mean potential of neighbors
potential[z][y][x] = (potential[z][y][x+1] + potential[z][y][x-1] + potential[z][y+1][x] + potential[z][y-1][x] + potential[z+1][y][x] + potential[z-1][y][x]) / 6
And for the outer cells:
for z in range(0,size):
for y in range(0,size):
for x in range(0,size):
potential[z,y,0] = potential[z,y,2]
potential[z,0,x] = potential[z,2,x]
potential[0,y,x] = potential[2,y,x]
if z == size-1:
potential[size-1,y,x] = potential[size-3,y,x]
elif y == size-1:
potential[z,size-1,x] = potential[z,size-3,x]
elif x == size-1:
potential[z,y,size-1] = potential[z,y,size-3]
What I need is to show a surface connecting the points that have the same value interval 'same colors' (like from 0 to 2.5).
I know that there are a lot of questions like this, but I can't adapt to my code, it either doesn't show (such as this) or it's not the same problem or it's not with python (as this one), that's why I'm asking again.
It can also be shown as a lot of subplots each with a surface.
Note: My 3D array is such that if I type print(potential[1,1,1]) it shows the value of that cell that, as you can see in the image below, is 10. And that's what I use to show the colors.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
z,y,x = potential.nonzero()
cube = ax.scatter(x, y, z, zdir='z', c=potential[z,y,x], cmap=plt.cm.rainbow) # Plot the cube
cbar = fig.colorbar(cube, shrink=0.6, aspect=5) # Add a color bar which maps values to colors.
It would be beneficial for you to create a Minimum, Complete and Verifiable Example to make assistance easier.
It's still not clear to me how you mean to calculate your potential, nor how you mean to generate your surface, so I have included trivial functions.
The code below will generate a 3D Scatterplot of coloured points and a Surface with the average value of the colour.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def fn(x, y):
"""Custom fuction to determine the colour (potential?) of the point"""
return (x + y) / 2 # use average as a placeholder
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
size = 11 # range 0 to 10
# Make the 3D grid
X, Y, Z = np.meshgrid(np.arange(0, size, 1),
np.arange(0, size, 1),
np.arange(0, size, 1))
# calculate a colour for point(x,y,z)
zs = np.array([fn(x, y) for x, y in zip(np.ravel(X), np.ravel(Y))])
ZZ = zs.reshape(X.shape) # this is used below
# create the surface
xx, yy = np.meshgrid(np.arange(0, size, 1), np.arange(0, size, 1))
# Calcule the surface Z value, e.g. average of the colours calculated above
zzs = np.array([np.average(ZZ[x][y]) for x, y in zip(np.ravel(xx), np.ravel(yy))])
zz= zzs.reshape(xx.shape)
cube = ax.scatter(X, Y, Z, zdir='z', c=zs, cmap=plt.cm.rainbow)
surf = ax.plot_surface(xx, yy, zz, cmap=plt.cm.rainbow)
cbar = fig.colorbar(cube, shrink=0.6, aspect=5) # Add a color bar
plt.show()
The image generated will look something like this:
EDIT: With your additional code, I'm able to replicate your cube.
Then use the following code to generate a surface:
xx, yy = np.meshgrid(np.arange(0, size, 1), np.arange(0, size, 1))
#define potential range
min_p = 1.0
max_p = 4.0
zz = np.zeros((size, size))
for i in range(size): # X
for j in range(size): # Y
for k in range(size): # Z
p = potential[k,j,i]
if min_p < p < max_p:
zz[j][i] = p # stop at the first element to meet the conditions
break # break to use the first value in range
Then to plot this surface:
surf = ax.plot_surface(xx, yy, zz, cmap=plt.cm.rainbow)
Note: include vmin and vmax keyword args to keep the same scale, I've left those out so the surface deviations are more visible. I also set the alpha on the cube to 0.2 to make it easier to see the surface.
I Have the following code taken from Here, in this code a figure of height two is generated by two cubes (upper and bottom), I want generate the figure of height two by a only one figure, likewise the figure of height 3,4,5,...
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
def cuboid_data(center, N, size=(1,1,1)):
# code taken from
# https://stackoverflow.com/questions/30715083/python-plotting-a-wireframe-3d-cuboid?noredirect=1&lq=1
# suppose axis direction: x: to left; y: to inside; z: to upper
# get the (left, outside, bottom) point
o = [a - b / 2 for a, b in zip(center, size)]
l, w, h = size
x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in bottom surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in upper surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in outside surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]]] # x coordinate of points in inside surface
y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in bottom surface
[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in upper surface
[o[1], o[1], o[1], o[1], o[1]], # y coordinate of points in outside surface
[o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] # y coordinate of points in inside surface
z = [[0,0,0,0,0],
[N,N,N,N,N],
[0, 0, N,N, 0],
[0, 0, N, N, 0]]
return x, y, z
def plotCubeAt(pos=(0,0), N=0, ax=None):
# Plotting N cube elements at position pos
if ax !=None:
if N > 0:
#for n in range(N):
X, Y, Z = cuboid_data( (pos[0],pos[1],N),N )
ax.plot_surface(X, Y, Z, color='y', rstride=1, cstride=1)#,linewidth=0)
def plotIsoMatrix(ax, matrix):
# plot a Matrix
# where matrix[i,j] cubes are added at position (i,j)
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
plotCubeAt(pos=(i,j), N=matrix[i,j], ax=ax)
l = max(matrix.shape[0], matrix.shape[1], matrix.max())
#bb = np.array([(0,0,0), (0,l,0), (l,0,0), (l,l,0),(0,0,l), (0,l,l), (l,0,l), (l,l,l)])
#ax.plot(bb[:,0], bb[:,1], bb[:,2], "w", alpha=0.0)
if __name__ == '__main__':
fig = plt.figure()
ax = fig.gca(projection='3d')
#ax.set_aspect('equal')
matrix = np.array([[2,2],[1,2]])
plotIsoMatrix(ax, matrix)
#ax.set_axis_off()
plt.ion()
plt.show()
This generate the following figure:
I want to generate the following figure:
How fix this ?
Thanks
Solution usiing matplotlib
First of all, if you do not want to have single cubes, but cuboids, there is a much simpler solution - using matplotlib bar3d.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect('equal')
matrix = np.array([[2,2],[1,2]])
xpos, ypos = np.meshgrid(np.arange(matrix.shape[0]),np.arange(matrix.shape[1]) )
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = np.ones_like(zpos)
dy = dx.copy()
dz = matrix.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='y', zsort='average', linewidth=0)
l = max(matrix.shape[0], matrix.shape[1], matrix.max())
bb = np.array([(0,0,0), (0,l,0), (l,0,0), (l,l,0),(0,0,l), (0,l,l), (l,0,l), (l,l,l)])
ax.plot(bb[:,0], bb[:,1], bb[:,2], "w", alpha=0.0)
ax.set_axis_off()
plt.show()
Concerning the overlapping faces of the cuboids, there is no solution in matplotlib. I think this behaviour is commonly considered to be an unresolvable bug, as is also described in the Matplotlib 3D FAQ. Also, manually setting zorder will not work. The good news is, however, that this overlapping is angle dependent. So you will always find a viewing angle (rotate the plot with the mouse) where it looks good.
Solution using mayavi
Using Mayavi, one does not have the problem of overlapping faces at all. Also the barchart in mayavi is much more convenient, making this only 5 lines of code:
import numpy as np
import mayavi.mlab as mlab
mlab.figure(bgcolor=(1,1,1))
matrix = np.array([[2,2],[1,2]])
mlab.barchart(matrix, color=(1.,0.86, 0.12), lateral_scale=1.0)
mlab.show()
I want to plot 3d cuboid in python.
Input :
center (3 points for the center)
radius (3 radius values, one for each dimension)
Ideally it should be a wireframe plot(I need to see whats inside).I am not exactly sure how to go about this. Using python matplotlib or Mayavi is fine.
Thanks!
So far I have tried the following code ..but that only draws a cube
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
#draw cube
r = [-1, 1]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s,e), color="b")
plt.show()
Whats missing in this code is that its only a cube(not a cuboid) and it's only centered around 0 (I actually want to provide the center)
After thinking a little bit I came up with this.Which seems right. Let me know if you think its not correct...this is the simplest possible way without installing myavi,pygame, povray (I had a hard time installing these on ipython, conda,my windows laptop)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
#draw cube
r1 = [-1, 1]
r2 = [-2, 2]
r3 = [-3, 3]
center =[5,5,5]
for s, e in combinations(np.array(list(product(r1,r2,r3))), 2):
s=np.array(center)+np.array(s)
e=np.array(center)+np.array(e)
ax.scatter3D(*center, color="r")
if np.linalg.norm(s-e) == 2*r1[1] or np.linalg.norm(s-e) == 2*r2[1] or np.linalg.norm(s-e) == 2*r3[1]:
print zip(s,e)
ax.plot3D(*zip(s,e), color="b")
plt.show()
I have encountered the same question, and tried to give a answer as follows.
def cuboid_data(center, size):
"""
Create a data array for cuboid plotting.
============= ================================================
Argument Description
============= ================================================
center center of the cuboid, triple
size size of the cuboid, triple, (x_length,y_width,z_height)
:type size: tuple, numpy.array, list
:param size: size of the cuboid, triple, (x_length,y_width,z_height)
:type center: tuple, numpy.array, list
:param center: center of the cuboid, triple, (x,y,z)
"""
# suppose axis direction: x: to left; y: to inside; z: to upper
# get the (left, outside, bottom) point
o = [a - b / 2 for a, b in zip(center, size)]
# get the length, width, and height
l, w, h = size
x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in bottom surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in upper surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in outside surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]]] # x coordinate of points in inside surface
y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in bottom surface
[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in upper surface
[o[1], o[1], o[1], o[1], o[1]], # y coordinate of points in outside surface
[o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] # y coordinate of points in inside surface
z = [[o[2], o[2], o[2], o[2], o[2]], # z coordinate of points in bottom surface
[o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h], # z coordinate of points in upper surface
[o[2], o[2], o[2] + h, o[2] + h, o[2]], # z coordinate of points in outside surface
[o[2], o[2], o[2] + h, o[2] + h, o[2]]] # z coordinate of points in inside surface
return x, y, z
def test():
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
center = [0, 0, 0]
length = 32 * 2
width = 50 * 2
height = 100 * 2
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = cuboid_data(center, (length, width, height))
ax.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=0.1)
ax.set_xlabel('X')
ax.set_xlim(-100, 100)
ax.set_ylabel('Y')
ax.set_ylim(-100, 100)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()
if __name__ == '__main__':
test()
This is the result:
Here is a wireframe plot for a cuboid.
def plot_cuboid(center, size):
"""
Create a data array for cuboid plotting.
============= ================================================
Argument Description
============= ================================================
center center of the cuboid, triple
size size of the cuboid, triple, (x_length,y_width,z_height)
:type size: tuple, numpy.array, list
:param size: size of the cuboid, triple, (x_length,y_width,z_height)
:type center: tuple, numpy.array, list
:param center: center of the cuboid, triple, (x,y,z)
"""
# suppose axis direction: x: to left; y: to inside; z: to upper
# get the (left, outside, bottom) point
import numpy as np
ox, oy, oz = center
l, w, h = size
x = np.linspace(ox-l/2,ox+l/2,num=10)
y = np.linspace(oy-w/2,oy+w/2,num=10)
z = np.linspace(oz-h/2,oz+h/2,num=10)
x1, z1 = np.meshgrid(x, z)
y11 = np.ones_like(x1)*(oy-w/2)
y12 = np.ones_like(x1)*(oy+w/2)
x2, y2 = np.meshgrid(x, y)
z21 = np.ones_like(x2)*(oz-h/2)
z22 = np.ones_like(x2)*(oz+h/2)
y3, z3 = np.meshgrid(y, z)
x31 = np.ones_like(y3)*(ox-l/2)
x32 = np.ones_like(y3)*(ox+l/2)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# outside surface
ax.plot_wireframe(x1, y11, z1, color='b', rstride=1, cstride=1, alpha=0.6)
# inside surface
ax.plot_wireframe(x1, y12, z1, color='b', rstride=1, cstride=1, alpha=0.6)
# bottom surface
ax.plot_wireframe(x2, y2, z21, color='b', rstride=1, cstride=1, alpha=0.6)
# upper surface
ax.plot_wireframe(x2, y2, z22, color='b', rstride=1, cstride=1, alpha=0.6)
# left surface
ax.plot_wireframe(x31, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
# right surface
ax.plot_wireframe(x32, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
ax.set_xlabel('X')
ax.set_xlim(-100, 100)
ax.set_ylabel('Y')
ax.set_ylim(-100, 100)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()
def test():
center = [0, 0, 0]
length = 32 * 2
width = 50 * 2
height = 100 * 2
plot_cuboid(center, (length, width, height))
if __name__ == '__main__':
test()
Here is the result.
Everybody forgets about POVray that handles 3D very well. It doesn't render wireframe, though, but you can use a half-transparent texture to see what is inside of the box.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
center='-1, -1, -1'
radius='1, 1, 1'
pov='camera { location <0, 2, -3> look_at <0, 1, 2> }\n\
light_source { <2, 4, -3> color rgb 1*1.5}\n\
background {color rgb <0.00, 0.00, 0.00>}\n\
box {<'+center+'>, < '+radius+'>\n\
pigment { color rgbt <0.67, 1.00, 0.39, 0.80> }\n\
rotate <52, 6, 0>\n\
scale 0.9\n\
translate <0, 1.2, 1>}\n\
'
f=open('scene.pov', 'w')
f.write(pov)
f.close()
os.system('povray +W400 +H300 +A +FN scene.pov')
Output "scene.png"
You need to read povray's documentation.