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()