Matplotlib change marker border color AND custom marker styles

6.1k views Asked by At

I am trying to plot a scatter plot in Matplotlib using colors from a list of colors. The code is here:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
from pylab import *
from random import shuffle
rcParams['legend.numpoints'] = 1

df = pd.DataFrame(np.random.rand(10,28),columns=list('ABCDEFGHIJKLMNOPQRSTUVWXYZab'))

markers = []
for m in Line2D.markers:
    try:
        if len(m) == 1 and m != ' ':
            markers.append(m)
    except TypeError:
        pass
cust = [r'$\lambda$',r'$\bowtie$',r'$\circlearrowleft$',r'$\clubsuit$',r'$\checkmark$',r'$\spadesuit$']
styles = markers + cust
shuffle(styles)

color=['magenta','blue','green','none','brown','red']

for i,column in enumerate(df.iloc[:,1:7]):
    plt.plot(df['A'], df.iloc[:,i], linestyle='None', marker=styles[i], markersize=10, label=column, mfc=color[i], markeredgewidth=1.5)
plt.legend()
plt.show()

The output is attached here.enter image description here

The problem is that the order of colors in the legend is not the same as the order of colors in the list.

I am expecting the following order of colors: - magenta, blue, green, none, brown, red

Questions:

  1. The order of colors in the plot is not the same as what I specified in the list. Is it possible to fix the code so that the colors are in the same order? (NOTE: If the marker has no face, then I want the marker edge to have the color from the list)
  2. From the custom marker list cust = [r'$\lambda$',r'$\bowtie$',r'$\circlearrowleft$',r'$\clubsuit$',r'$\checkmark$',r'$\spadesuit$'], are there more options similar to this? i.e. how can I see a list of similar markers?

EDIT: Here is the resulting plot by commenting out the list of marker styles [i.e. I commented out this line: shuffle(styles)]. enter image description here

0

There are 0 answers