I am plotting an array A
which has len(A)=2
. I want to plot A[0],A[1]
with separate colors on the same graph, not a single color as shown in the current output and also add legends for each A[0],A[1]
.
import numpy as np
import matplotlib.pyplot as plt
A=np.array([[[0.02109 ],
[0.02109 ],
[0.0201082 ],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02055473],
[0.02055797],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]],
[[0.02109 ],
[0.02109 ],
[0.01127196],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.01573726],
[0.01576968],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]]])
plt.title("Line graph")
for i in range(0,len(A)):
plt.plot(A[i], color="red", ls='', marker='o')
plt.xlabel('Node number')
plt.ylabel('IFT (mN/m)')
The current output is
CodePudding user response:
If I am understating well your question, I would suggest to use plot.scatter
, and also to remove the loop for plotting (which is faster in python if your A
array is bigger), such as the following (vec_x
is a vector with the corresponding index according to the shape of your matrix):
vec_x = np.arange(np.shape(A)[1])
plt.scatter(vec_x, A[0], c='r', label='A[0]')
plt.scatter(vec_x, A[1], c='b', label='A[1]')
plt.legend()
CodePudding user response:
You can make a list of color and assign it for A[i] in for loop. And for legend you need to add label, If you can also do this like the previous one but I did it like this label= A[i]
so final code should be:
import numpy as np
import matplotlib.pyplot as plt
A=np.array([[[0.02109 ],
[0.02109 ],
[0.0201082 ],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02055473],
[0.02055797],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]],
[[0.02109 ],
[0.02109 ],
[0.01127196],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.01573726],
[0.01576968],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]]])
color_dot = ["blue", "red"]
plt.title("Line graph")
for i in range(0,len(A)):
plt.plot(A[i], color= color_dot[i], ls='', marker='o', label= A[i])
plt.legend()
plt.xlabel('Node number')
plt.ylabel('IFT (mN/m)')
CodePudding user response:
You can try to draw plots separately:
import numpy as np
import matplotlib.pyplot as plt
A=np.array([[[0.02109 ],
[0.02109 ],
[0.0201082 ],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02055473],
[0.02055797],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]],
[[0.02109 ],
[0.02109 ],
[0.01127196],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.01573726],
[0.01576968],
[0.02109 ],
[0.02109 ],
[0.02109 ],
[0.02109 ]]])
plt.title("Line graph")
for i in range(0,len(A[0])):
plt.plot(i, A[0][i], color = 'red', ls='', marker='o')
plt.plot(i, A[1][i], color = 'blue', ls='', marker='o')
plt.xlabel('Node number')
plt.ylabel('IFT (mN/m)')
plt.legend([1, 2])
plt.show()