Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Matplotlib – Series of curves on with individual markers

I am trying to plot a series of curves on the same graph with individual markers:

  • Each curve has one colour
  • Each data point has its own markers

For this, I created 3 lists of lists: x_data, y_data and markers. Using np.array(), x_data and y_data can be plotted properly as different curves (with individual colours).

However, np.array() cannot be used with the attribute marker and I do not know how to pass markers to ax.plot().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does someone know how to attribute individual markers?

MWE

import matplotlib.pyplot as plt
import numpy as np

x_data=[[1,2,3,4,5],[5,10,3,8,6]]
y_data=[[5,10,3,8,6],[1,2,3,4,5]]
markers=[["o","+","D","+","D"],["D","o","o","D","+"]]

fig, ax = plt.subplots()

for n in range(0,len(x_data)):
    ax.plot(np.array(x_data[n]), np.array(y_data[n]),linewidth=1,marker=np.array(markers))

plt.show()

>Solution :

One option is an inner loop to scatter plot each marker individually:

import matplotlib.pyplot as plt
import numpy as np

x_data=[[1,2,3,4,5],[5,10,3,8,6]]
y_data=[[5,10,3,8,6],[1,2,3,4,5]]
markers=[["o","+","D","+","D"],["D","o","o","D","+"]]

fig, ax = plt.subplots()

for xs, ys, markers in zip(x_data, y_data, markers):
    line = ax.plot(xs, ys, linewidth=1)
    colour = line[0].get_color()
    for x, y, marker in zip(xs, ys, markers):
        ax.scatter(x, y, marker=marker, color=colour)
plt.show()

Output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading