python - 在图例条目中叠加多个艺术家

标签 python matplotlib

我正在尝试向背景中有补丁的图例添加条目。所需效果与所示效果相似 here ,但我也想覆盖一条线。基本上我正在尝试获得如下所示的内容:

enter image description here

但我不知道如何将蓝色虚线放在黄色补丁上。如何结合艺术家来获得这种效果?我没有看到任何示例或明显的方法。

最佳答案

基本思想是使用plt.legend([(X, Y)])在同一个渲染XY图例条目。请注意额外的一对括号。

这是一个与您的示例大致匹配的演示:

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

yellow_patch = mpatches.Patch(color='yellow')
blue_dotted_line = mlines.Line2D([], [], linewidth=2, linestyle="--", dashes=(3.7, 2), color='blue')
brown_line = mlines.Line2D([], [], linewidth=2, color='brown')

plt.legend([(yellow_patch, blue_dotted_line), brown_line], ["Entry One", "Entry Two"])

plt.show()

The legend approximately matching the wanted image.

虽然你是对的,但这并不明显。

关于python - 在图例条目中叠加多个艺术家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944931/

相关文章:

python - ipython笔记本中的Unicode

python - 在 matplotlib 中使用 bbox_inches ='tight' 时,如何获得具有精确大小(以像素为单位)的图像?

python - 有没有办法使用 pyodbc 向 sql server 2005 插入一个请求 scope_identity()

python - 如何获取 SQLALchemy 查询中实体的类型

python - 在 Pyramid 中,使用存储数据库连接的 python 全局变量是否安全?

python-3.x - 如何在 Matplotlib 中设置轴的 view_limits/range

python - x 轴上的 Matplotlib 日期时间

python - 生产中的 Django 的 POST 请求在计算引擎上抛出服务器错误(500)

OS X Yosemite 上的 Python 解释器——使用哪一个?

python - 如何给一些矩阵着色