Skip to content
Advertisement

Plotting different shape 2D arrays in matplotlib

I couldn’t find any answer to this specific case. Say I want to create a scatter plot from the following data:

y = np.array([[1,2,3,6,5,3,4,2],[1,2,4,5,5,3,3,2]])
X = np.array([[86],[82]])

Where all the values of y[0] are the correseponding values of X[0] (86) and so on. I know I can just use numpy repeat function but I thought maybe there’s any other more feasilbe option? I’ve tried to reshape the any of the arrays but it didn’t help. Any ideas?

Advertisement

Answer

I really like the tile function from numpy. I dont think there is a way around of “enlarging” X[i] to make it match the length of y[i]

import numpy as np
import matplotlib.pyplot as plt

y = np.array([[1,2,3,6,5,3,4,2],[1,2,4,5,5,3,3,2]])
X = np.array([[86],[82]])

for i in range(len(X)):
    plt.plot(np.tile(X[i], len(y[i])), y[i], '.')
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement