【问题标题】:Matplotlib doesn't show hatches in custom legendMatplotlib 在自定义图例中不显示阴影
【发布时间】:2015-01-23 13:02:43
【问题描述】:

我创建了一个自定义图例,但他没有显示补丁的阴影。怎么了?

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()

谢谢

【问题讨论】:

标签: python matplotlib patch legend handles


【解决方案1】:

Patch 对象中的 color 关键字将覆盖 facecolor 和 edgecolor。所以它可以正确地显示舱口,但与补丁的颜色相同。具体设置facecolor就可以解决了,edgecolor可以用来给阴影上色。

那就试试吧:

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

如果您只想查看舱口,可以将linewidth 设置为零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多