Matplotlib 如何在图表中创建自定义图例 |
您所在的位置:网站首页 › 如何在图表中显示图例数据 › Matplotlib 如何在图表中创建自定义图例 |
Matplotlib 如何在图表中创建自定义图例
阅读更多:Matplotlib 教程 简介在图表中加入图例可以方便地为数据提供说明和解释。Matplotlib中的默认图例显示了一些基本信息,如线条、颜色、标记等。但是很多时候,我们需要创建自定义图例,以更清晰地向读者呈现数据。 下面将介绍如何在Matplotlib中创建自定义图例。 创建自定义图例 方法一:使用matplotlib.lines.Line2DLine2D是Matplotlib中一个重要的类,用于创建线条。我们可以使用Line2D创建一个自定义的线条图例。 首先,我们需要导入matplotlib.lines,并创建一个Line2D对象。然后,我们可以使用ax.add_artist()将图例添加到图表中。 import matplotlib.pyplot as plt import matplotlib.lines as lines fig, ax = plt.subplots() # 创建一个Line2D对象 line1 = lines.Line2D([], [], color='blue', linewidth=2, linestyle='--') line2 = lines.Line2D([], [], color='orange', linewidth=2, linestyle='-') # 添加Line2D对象到图表中 ax.add_artist(line1) ax.add_artist(line2) # 创建图例 leg = ax.legend(['line1', 'line2'], loc='upper right', frameon=False)这里,我们创建了两个Line2D对象: line1和line2。然后,我们使用ax.add_artist()将它们添加到图表中。最后,我们使用ax.legend()创建图例,并在其中显示line1和line2。 方法二:使用matplotlib.patches.Patch如果你需要创建一个自定义的图形图例,可以使用Patch类。Patch是Matplotlib中一个重要的类,用于绘制各种形状的图形,包括矩形、圆形、多边形等。 首先,我们需要导入matplotlib.patches,并创建一个Patch对象。然后,我们可以使用ax.add_artist()将图例添加到图表中。 import matplotlib.pyplot as plt import matplotlib.patches as patches fig, ax = plt.subplots() # 创建一个Patch对象 rect = patches.Rectangle((0.2, 0.2), 0.2, 0.2, color='red') # 添加Patch对象到图表中 ax.add_artist(rect) # 创建图例 leg = ax.legend([rect], ['Rectangle'], loc='upper right', frameon=False)这里,我们创建了一个Rectangle对象,它代表一个矩形。然后,我们使用ax.add_artist()将它添加到图表中。最后,我们使用ax.legend()创建图例,并在其中显示Rectangle。 方法三:使用matplotlib.lines.Line2D和matplotlib.patches.Patch的组合有时候我们需要同时显示线条和图形的图例,这时我们可以将两种方法组合使用。 import matplotlib.pyplot as plt import matplotlib.lines as lines import matplotlib.patches as patches fig, ax = plt.subplots() # 创建一个Line2D对象 line1 = lines.Line2D([], [], color='blue', linewidth=2, linestyle='--') # 创建一个Patch对象 rect = patches.Rectangle((0.2, 0.2), 0.2, 0.2, color='red') # 添加Line2D对象和Patch对象到图表中 ax.add_artist(line1) ax.add_artist(rect) # 创建图例 leg = ax.legend([line1, rect], ['Line', 'Rectangle'], loc='upper right', frameon=False)这里,我们创建了一个Line2D对象和一个Rectangle对象,并将它们添加到图表中。最后,我们使用ax.legend()创建图例,并在其中显示Line和Rectangle。 自定义图例样式我们可以通过一些方法来自定义图例的样式,包括字体、颜色、边框等。 自定义字体和颜色 import matplotlib.pyplotas plt import matplotlib.lines as lines import matplotlib.patches as patches import matplotlib.font_manager as fm fig, ax = plt.subplots() # 创建一个Line2D对象 line1 = lines.Line2D([], [], color='blue', linewidth=2, linestyle='--') # 创建一个Patch对象 rect = patches.Rectangle((0.2, 0.2), 0.2, 0.2, color='red') # 添加Line2D对象和Patch对象到图表中 ax.add_artist(line1) ax.add_artist(rect) # 创建图例 leg = ax.legend([line1, rect], ['Line', 'Rectangle'], loc='upper right', frameon=False) # 自定义字体和颜色 font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc', size=12) for text in leg.get_texts(): text.set_font_properties(font) text.set_color('black')这里,我们使用FontProperties来指定字体文件和字号大小,并使用set_font_properties()和set_color()方法设置字体和颜色。 自定义边框和背景 import matplotlib.pyplot as plt import matplotlib.lines as lines import matplotlib.patches as patches import matplotlib.font_manager as fm fig, ax = plt.subplots() # 创建一个Line2D对象 line1 = lines.Line2D([], [], color='blue', linewidth=2, linestyle='--') # 创建一个Patch对象 rect = patches.Rectangle((0.2, 0.2), 0.2, 0.2, color='red') # 添加Line2D对象和Patch对象到图表中 ax.add_artist(line1) ax.add_artist(rect) # 创建图例 leg = ax.legend([line1, rect], ['Line', 'Rectangle'], loc='upper right', frameon=True, edgecolor='gray', facecolor='lightgray') # 自定义边框和背景 leg.get_frame().set_linewidth(0.0)这里,我们将frameon设置为True,并使用edgecolor和facecolor分别设置边框和背景的颜色。然后,我们使用get_frame()方法获取图例的边框,并使用set_linewidth()方法将边框的宽度设置为0。 总结Matplotlib中的默认图例不能满足所有的需求,但是我们可以使用Line2D和Patch等类来创建自定义图例。在创建自定义图例的过程中,我们可以自定义图例的样式,包括字体、颜色、边框、背景等。 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |