How to put multiple marks on the same line chart

Viewed 35

I am currently trying to plot with multiple marks on the same line plot using matplotlib library, my code is as follows

import random

def is_prime(n):
  for i in range(2,n):
    if (n%i) == 0:
      return False
  return True

a=[]
b=[]
prime=[]
for i in range(20):
  prime.append(is_prime(i))
  numero = random.randint(1, 100)
  a.append(numero)
  b.append(i)
print(a)
print(b)
print(prime)

plt.plot(b, a,'go--', linewidth=2, markersize=9,  marker = 'o', mfc = 'b')
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict = font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()

I want my chart mark to change depending on prime array if true put a different marker to when false.

1 Answers

I'd recommend using Seaborn as it makes it very easy to change markers based on criteria by using its built in style feature:

import random
import matplotlib.pyplot as plt
import seaborn as sns

def is_prime(n):
    for i in range(2,n):
        if (n%i) == 0:
            return False
        return True

a=[]
b=[]
prime=[]
for i in range(20):
    prime.append(is_prime(i))
    numero = random.randint(1, 100)
    a.append(numero)
    b.append(i)
print(a)
print(b)
print(prime)

plt.plot(b, a,'--', linewidth=2, c = 'g', zorder=0)
sns.scatterplot(x=b, y=a, style=prime, s=200, legend=False, markers=['o', 's'])

font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict = font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()

Graph:

enter image description here

If not, then you will need to create 2 scatter plots and place them on top of your line plot, then cycle through those points assigning a marker type based on if they were prime or not:

import random

def is_prime(n):
    for i in range(2,n):
        if (n%i) == 0:
            return False
        return True

a=[]
b=[]
prime=[]
for i in range(20):
    prime.append(is_prime(i))
    numero = random.randint(1, 100)
    a.append(numero)
    b.append(i)
print(a)
print(b)
print(prime)

plt.plot(b, a,'--', linewidth=2, c = 'g', zorder=0)

for pos, value in enumerate(prime):
    if value:
        plt.scatter(b[pos], a[pos], marker='o', c='b', s=100)
    elif value==False:
        plt.scatter(b[pos], a[pos], marker='s', c='b', s=100)


font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict = font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()

Graph:

enter image description here

Related