1

the functions below each plot a single numpy array
plot1D, plot2D, and plot3D take arrays with 1, 2, and 3 columns, respectively

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D


def plot1D(data):
    x=np.arange(len(data))
    plot2D(np.hstack((np.transpose(x), data)))

def plot2D(data):
    # type: (object) -> object
    #if 2d, make a scatter
    plt.plot(data[:,0], data[:,1], *args, **kwargs)

def plot3D(data):
    #if 3d, make a 3d scatter
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot(data[:,0], data[:,1], data[:,2], *args, **kwargs)

I would like the ability to input a list of 1, 2, or 3d arrays and plot all arrays from the list onto one figure

I have added the looping elements, but am unsure how hold a figure and add additional plots...

def plot1D_list(data):
    for i in range(0, len(data)):
        x=np.arange(len(data[i]))
        plot2D(np.hstack((np.transpose(x), data[i])))

def plot2D_list(data):
    # type: (object) -> object
    #if 2d, make a scatter
    for i in range(0, len(data)):
        plt.plot(data[i][:,0], data[i][:,1], *args, **kwargs)

def plot3D_list(data):
    #if 3d, make a 3d scatter
    for i in range(0, len(data)):
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.plot(data[i][:,0], data[i][:,1], data[i][:,2], *args, **kwargs)

1 Answer 1

1

To plot multiple data sets on the same axes, you can do something like this:

def plot2D_list(data,*args,**kwargs):
    # type: (object) -> object
    #if 2d, make a scatter
    n = len(data)
    fig,ax = plt.subplots() #create figure and axes
    for i in range(n):
        #now plot data set i
        ax.plot(data[i][:,0], data[i][:,1], *args, **kwargs)

Your other functions can be generalised in the same way. Here's an example of using the above function with a 5 sets of randomly generated x-y coordinates, each with length 100 (each of the 5 data sets appears as a different color):

import numpy as np

X = np.random.randn(5,100,2)
plot2D_list(X,'o')
plt.show()

enter image description here

4
  • thanks! perhaps my original question should have been more clear: I would like all data visualizations overlayed onto a single plot in a single figure. Your solution gives multiple plote (one for each item), with all plots appearing on the same figure
    – user80112
    Commented Oct 7, 2016 at 16:17
  • Ah...you meant plot on same axes not same figure Commented Oct 7, 2016 at 17:29
  • yes! sorry.. can you help with this also? It seems like it would just be a very small variation on the answer you provided but I keep getting errors still..
    – user80112
    Commented Oct 7, 2016 at 17:51
  • There you go, it's updated to explain how to plot multiple data sets on the same axes. Commented Oct 8, 2016 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.