Matplotlib自定义图例以显示正方形而不是矩形

时间:2016-11-18 08:05:09

标签: python matplotlib

这是我尝试将条形图的图例从矩形更改为方形:

import matplotlib.patches as patches

rect1 = patches.Rectangle((0,0),1,1,facecolor='#FF605E')
rect2 = patches.Rectangle((0,0),1,1,facecolor='#64B2DF')
plt.legend((rect1, rect2), ('2016', '2015'))

但是当我绘制这个时,我仍然会看到矩形而不是正方形:

enter image description here

有关如何执行此操作的任何建议?

我尝试了@ImportanceOfBeingErnest和@furas提供的两种解决方案,结果如下:

@ ImportanceOfBeingErnest的解决方案是最容易做到的:

plt.rcParams['legend.handlelength'] = 1
plt.rcParams['legend.handleheight'] = 1.125

结果如下:

enter image description here

我的最终代码如下:

plt.legend((df.columns[1], df.columns[0]), handlelength=1, handleheight=1) # the df.columns = the legend text

@ furas的解决方案产生了这个,我不知道为什么文本远离矩形,但我确信差距可以以某种方式改变:

enter image description here

2 个答案:

答案 0 :(得分:3)

Matplotlib提供rcParams

legend.handlelength  : 2.    # the length of the legend lines in fraction of fontsize
legend.handleheight  : 0.7   # the height of the legend handle in fraction of fontsize

您可以在通话中设置plt.legend()

plt.legend(handlelength=1, handleheight=1)

或在脚本开头使用rcParams

import matplotlib
matplotlib.rcParams['legend.handlelength'] = 1
matplotlib.rcParams['legend.handleheight'] = 1

不幸的是,提供相等的handlelength=1, handleheight=1将无法提供完美的矩形。似乎handlelength=1, handleheight=1.125将完成这项工作,但这可能取决于所使用的字体。

另一种方法是,如果你想使用代理艺术家,可以使用plot / scatter方法中的方形标记。

bar1 = plt.plot([], marker="s", markersize=15, linestyle="", label="2015")

并将其提供给图例legend(handles=[bar1])。使用此方法需要设置matplotlib.rcParams['legend.numpoints'] = 1,否则图例中将出现两个标记。

<小时/> 以下是两种方法

的完整示例
import matplotlib.pyplot as plt
plt.rcParams['legend.handlelength'] = 1
plt.rcParams['legend.handleheight'] = 1.125
plt.rcParams['legend.numpoints'] = 1


fig, ax = plt.subplots(ncols=2, figsize=(5,2.5))

# Method 1: Set the handlesizes already in the rcParams
ax[0].set_title("Setting handlesize")
ax[0].bar([0,2], [6,3], width=0.7, color="#a30e73", label="2015", align="center")
ax[0].bar([1,3], [3,2], width=0.7, color="#0943a8", label="2016", align="center" )
ax[0].legend()

# Method 2: use proxy markers. (Needs legend.numpoints to be 1)
ax[1].set_title("Proxy markers")
ax[1].bar([0,2], [6,3], width=0.7, color="#a30e73", align="center" )
ax[1].bar([1,3], [3,2], width=0.7, color="#0943a8", align="center" )
b1, =ax[1].plot([], marker="s", markersize=15, linestyle="", color="#a30e73",  label="2015")
b2, =ax[1].plot([], marker="s", markersize=15, linestyle="", color="#0943a8",  label="2016")
ax[1].legend(handles=[b1, b2])

[a.set_xticks([0,1,2,3]) for a in ax]
plt.show()

生产

enter image description here

答案 1 :(得分:3)

似乎他们很久以前就改变了 - 现在有些元素不能直接用在图例中。

现在需要handlerImplementing a custom legend handler

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.legend_handler import HandlerPatch

# --- handlers ---

class HandlerRect(HandlerPatch):

    def create_artists(self, legend, orig_handle,
                       xdescent, ydescent, width, height,
                       fontsize, trans):

        x = width//2
        y = 0
        w = h = 10

        # create
        p = patches.Rectangle(xy=(x, y), width=w, height=h)

        # update with data from oryginal object
        self.update_prop(p, orig_handle, legend)

        # move xy to legend
        p.set_transform(trans)

        return [p]


class HandlerCircle(HandlerPatch):

    def create_artists(self, legend, orig_handle,
                       xdescent, ydescent, width, height,
                       fontsize, trans):

        r = 5
        x = r + width//2
        y = height//2

        # create 
        p = patches.Circle(xy=(x, y), radius=r)

        # update with data from oryginal object
        self.update_prop(p, orig_handle, legend)

        # move xy to legend
        p.set_transform(trans)

        return [p]

# --- main ---

rect = patches.Rectangle((0,0), 1, 1, facecolor='#FF605E')
circ = patches.Circle((0,0), 1, facecolor='#64B2DF')

plt.legend((rect, circ), ('2016', '2015'),
            handler_map={
               patches.Rectangle: HandlerRect(),
               patches.Circle: HandlerCircle(),
            })

plt.show()

图例保留了矩形的位置,此方法无法更改,因此空间太多。

enter image description here