Matplotlib中在同一图表上放置两个不同图例的详细指南

Matplotlib中在同一图表上放置两个不同图例的详细指南

参考:Placing Two Different Legends on the Same Graph With Matplotlib

Matplotlib是Python中强大的数据可视化库,它提供了丰富的功能来创建各种类型的图表。在数据可视化过程中,我们经常需要在同一张图表上展示多组数据,并为每组数据添加相应的图例。有时,这些数据可能需要不同的图例来更好地解释。本文将详细介绍如何在Matplotlib中在同一图表上放置两个不同的图例,以及相关的技巧和注意事项。

1. 为什么需要两个图例?

在某些情况下,我们可能需要在同一张图表上放置两个不同的图例。这通常发生在以下场景:

  1. 展示多组相关但不同类型的数据
  2. 比较不同数据集的特征
  3. 显示主要数据和辅助数据的图例
  4. 在同一图表上展示不同尺度或单位的数据

使用两个图例可以更清晰地展示复杂的数据关系,提高图表的可读性和信息传递效率。

2. Matplotlib中创建基本图表

在开始添加多个图例之前,让我们先创建一个基本的图表。以下是一个简单的示例,展示了如何使用Matplotlib创建一个包含两条线的图表:

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 创建图表
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='Sine - how2matplotlib.com')
plt.plot(x, y2, label='Cosine - how2matplotlib.com')

# 添加标题和轴标签
plt.title('Sine and Cosine Functions - how2matplotlib.com')
plt.xlabel('X-axis - how2matplotlib.com')
plt.ylabel('Y-axis - how2matplotlib.com')

# 添加图例
plt.legend()

plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们创建了一个包含正弦和余弦函数的图表。plt.legend()函数自动添加了一个包含两条线标签的图例。

3. 使用两个ax.legend()创建两个图例

要在同一图表上放置两个不同的图例,我们可以使用ax.legend()方法两次。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x**2

# 创建图表
fig, ax = plt.subplots(figsize=(12, 8))

# 绘制线条
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')
line3, = ax.plot(x, y3, label='Tangent - how2matplotlib.com')
line4, = ax.plot(x, y4, label='Square - how2matplotlib.com')

# 创建第一个图例
legend1 = ax.legend(handles=[line1, line2], loc='upper left', title='Trigonometric - how2matplotlib.com')

# 添加第二个图例
ax.legend(handles=[line3, line4], loc='upper right', title='Other Functions - how2matplotlib.com')

# 将第一个图例添加到轴
ax.add_artist(legend1)

# 添加标题和轴标签
ax.set_title('Multiple Functions with Two Legends - how2matplotlib.com')
ax.set_xlabel('X-axis - how2matplotlib.com')
ax.set_ylabel('Y-axis - how2matplotlib.com')

plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们使用ax.legend()方法创建了两个独立的图例。第一个图例包含正弦和余弦函数,第二个图例包含正切和平方函数。我们使用loc参数指定了每个图例的位置,并使用title参数为每个图例添加了标题。

注意,我们需要使用ax.add_artist(legend1)将第一个图例添加回图表中,因为第二次调用ax.legend()会覆盖之前的图例。

4. 使用plt.legend()和ax.legend()组合

另一种创建两个图例的方法是结合使用plt.legend()ax.legend()。这种方法特别适用于当你想要一个全局图例和一个特定于某个轴的图例时。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.exp(x/10)

# 创建图表
fig, ax1 = plt.subplots(figsize=(12, 8))

# 创建第二个y轴
ax2 = ax1.twinx()

# 绘制线条
line1, = ax1.plot(x, y1, 'b-', label='Sine - how2matplotlib.com')
line2, = ax1.plot(x, y2, 'r-', label='Cosine - how2matplotlib.com')
line3, = ax2.plot(x, y3, 'g-', label='Exponential - how2matplotlib.com')

# 创建第一个图例(使用plt.legend)
plt.legend(handles=[line1, line2], loc='upper left', title='Trigonometric - how2matplotlib.com')

# 创建第二个图例(使用ax2.legend)
ax2.legend(handles=[line3], loc='upper right', title='Exponential - how2matplotlib.com')

# 添加标题和轴标签
ax1.set_title('Trigonometric and Exponential Functions - how2matplotlib.com')
ax1.set_xlabel('X-axis - how2matplotlib.com')
ax1.set_ylabel('Y-axis (Trigonometric) - how2matplotlib.com')
ax2.set_ylabel('Y-axis (Exponential) - how2matplotlib.com')

plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们创建了一个具有两个y轴的图表。左侧的y轴用于绘制三角函数,右侧的y轴用于绘制指数函数。我们使用plt.legend()创建了一个包含三角函数的图例,并使用ax2.legend()创建了一个包含指数函数的图例。

5. 自定义图例样式

Matplotlib提供了多种方法来自定义图例的样式。以下是一些常用的自定义选项:

5.1 更改图例位置

你可以使用loc参数来指定图例的位置。Matplotlib提供了多个预定义的位置,如’upper left’、’lower right’等。你也可以使用数字来精确定位图例。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig, ax = plt.subplots(figsize=(10, 6))
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')

# 使用预定义位置
legend1 = ax.legend(handles=[line1], loc='upper left', title='Legend 1 - how2matplotlib.com')

# 使用精确坐标
ax.legend(handles=[line2], loc=(0.8, 0.1), title='Legend 2 - how2matplotlib.com')

ax.add_artist(legend1)

plt.title('Custom Legend Positions - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们将第一个图例放置在左上角,将第二个图例放置在坐标(0.8, 0.1)的位置。

5.2 调整图例框的样式

你可以通过设置图例的各种属性来自定义其外观。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig, ax = plt.subplots(figsize=(10, 6))
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')

legend1 = ax.legend(handles=[line1], loc='upper left', title='Legend 1 - how2matplotlib.com',
                    fancybox=True, shadow=True, framealpha=0.7)

legend2 = ax.legend(handles=[line2], loc='lower right', title='Legend 2 - how2matplotlib.com',
                    edgecolor='red', facecolor='lightgray')

ax.add_artist(legend1)

plt.title('Custom Legend Styles - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们为第一个图例添加了圆角(fancybox=True)、阴影(shadow=True)和半透明背景(framealpha=0.7)。对于第二个图例,我们自定义了边框颜色(edgecolor='red')和背景颜色(facecolor='lightgray')。

5.3 调整图例中的标记大小

有时,默认的图例标记可能太大或太小。你可以使用markerscale参数来调整标记的大小。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig, ax = plt.subplots(figsize=(10, 6))
line1, = ax.plot(x, y1, 'o-', label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, 's-', label='Cosine - how2matplotlib.com')

legend1 = ax.legend(handles=[line1], loc='upper left', title='Legend 1 - how2matplotlib.com',
                    markerscale=0.5)

legend2 = ax.legend(handles=[line2], loc='lower right', title='Legend 2 - how2matplotlib.com',
                    markerscale=2)

ax.add_artist(legend1)

plt.title('Custom Legend Marker Sizes - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们将第一个图例的标记缩小到原来的一半(markerscale=0.5),将第二个图例的标记放大到原来的两倍(markerscale=2)。

6. 处理重叠的图例

当你在图表上放置多个图例时,可能会遇到图例重叠的问题。以下是一些解决这个问题的方法:

6.1 调整图例位置

最简单的方法是调整图例的位置,确保它们不会重叠。你可以使用loc参数或精确坐标来定位图例。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

fig, ax = plt.subplots(figsize=(12, 8))
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')
line3, = ax.plot(x, y3, label='Tangent - how2matplotlib.com')

legend1 = ax.legend(handles=[line1, line2], loc='upper left', title='Trigonometric 1 - how2matplotlib.com')
legend2 = ax.legend(handles=[line3], loc='lower right', title='Trigonometric 2 - how2matplotlib.com')

ax.add_artist(legend1)

plt.title('Avoiding Legend Overlap - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们将一个图例放在左上角,另一个放在右下角,以避免重叠。

6.2 使用bbox_to_anchor参数

bbox_to_anchor参数允许你更精确地控制图例的位置。你可以将图例放置在图表的任何位置,甚至是图表区域之外。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

fig, ax = plt.subplots(figsize=(12, 8))
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')
line3, = ax.plot(x, y3, label='Tangent - how2matplotlib.com')

legend1 = ax.legend(handles=[line1, line2], bbox_to_anchor=(0, 1), loc='upper left', title='Trigonometric 1 - how2matplotlib.com')
legend2 = ax.legend(handles=[line3], bbox_to_anchor=(1, 0), loc='lower right', title='Trigonometric 2 - how2matplotlib.com')

ax.add_artist(legend1)

plt.title('Using bbox_to_anchor for Legend Positioning - how2matplotlib.com')
plt.tight_layout()
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们使用bbox_to_anchor参数将第一个图例放置在图表的左上角,将第二个图例放置在图表的右下角。这种方法可以更灵活地控制图例的位置,避免重叠。

6.3 调整图表布局

有时,你可能需要调整整个图表的布局来为多个图例腾出空间。你可以使用plt.subplots_adjust()fig.tight_layout()来实现这一点。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

fig, ax = plt.subplots(figsize=(12, 8))
line1, = ax.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, label='Cosine - how2matplotlib.com')
line3, = ax.plot(x, y3, label='Tangent - how2matplotlib.com')

legend1 = ax.legend(handles=[line1, line2], loc='upper left', title='Trigonometric 1 - how2matplotlib.com')
legend2 = ax.legend(handles=[line3], loc='upper right', title='Trigonometric 2 - how2matplotlib.com')

ax.add_artist(legend1)

plt.title('Adjusting Layout for Multiple Legends - how2matplotlib.com')
plt.subplots_adjust(right=0.85)  # 调整右边界,为右侧图例腾出空间
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们使用plt.subplots_adjust(right=0.85)来缩小图表的右边界,为右侧的图例腾出更多空间。

7. 处理多个子图的图例

当你的图表包含多个子图时,处理图例可能会变得更加复杂。以下是一些处理多个子图图例的方法:

7.1 为每个子图添加独立的图例

你可以为每个子图添加独立的图例。这种方法适用于每个子图都有自己的数据集和图例的情况。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x**2

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))

line1, = ax1.plot(x, y1, label='Sine - how2matplotlib.com')
line2, = ax1.plot(x, y2, label='Cosine - how2matplotlib.com')
ax1.legend(loc='upper left', title='Subplot 1 - how2matplotlib.com')
ax1.set_title('Trigonometric Functions - how2matplotlib.com')

line3, = ax2.plot(x, y3, label='Tangent - how2matplotlib.com')
line4, = ax2.plot(x, y4, label='Square - how2matplotlib.com')
ax2.legend(loc='upper left', title='Subplot 2 - how2matplotlib.com')
ax2.set_title('Other Functions - how2matplotlib.com')

plt.tight_layout()
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们创建了两个子图,每个子图都有自己的图例。

7.2 创建一个共享的图例

有时,你可能希望为所有子图创建一个共享的图例。这可以通过在图表级别而不是子图级别创建图例来实现。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x**2

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))

line1, = ax1.plot(x, y1, 'r-', label='Sine - how2matplotlib.com')
line2, = ax1.plot(x, y2, 'b-', label='Cosine - how2matplotlib.com')
ax1.set_title('Trigonometric Functions - how2matplotlib.com')

line3, = ax2.plot(x, y3, 'g-', label='Tangent - how2matplotlib.com')
line4, = ax2.plot(x, y4, 'm-', label='Square - how2matplotlib.com')
ax2.set_title('Other Functions - how2matplotlib.com')

# 创建共享图例
fig.legend(loc='lower center', ncol=4, title='Shared Legend - how2matplotlib.com')

plt.tight_layout()
plt.subplots_adjust(bottom=0.2)  # 为底部的共享图例腾出空间
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们使用fig.legend()创建了一个共享的图例,并将其放置在图表的底部。我们还使用plt.subplots_adjust(bottom=0.2)来为底部的图例腾出空间。

8. 处理不同类型的图表元素

有时,你的图表可能包含不同类型的元素,如线条、散点、条形等。在这种情况下,你可能需要为不同类型的元素创建不同的图例。以下是一个示例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 20)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.random.rand(20)

fig, ax = plt.subplots(figsize=(12, 8))

line1, = ax.plot(x, y1, 'r-', label='Sine - how2matplotlib.com')
line2, = ax.plot(x, y2, 'b-', label='Cosine - how2matplotlib.com')
scatter = ax.scatter(x, y3, c='g', label='Random Data - how2matplotlib.com')

# 创建线条图例
legend1 = ax.legend(handles=[line1, line2], loc='upper left', title='Lines - how2matplotlib.com')
ax.add_artist(legend1)

# 创建散点图例
legend2 = ax.legend(handles=[scatter], loc='lower right', title='Scatter - how2matplotlib.com')

ax.set_title('Multiple Chart Types with Separate Legends - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们创建了一个包含线条和散点的图表,并为每种类型的元素创建了单独的图例。

9. 使用自定义图例

有时,默认的图例可能无法满足你的需求。在这种情况下,你可以创建自定义的图例。以下是一个创建自定义图例的示例:

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

fig, ax = plt.subplots(figsize=(10, 6))

# 创建一些虚拟数据
ax.plot([1, 2, 3], [1, 2, 3], 'ro-', label='Data 1 - how2matplotlib.com')
ax.plot([1, 2, 3], [2, 3, 4], 'bs-', label='Data 2 - how2matplotlib.com')

# 创建自定义图例元素
red_patch = mpatches.Patch(color='red', label='Red Data - how2matplotlib.com')
blue_patch = mpatches.Patch(color='blue', label='Blue Data - how2matplotlib.com')
custom_line = plt.Line2D([0], [0], color='green', lw=4, label='Custom Line - how2matplotlib.com')

# 添加默认图例
ax.legend(loc='upper left', title='Default Legend - how2matplotlib.com')

# 添加自定义图例
ax.legend(handles=[red_patch, blue_patch, custom_line], loc='lower right', title='Custom Legend - how2matplotlib.com')

ax.set_title('Chart with Custom Legend - how2matplotlib.com')
plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们使用matplotlib.patches.Patchplt.Line2D创建了自定义的图例元素,然后将它们添加到一个单独的图例中。

10. 图例的交互性

Matplotlib还提供了一些交互性功能,允许用户通过图例来控制图表的显示。以下是一个示例,展示如何创建一个可以通过点击图例来切换数据线可见性的图表:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

fig, ax = plt.subplots(figsize=(10, 6))

lines = []
lines.append(ax.plot(x, y1, label='Sine - how2matplotlib.com')[0])
lines.append(ax.plot(x, y2, label='Cosine - how2matplotlib.com')[0])
lines.append(ax.plot(x, y3, label='Tangent - how2matplotlib.com')[0])

ax.set_title('Interactive Legend - how2matplotlib.com')

leg = ax.legend(loc='upper right', title='Click to toggle - how2matplotlib.com')

def on_pick(event):
    legline = event.artist
    origline = lined[legline]
    visible = not origline.get_visible()
    origline.set_visible(visible)
    legline.set_alpha(1.0 if visible else 0.2)
    fig.canvas.draw()

lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(True)
    lined[legline] = origline

fig.canvas.mpl_connect('pick_event', on_pick)

plt.show()

Output:

Matplotlib中在同一图表上放置两个不同图例的详细指南

在这个示例中,我们创建了一个交互式图例,用户可以通过点击图例中的项目来切换相应数据线的可见性。

结论

在Matplotlib中在同一图表上放置两个不同的图例是一项强大的技能,可以帮助你更有效地传达复杂的数据关系。通过本文介绍的各种技巧和方法,你可以创建更加丰富和信息量大的可视化图表。

记住,图例的设计应该始终以提高图表的可读性和理解性为目标。合理使用多个图例可以帮助你更清晰地展示数据,但过度使用可能会导致图表变得混乱。因此,在决定是否使用多个图例以及如何布置它们时,要根据你的具体需求和数据特性来权衡。

通过实践和探索,你将能够掌握在Matplotlib中创建和管理多个图例的技巧,从而制作出更加专业和有说服力的数据可视化图表。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程