Ask Your Question
3

Axes labels on 3D plots?

asked 2016-03-27 10:55:40 +0100

Alasdair gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-03-27 23:48:15 +0100

eric_g gravatar image

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 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
edit flag offensive delete link more

Comments

1

Many 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...

Alasdair gravatar imageAlasdair ( 2016-03-28 02:39:46 +0100 )edit

The 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_labels
eric_g gravatar imageeric_g ( 2016-09-30 17:08:11 +0100 )edit

@eric_g could you please provide a working example of set_axes_labels usage? Thx

Caterpillar gravatar imageCaterpillar ( 2016-10-05 15:06:34 +0100 )edit

You have one in the EXAMPLE section in the code displayed above.

eric_g gravatar imageeric_g ( 2016-10-10 09:57:49 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2016-03-27 10:55:40 +0100

Seen: 3,179 times

Last updated: Mar 27 '16