Options, e.g. title, thickness, etc for plot3d and parametric_plot3d?

asked 2019-05-17 21:33:20 +0200

PaulF gravatar image

I'm switching from Mathematica to Sage for teaching purposes and am having some difficulty with plot options. Here's an example, in which I create the function f(x,y)=x^2+y^2 and plot the function along with the cross-section curve f(.2,y).

 x,y = var('x,y')
f(x,y) = x^2 + y^2

Surface=plot3d(f(x,y),(x,-1,1),(y,-1,1), frame=True,color='green', opacity=0.1,title='A plot of $f(x,y)$')
x0=.2
Cross_Section=parametric_plot3d((x0,y,f(x0,y)), (y,-1,1), color='red', thickness=5)
Cross_Section+Surface

Questions:

  1. The title does not appear. How can I correct that?
  2. Changing the "thickness" option doesn't appear to have any effect on the cross-section curve.

For 2-dim graphs, I don't seem to encounter problems. To plot a contour diagram for the above function, I entered the following, which worked fine:

C=contour_plot(f,(-1,1),(-1,1),fill=False, cmap='hsv', labels=True,gridlines=True,title='Contour Plot of $f(x,y)=x^2+y^2$')
C.axes_labels(['$x$','$y$'])
C.axes_labels_size(2.5)   
C.fontsize(6)    
C
edit retag flag offensive close merge delete

Comments

Hello, @PaulF. It is my understanding that SageMath doesn't use a graphics library for 3D plots; it stores the plots into data structures, and then uses software like Jmol, Tachyon or three.js to render the corresponding 3D scene. Unfortunately, the title parameter doesn't seem to be implemented (?).

So, as for your first question, I have a coiple of suggestions: 1. You could hack Sage's 3D-plotting subroutines to use a title (that's the beauty of open-source). 2. You could plot directly with Matplotlib, which is part of Sage itself. (Please, let me know if you require help with this, and how soon you need it.)

Regarding your second question, I have tried changing the thickness to 1 (for example), and it worked correctly. Can you confirm the problem again?

dsejas gravatar imagedsejas ( 2019-05-20 19:13:24 +0200 )edit

Thanks. I resolved the thickness issue. As for the surface, here's what I have so far, which works:

import matplotlib.pyplot as plt

import numpy as np

fig = plt.figure()

ax = fig.gca(projection='3d')

X = np.arange(-1, 1, 0.25)

Y = np.arange(-1, 1, 0.25)

X, Y = np.meshgrid(X, Y)

Z=X**2+Y**2

surf = ax.plot_surface(X, Y, Z,)

plt.title('$f(x,y)=x^2+y^2$', loc='right', horizontalalignment='center', verticalalignment='top')

plt.show()

Three questions:

  1. Is there a more efficient way to create surface above?

  2. How might I superimpose a cross-section on the surface?

  3. Is it possible to add a command/routine to permit rotating the surface with a mouse?

Thanks.

PaulF gravatar imagePaulF ( 2019-05-20 20:47:55 +0200 )edit

Hello, @PaulF. Unfortunately I have failed miserably trying to mix the cross section with your Matplotlib plot. I have tried other alternatives. For example, I tried using the text3d command; however, it makes the axes a little larger. Then I tried to rewrite the code that renders Jmol scenes, and I succeeded, but only with static images (PNG, etc.). I failed doing it with dynamic ones, because the base code for 3D plotting is written in Cython, which I don't know very well.

I am so sorry I failed with this! I will keep trying to modify my code to work with dynamic images. If I do it, I will send you the code immediately.

Maybe the lack of a title command for 3D plots should be filed as a bug. Can somebody confirm this?

dsejas gravatar imagedsejas ( 2019-05-21 03:47:27 +0200 )edit