0

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

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.