7

I have quite complex 3D data that I want to plot using matplotlib. To be able to get a better understanding I want to plot the same data on different subplots, with just different view_init in each plot. At the moment, my code looks something along the lines:

fig = plt.figure()
views = [(0, 0), (90, 0), (0, 90)]
for i (elev, azim) in enumerate(views):
    ax = fig.add_subplot(1, 3, i+1, projection='3d')
    plot_heavy_data(ax)  # expensive call
    ax.view_init(elev, azim)

however, my call to plot_heavy_data is quite expensive and I was wondering if there is a way of just "copying" a subplot and plotting it with changing just the view direction, but keeping everything else the same. That way I would only do the expensive operation once instead of repeating it 3 times.

clarification: The function plot_heavy_data is ONLY plotting to the subplot, something like this:

def plot_heavy_data(ax):
    ax.scatter(*data)  # data is huge

I would like to avoid calling this function multiple times.

The following would be a minimal working sample where I would like to call the plot function only once.

import matplotlib.pyplot as plt
import numpy as np

def plot(ax, data):
    # ideally only call this function once
    ax.scatter(*data)

data = np.random.random((2, 20))

fig = plt.figure(figsize=(12, 4))

for i in range(3):
    ax = fig.add_subplot(1, 3, i+1)
    plot(ax, data)
    ax.set_xlim([0, i+1])  # do 'some' manipulation on ax

plt.show()
juqa
  • 105
  • 7
  • 1
    Not sure it's 100% dupe, but could help - https://stackoverflow.com/questions/45810557/pyplot-copy-an-axes-content-and-show-it-in-a-new-figure/45812071#45812071 – DavidG Nov 15 '18 at 11:52
  • It's going to be difficult to say without saying more about your `plot_heavy_data()` function. Is it just a plotting function? Is it doing some data manipulation? If it's the later, I suspect you could break it down in two, one function doing the data manipulation once and then use the result to plot multiple times. If it is the former, then it seems plausible that you could create a copy of all the `Artist`s in one axes and put them in an other axes, but we'd need to have a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) to test it out – Diziet Asahi Nov 15 '18 at 11:52
  • @DizietAsah I added a minimal working sample – juqa Nov 15 '18 at 13:10
  • 1
    This assumes that you have already factored out any computational parts from the `plot_heavy_data`: What makes plotting expensive is not the function call itself, but *drawing* the actual content. This means even if you were able to copy some artists from one axes to another, the net gain is close to zero, because the same content would still need to be drawn on a different place on the canvas. One may use blitting though to blit (instead of draw) some content on the canvas. This is however only possible for the exact same image, not for a rotated version of it. – ImportanceOfBeingErnest Nov 15 '18 at 15:02
  • We could close this as duplicate of the linked question, or I could provide the above as answer, not sure which one is better suited here. – ImportanceOfBeingErnest Nov 15 '18 at 15:09
  • 1
    @ImportanceOfBeingErnest thanks for the thoughts, I do not think that the questions is a duplicate to the linked one. I guess you could write your comment, that what I want to achieve is *not* possible, as an answer, so I can accept it. I was just hoping that there would be a good way to re-render a plot from a different perspective as, when using a single plot, you can move the axis around quite quickly. – juqa Nov 16 '18 at 06:56

0 Answers0