Matplotlib 不在自定义图例中显示影线
Matplotlib doesn't show hatches in custom legend
我创建了一个自定义图例,但他没有显示补丁的影线。怎么了?
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()
谢谢
Patch
对象中的颜色关键字将覆盖面颜色和边颜色。所以它可能正确地显示了舱口,但颜色与补丁相同。具体设置 facecolor
将解决它, edgecolor
可用于为影线着色。
所以尝试:
near_patch = Patch(facecolor=[168/256,175/256,175/256],
label='Nearshore', hatch ='o')
如果您只想看到影线,可以将 linewidth
设置为零。
我创建了一个自定义图例,但他没有显示补丁的影线。怎么了?
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()
谢谢
Patch
对象中的颜色关键字将覆盖面颜色和边颜色。所以它可能正确地显示了舱口,但颜色与补丁相同。具体设置 facecolor
将解决它, edgecolor
可用于为影线着色。
所以尝试:
near_patch = Patch(facecolor=[168/256,175/256,175/256],
label='Nearshore', hatch ='o')
如果您只想看到影线,可以将 linewidth
设置为零。