Home > Net >  Matplotlib - Series of curves on with individual markers
Matplotlib - Series of curves on with individual markers

Time:10-29

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().

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()

CodePudding user response:

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

  • Related