change markersize in legend matplotlib

Solutions on MaxInterview for change markersize in legend matplotlib by the best coders in the world

showing results for - "change markersize in legend matplotlib"
Vincenzo
13 Oct 2017
1lgnd = plt.legend(loc="lower left", scatterpoints=1, fontsize=10)
2lgnd.legendHandles[0]._sizes = [30]
3lgnd.legendHandles[1]._sizes = [30]
Till
15 Apr 2016
1import matplotlib.pyplot as plt
2import numpy as np
3
4def rand_data():
5    return np.random.uniform(low=0., high=1., size=(100,))
6
7# Generate data.
8x1, y1 = [rand_data() for i in range(2)]
9x2, y2 = [rand_data() for i in range(2)]
10
11plt.figure()
12plt.plot(x1, y1, 'o', label='first', markersize=np.sqrt(20.), c='b')
13plt.plot(x2, y2, 'o', label='second', markersize=np.sqrt(35.), c='r')
14# Plot legend.
15lgnd = plt.legend(loc="lower left", numpoints=1, fontsize=10)
16
17#change the marker size manually for both lines
18lgnd.legendHandles[0]._legmarker.set_markersize(6)
19lgnd.legendHandles[1]._legmarker.set_markersize(6)
20plt.show()
21