I'm trying to add a method to the legend class in matplotlib. I've read the following answers here. and I'm basically trying to refactor the answer found here into a reusable method.
I'm not quite sure if I am doing this correctly, but I would like my end result to look something like this:
import matplotlib.pyplot as plt
from matplotlib.pyplot import legend
import pandas as pd
import numpy as np
def set_marker_legend(self, size):
for handle in lgnd.legendHandles:
handle._legmarker.set_markersize(size)
# Adding method to legend class
legend.set_marker_legend = set_marker_legend
x = np.arange(1, 11, 1)
y = x**2
group = np.repeat(['case1','case2'], [5,5], axis=0)
dat = pd.DataFrame({"x":x, "y":y, "group":group})
colors = {'case1':'black', 'case2':'red'}
grouped = dat.groupby('group')
for key, group in grouped:
plt.plot(group.x, group.y,
markersize=3, marker='o',
linestyle='None', color=colors[key])
# What I would like to be able to do
plt.legend(labels=['Case 1', 'Case 2']).set_marker_legend(8)
plt.show()