johnhenry
johnhenry

Reputation: 1343

multiple graphs from a loop in one single plot - Python

My program produces two arrays and I have to plot one of them in the X axis and the other one on the Y axis (the latter are taken from the row of a matrix).

The problem is that I have to repeat this operation for a number of times (I am running a loop) but all the graphs should be on the same plot. Every time the dots should be of a different colour. Then I should save the file.

I have tried with

for row in range(6):
    plt.plot(betaArray, WabArray[row], 'ro')
    plt.show()

but this only shows one plot each for every iteration and always of the same colour.

Upvotes: 0

Views: 9036

Answers (1)

cel
cel

Reputation: 31379

You could try something like this:

import numpy as np
import matplotlib.pylab as plt
import matplotlib as mpl

x = [1,2,3,4]
y_mat = np.array([[1,2,3,4], [5,6,7,8]])

n, _ = y_mat.shape

colors = mpl.cm.rainbow(np.linspace(0, 1, n))
fig, ax = plt.subplots()
for color, y in zip(colors, y_mat):
    ax.scatter(x, y, color=color)
plt.show()

This creates n colors from the rainbow color map and uses scatter to plot the points in the respective color. You may want to switch to a different color map or even choose the colors manually.

This is the result:

plot6

Upvotes: 3

Related Questions