1
votes

In an offset contour plot (like on the left in the image below):

Example of offset contour plot

I have been trying to add horizontal and vertical axis in the X-Z and Y-Z planes over the offset contour plots (like in the right image). Notice that the horizontal & vertical axis go all the way out to the xlim, ylim & zlim extents and dont stop at the end of the main data plot.

Many of you will recognize this demo as the matplotlib3d contour3d_demo3. My code for now is the exact same.

The bit of code for plotting the offset 2D contour plot in the Y-Z plane is:

cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)

In 2D plots I would just use axhline(0, color='black'). Unfortunately axhline/axvline don't have an offset option that functions like the ax.contour does as far as I can tell. I can kind of get what I'm looking for playing tricks like zeroing out the Z data to get a horizontal line:

cset = ax.contour(X, Y, 0*Z, zdir='x', offset=-40, cmap=cm.coolwarm)

But the problem is that the line stops at the edge of the data. I want to be able to arbitrarily set my xlim, ylim & zlim and get horizontal & vertical axis that span those ranges. Any ideas on a way to do this?

1

1 Answers

1
votes

It's simple, just draw them yourself! Adapting the code from the example, add the following lines:

import numpy as np
XL = np.linspace(X.min(),X.max(),100)
YL = np.linspace(Y.min(),Y.max(),100)
ZL = np.linspace(Z.min(),Z.max(),100)

ax.plot(XL,0*XL,-100,zdir='z',color='k',lw=5)
ax.plot(0*YL,YL,-100,zdir='z',color='k',lw=5)

ax.plot(YL,0*YL,-40,zdir='x',color='k',lw=5)
ax.plot(0*ZL,ZL,-40,zdir='x',color='k',lw=5)

ax.plot(XL,0*XL,40,zdir='y',color='k',lw=5)
ax.plot(0*ZL,ZL,40,zdir='y',color='k',lw=5)

enter image description here

The trick is that ax.plot in 3D has extra arguments:

plot(self, xs, ys, *args, **kwargs) method of matplotlib.axes.Axes3DSubplot instance
    Plot 2D or 3D data.

    ==========  ================================================
    Argument    Description
    ==========  ================================================
    *xs*, *ys*  X, y coordinates of vertices
    *zs*        z value(s), either one for all points or one for each point.
    *zdir*      Which direction to use as z ('x', 'y' or 'z') when plotting a 2D set.
    ==========  ================================================

    Other arguments are passed on to
    :func:`~matplotlib.axes.Axes.plot`