Python Matplotlib Multi-color Legend Entry

2020-07-09 06:39发布

I would like to make a legend entry in a matplotlib look something like this:

enter image description here

It has multiple colors for a given legend item. Code is shown below which outputs a red rectangle. I'm wondering what I need to do to overlay one color ontop of another? Or is there a better solution?

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

red_patch = mpatches.Patch(color='red', label='Foo')
plt.legend(handles=[red_patch])

plt.show()

2条回答
等我变得足够好
2楼-- · 2020-07-09 06:59

The solution I am proposing is to combine two different proxy-artists for one entry legend, as described here: Combine two Pyplot patches for legend.

The strategy is then to set the fillstyle of the first square marker to left while the other one is set to right (see http://matplotlib.org/1.3.0/examples/pylab_examples/filledmarker_demo.html). Two different colours can then be attributed to each marker in order to produce the desired two-colour legend entry.

The code below show how this can be done. Note that the numpoints=1 argument in plt.legend is important in order to display only one marker for each entry.

import matplotlib.pyplot as plt

plt.close('all')

#---- Generate a Figure ----

fig = plt.figure(figsize=(4, 4))
ax = fig.add_axes([0.15, 0.15, 0.75, 0.75])
ax.axis([0, 1, 0, 1])

#---- Define First Legend Entry ----

m1, = ax.plot([], [], c='red' , marker='s', markersize=20,
              fillstyle='left', linestyle='none')

m2, = ax.plot([], [], c='blue' , marker='s', markersize=20,
              fillstyle='right', linestyle='none')

#---- Define Second Legend Entry ----

m3, = ax.plot([], [], c='cyan' , marker='s', markersize=20,
              fillstyle='left', linestyle='none')

m4, = ax.plot([], [], c='magenta' , marker='s', markersize=20,
              fillstyle='right', linestyle='none')

#---- Plot Legend ----

ax.legend(((m2, m1), (m3, m4)), ('Foo', 'Foo2'), numpoints=1, labelspacing=2,
          loc='center', fontsize=16)

plt.show(block=False)

Which results in:

enter image description here

Disclaimer: This will only work for a two-colors legend entry. If more than two colours is desired, I cannot think of any other way to do this other than the approach described by @jwinterm (Python Matplotlib Multi-color Legend Entry)

查看更多
聊天终结者
3楼-- · 2020-07-09 07:13

Probably not exactly what you're looking for, but you can do it (very) manually by placing patches and text yourself on the plot. For instance:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

red_patch = mpatches.Patch(color='red', label='Foo')
plt.legend(handles=[red_patch])

r1 = mpatches.Rectangle((0.1, 0.1), 0.18, 0.1, fill=False)
r2 = mpatches.Rectangle((0.12, 0.12), 0.03, 0.06, fill=True, color='red')
r3 = mpatches.Rectangle((0.15, 0.12), 0.03, 0.06, fill=True, color='blue')
ax.add_patch(r1)
ax.add_patch(r2)
ax.add_patch(r3)
ax.annotate('Foo', (0.2, 0.13), fontsize='x-large')

plt.show()
查看更多
登录 后发表回答