Matplotlib doesn't show hatches in custom legend
Asked Answered
D

1

8

I create a custom legend, but he does not show the hatches of the patches. Whats wrong?

import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from pylab import *
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
fig1 = plt.figure(1,(10,10))
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
far_patch = Patch(color=[168/256,175/256,175/256], label='Farshore')
near_patch = Patch(color=[168/256,175/256,175/256], label='Nearshore',     hatch ='o')
legend=plt.legend(handles=[far_patch, near_patch],loc='upper left',     handlelength=1, handleheight=1,labelspacing=0,    fontsize=8,borderaxespad=0.3,handletextpad=0.2)

frame = legend.get_frame()
frame.set_edgecolor('none')

figureName='test'

plt.savefig(figureName+'.pdf',bbox_inches='tight',dpi=fig1.dpi)
plt.show()

Thanks

Danaus answered 23/1, 2015 at 13:2 Comment(1)
please provide a minimal working example: stackoverflow.com/help/how-to-askEmmaemmalee
E
7

The color keyword from the Patch object will override both the facecolor and the edgecolor. So its propably showing the hatch correctly, but in the same color as the patch. Specifically setting the facecolor will solve it, and the edgecolor can be used to color the hatch.

So try:

near_patch = Patch(facecolor=[168/256,175/256,175/256], 
                   label='Nearshore', hatch ='o')

enter image description here

You can set the linewidth to zero if you only want to see the hatch.

Escapade answered 11/2, 2015 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.