Home > front end >  Matplotlib: how to plot with filled and unfilled marker alternate one by one?
Matplotlib: how to plot with filled and unfilled marker alternate one by one?

Time:08-21

I am plotting with a set of data and every m points are a subset. I would like to use filled marker and hollow marker alternatively for each subset, i.e. if i equals even numbers the marker is filled, and if i equals odd numbers the marker is unfilled. I could accomplish this by nesting if statement in the for loop. But I am wondering whether I could define a list for the markerfacecolor and simply use markerfacecolor=mfclist[i] in the ax.plot command like ax.plot(x[i*m:(i 1)*m], y[i*m:(i 1)*m], linestyle='none', marker='o', color=colorlist[i], markerfacecolor=mfclist[i]). The list value for the hollow marker would be 'none', but I do not know how to set the list value for the filled marker.

import numpy as np
import matplotlib.pyplot as plt

m, n = 3, 4
x = np.arange(0, m*n)
y = np.random.rand(m*n)
colorlist = ['red', 'blue', 'black', 'green']
print(x, y)

fig, ax = plt.subplots(dpi = 100)
for i in range(0, n):
    ax.plot(x[i*m:(i 1)*m], y[i*m:(i 1)*m], linestyle='none', marker='o', color=colorlist[i])

CodePudding user response:

This code can do that.

...

colorlist = ['red', 'blue', 'black', 'green']
mfclist = colorlist
mfclist = [x if i % 2 == 0 else "None" for i,x in enumerate(mfclist)]
...

for i in range(0, n):
    ax.plot(x[i*m:(i 1)*m], y[i*m:(i 1)*m], markerfacecolor=mfclist[i],linestyle='none', marker='o', color=colorlist[I])
  • Related