ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 10 Oct 2016 09:57:49 +0200Axes labels on 3D plots?https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/I know you can label the axes on 2d plots, but what about on 3d plots, obtained with plot3d? I've hunted around in the documentation, but have found nothing. Or can this be done by first creating a graphics rectangular parallelepiped, appropriately labelled, and inserting the plot3d (plotted without labels) into it? Or some other way?Sun, 27 Mar 2016 10:55:40 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/Answer by eric_g for <p>I know you can label the axes on 2d plots, but what about on 3d plots, obtained with plot3d? I've hunted around in the documentation, but have found nothing. Or can this be done by first creating a graphics rectangular parallelepiped, appropriately labelled, and inserting the plot3d (plotted without labels) into it? Or some other way?</p>
https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?answer=32896#post-id-32896Hi,
To my knowledge, there is indeed no axes label fonctionality for 3d plots. For the SageManifolds project, we set up some workaround function that uses `text3d` to set labels at locations determined from the bounding box of the graphic object. Here is this function:
def set_axes_labels(graph, xlabel, ylabel, zlabel, **kwds):
r"""
Set axes labels for a 3D graphics object.
This is a workaround for the lack of axes labels in Sage 3D plots; it
sets the labels as text3d objects at locations determined from the
bounding box of the graphic object ``graph``.
INPUT:
- ``graph`` -- a 3D graphic object, as an instance of
:class:`~sage.plot.plot3d.base.Graphics3d`
- ``xlabel`` -- string for the x-axis label
- ``ylabel`` -- string for the y-axis label
- ``zlabel`` -- string for the z-axis label
- ``**kwds`` -- options (e.g. color) for text3d
OUTPUT:
- the 3D graphic object with text3d labels added.
EXAMPLE::
sage: g = sphere()
sage: print g
Graphics3d Object
sage: show(g) # no axes labels
sage: from sage.manifolds.utilities import set_axes_labels
sage: ga = set_axes_labels(g, 'X', 'Y', 'Z', color='red')
sage: print ga
Graphics3d Object
sage: show(ga) # the 3D frame has now axes labels
"""
from sage.plot.plot3d.shapes2 import text3d
xmin, ymin, zmin = graph.bounding_box()[0]
xmax, ymax, zmax = graph.bounding_box()[1]
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
x1 = xmin + dx / 2
y1 = ymin + dy / 2
z1 = zmin + dz / 2
xmin1 = xmin - dx / 20
xmax1 = xmax + dx / 20
ymin1 = ymin - dy / 20
zmin1 = zmin - dz / 20
graph += text3d(' ' + xlabel, (x1, ymin1, zmin1), **kwds)
graph += text3d(' ' + ylabel, (xmax1, y1, zmin1), **kwds)
graph += text3d(' ' + zlabel, (xmin1, ymin1, z1), **kwds)
return graph
Sun, 27 Mar 2016 23:48:15 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?answer=32896#post-id-32896Comment by eric_g for <p>Hi,
To my knowledge, there is indeed no axes label fonctionality for 3d plots. For the SageManifolds project, we set up some workaround function that uses <code>text3d</code> to set labels at locations determined from the bounding box of the graphic object. Here is this function:</p>
<pre><code>def set_axes_labels(graph, xlabel, ylabel, zlabel, **kwds):
r"""
Set axes labels for a 3D graphics object.
This is a workaround for the lack of axes labels in Sage 3D plots; it
sets the labels as text3d objects at locations determined from the
bounding box of the graphic object ``graph``.
INPUT:
- ``graph`` -- a 3D graphic object, as an instance of
:class:`~sage.plot.plot3d.base.Graphics3d`
- ``xlabel`` -- string for the x-axis label
- ``ylabel`` -- string for the y-axis label
- ``zlabel`` -- string for the z-axis label
- ``**kwds`` -- options (e.g. color) for text3d
OUTPUT:
- the 3D graphic object with text3d labels added.
EXAMPLE::
sage: g = sphere()
sage: print g
Graphics3d Object
sage: show(g) # no axes labels
sage: from sage.manifolds.utilities import set_axes_labels
sage: ga = set_axes_labels(g, 'X', 'Y', 'Z', color='red')
sage: print ga
Graphics3d Object
sage: show(ga) # the 3D frame has now axes labels
"""
from sage.plot.plot3d.shapes2 import text3d
xmin, ymin, zmin = graph.bounding_box()[0]
xmax, ymax, zmax = graph.bounding_box()[1]
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
x1 = xmin + dx / 2
y1 = ymin + dy / 2
z1 = zmin + dz / 2
xmin1 = xmin - dx / 20
xmax1 = xmax + dx / 20
ymin1 = ymin - dy / 20
zmin1 = zmin - dz / 20
graph += text3d(' ' + xlabel, (x1, ymin1, zmin1), **kwds)
graph += text3d(' ' + ylabel, (xmax1, y1, zmin1), **kwds)
graph += text3d(' ' + zlabel, (xmin1, ymin1, z1), **kwds)
return graph
</code></pre>
https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=34999#post-id-34999The function `set_axes_labels` has been included in SageMath 7.3: to get it, it suffices to type
from sage.manifolds.utilities import set_axes_labelsFri, 30 Sep 2016 17:08:11 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=34999#post-id-34999Comment by Caterpillar for <p>Hi,
To my knowledge, there is indeed no axes label fonctionality for 3d plots. For the SageManifolds project, we set up some workaround function that uses <code>text3d</code> to set labels at locations determined from the bounding box of the graphic object. Here is this function:</p>
<pre><code>def set_axes_labels(graph, xlabel, ylabel, zlabel, **kwds):
r"""
Set axes labels for a 3D graphics object.
This is a workaround for the lack of axes labels in Sage 3D plots; it
sets the labels as text3d objects at locations determined from the
bounding box of the graphic object ``graph``.
INPUT:
- ``graph`` -- a 3D graphic object, as an instance of
:class:`~sage.plot.plot3d.base.Graphics3d`
- ``xlabel`` -- string for the x-axis label
- ``ylabel`` -- string for the y-axis label
- ``zlabel`` -- string for the z-axis label
- ``**kwds`` -- options (e.g. color) for text3d
OUTPUT:
- the 3D graphic object with text3d labels added.
EXAMPLE::
sage: g = sphere()
sage: print g
Graphics3d Object
sage: show(g) # no axes labels
sage: from sage.manifolds.utilities import set_axes_labels
sage: ga = set_axes_labels(g, 'X', 'Y', 'Z', color='red')
sage: print ga
Graphics3d Object
sage: show(ga) # the 3D frame has now axes labels
"""
from sage.plot.plot3d.shapes2 import text3d
xmin, ymin, zmin = graph.bounding_box()[0]
xmax, ymax, zmax = graph.bounding_box()[1]
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
x1 = xmin + dx / 2
y1 = ymin + dy / 2
z1 = zmin + dz / 2
xmin1 = xmin - dx / 20
xmax1 = xmax + dx / 20
ymin1 = ymin - dy / 20
zmin1 = zmin - dz / 20
graph += text3d(' ' + xlabel, (x1, ymin1, zmin1), **kwds)
graph += text3d(' ' + ylabel, (xmax1, y1, zmin1), **kwds)
graph += text3d(' ' + zlabel, (xmin1, ymin1, z1), **kwds)
return graph
</code></pre>
https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=35076#post-id-35076@eric_g could you please provide a working example of set_axes_labels usage? ThxWed, 05 Oct 2016 15:06:34 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=35076#post-id-35076Comment by eric_g for <p>Hi,
To my knowledge, there is indeed no axes label fonctionality for 3d plots. For the SageManifolds project, we set up some workaround function that uses <code>text3d</code> to set labels at locations determined from the bounding box of the graphic object. Here is this function:</p>
<pre><code>def set_axes_labels(graph, xlabel, ylabel, zlabel, **kwds):
r"""
Set axes labels for a 3D graphics object.
This is a workaround for the lack of axes labels in Sage 3D plots; it
sets the labels as text3d objects at locations determined from the
bounding box of the graphic object ``graph``.
INPUT:
- ``graph`` -- a 3D graphic object, as an instance of
:class:`~sage.plot.plot3d.base.Graphics3d`
- ``xlabel`` -- string for the x-axis label
- ``ylabel`` -- string for the y-axis label
- ``zlabel`` -- string for the z-axis label
- ``**kwds`` -- options (e.g. color) for text3d
OUTPUT:
- the 3D graphic object with text3d labels added.
EXAMPLE::
sage: g = sphere()
sage: print g
Graphics3d Object
sage: show(g) # no axes labels
sage: from sage.manifolds.utilities import set_axes_labels
sage: ga = set_axes_labels(g, 'X', 'Y', 'Z', color='red')
sage: print ga
Graphics3d Object
sage: show(ga) # the 3D frame has now axes labels
"""
from sage.plot.plot3d.shapes2 import text3d
xmin, ymin, zmin = graph.bounding_box()[0]
xmax, ymax, zmax = graph.bounding_box()[1]
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
x1 = xmin + dx / 2
y1 = ymin + dy / 2
z1 = zmin + dz / 2
xmin1 = xmin - dx / 20
xmax1 = xmax + dx / 20
ymin1 = ymin - dy / 20
zmin1 = zmin - dz / 20
graph += text3d(' ' + xlabel, (x1, ymin1, zmin1), **kwds)
graph += text3d(' ' + ylabel, (xmax1, y1, zmin1), **kwds)
graph += text3d(' ' + zlabel, (xmin1, ymin1, z1), **kwds)
return graph
</code></pre>
https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=35106#post-id-35106You have one in the EXAMPLE section in the code displayed above.Mon, 10 Oct 2016 09:57:49 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=35106#post-id-35106Comment by Alasdair for <p>Hi,
To my knowledge, there is indeed no axes label fonctionality for 3d plots. For the SageManifolds project, we set up some workaround function that uses <code>text3d</code> to set labels at locations determined from the bounding box of the graphic object. Here is this function:</p>
<pre><code>def set_axes_labels(graph, xlabel, ylabel, zlabel, **kwds):
r"""
Set axes labels for a 3D graphics object.
This is a workaround for the lack of axes labels in Sage 3D plots; it
sets the labels as text3d objects at locations determined from the
bounding box of the graphic object ``graph``.
INPUT:
- ``graph`` -- a 3D graphic object, as an instance of
:class:`~sage.plot.plot3d.base.Graphics3d`
- ``xlabel`` -- string for the x-axis label
- ``ylabel`` -- string for the y-axis label
- ``zlabel`` -- string for the z-axis label
- ``**kwds`` -- options (e.g. color) for text3d
OUTPUT:
- the 3D graphic object with text3d labels added.
EXAMPLE::
sage: g = sphere()
sage: print g
Graphics3d Object
sage: show(g) # no axes labels
sage: from sage.manifolds.utilities import set_axes_labels
sage: ga = set_axes_labels(g, 'X', 'Y', 'Z', color='red')
sage: print ga
Graphics3d Object
sage: show(ga) # the 3D frame has now axes labels
"""
from sage.plot.plot3d.shapes2 import text3d
xmin, ymin, zmin = graph.bounding_box()[0]
xmax, ymax, zmax = graph.bounding_box()[1]
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
x1 = xmin + dx / 2
y1 = ymin + dy / 2
z1 = zmin + dz / 2
xmin1 = xmin - dx / 20
xmax1 = xmax + dx / 20
ymin1 = ymin - dy / 20
zmin1 = zmin - dz / 20
graph += text3d(' ' + xlabel, (x1, ymin1, zmin1), **kwds)
graph += text3d(' ' + ylabel, (xmax1, y1, zmin1), **kwds)
graph += text3d(' ' + zlabel, (xmin1, ymin1, z1), **kwds)
return graph
</code></pre>
https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=32897#post-id-32897Many thanks for your detailed reply. I assumed there was no axes labelling functionality (or I'd have found it), but still it's nice to be confirmed. Maybe somebody cleverer (and with more time) than me can implement it some time...Mon, 28 Mar 2016 02:39:46 +0200https://ask.sagemath.org/question/32890/axes-labels-on-3d-plots/?comment=32897#post-id-32897