6

I have the following question about a Matplotlib plot.

I am plotting data from different experiment as scatter plot; each of the set of data has its own marker and color. I would like to have them grouped in one single line of the legend because for me they have all the same "meaning". Es. Let's say I have 3 set of data from 3 research group:

plt.plot(group1, marker='^', c='r', label='groupdata')
plt.plot(group2, marker='o', c='b', label='groupdata')
plt.plot(group3, marker='s', c='g', label='groupdata')

I'd like to have a single line in legend that shows:

^ o s = groupdata

The best way to show you what I mean is this awfull drawing ;D

enter image description here

As suggested, a working example; as you can see I got 3 lines in the legend, all the data are named 'groupdata'; I'd like to know if it is possible to group them under the same legend line.

import matplotlib.pyplot as plt
import numpy as np
group1 = np.array([[1,4,6],[3,2,5]])
group2 = np.array([[1,5,9],[2,2,5]])
group3 = np.array([[1,4,2],[11,2,7]])
plt.plot(group1[0,:],group1[1,:], 'ro', marker='^', label='groupdata')
plt.plot(group2[0,:],group2[1,:], 'bo', marker='o', label='groupdata')
plt.plot(group3[0,:],group3[1,:], 'go', marker='s', label='groupdata')
plt.legend()
plt.show()

Thanks for your help

Giovanni Frison
  • 628
  • 3
  • 19
  • Although your question is clear and well-posed, you could extend you code by a few line to get a minimal working example. That would also show that/how you get stuck. – Tom de Geus Mar 04 '19 at 09:14
  • 1
    Dear @TomdeGeus, thanks for your suggestion. Post updated – Giovanni Frison Mar 04 '19 at 09:27
  • Check [How to create two legend objects for a single plot instance?](https://stackoverflow.com/questions/52038171/how-to-create-two-legend-objects-for-a-single-plot-instance). Possibly of interest for more sophisticated stuff with scatters would be [this one](https://stackoverflow.com/questions/49297599/why-doesnt-the-color-of-the-points-in-a-scatter-plot-match-the-color-of-the-poi). – ImportanceOfBeingErnest Mar 04 '19 at 11:05

1 Answers1

6

Found it!

import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple
import numpy as np
group1 = np.array([[1,4,6],[3,2,5]])
group2 = np.array([[1,5,9],[2,2,5]])
group3 = np.array([[1,4,2],[11,2,7]])
a, =plt.plot(group1[0,:],group1[1,:], 'ro', marker='^')
b, =plt.plot(group2[0,:],group2[1,:], 'bo', marker='o')
c, =plt.plot(group3[0,:],group3[1,:], 'go', marker='s')
plt.legend([(a,b,c)], ['goupdata'], numpoints=1, handler_map={tuple: HandlerTuple(ndivide=None)})
plt.show()

enter image description here

Thanks very much to anyone that at least tried to help!

Update: Something that i found useful; If you want to add more than one entries:

plt.legend([(a,b),(c)], ['goupdata1', 'groupdata2'], numpoints=1, handler_map={tuple: HandlerTuple(ndivide=None)})
Giovanni Frison
  • 628
  • 3
  • 19
  • Nice! If I understand correctly: To make your example cleaner I guess you should remove the `label` options, are you are now overwriting them. – Tom de Geus Mar 04 '19 at 11:46