如何在Matplotlib中仅显示图例中的文本标签?

如何在Matplotlib中仅显示图例中的文本标签?

为了仅在绘图图例中显示文本标签,我们可以在legend方法中使用handlelength=0、handletextpad=0和fancybox=0参数。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用numpy创建随机的x和y数据点。

  • 使用subplots()方法创建一个图形和一组子图。

  • 使用plot()方法绘制带有图例的x和y数据点,图例标签为“Zig-Zag”。

  • 使用legend()方法将图例标签放置在图中,参数为handlelength=0、handletextpad=0和fancybox=0。

  • 使用show()方法显示图形。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.rand(100)
y = np.random.rand(100)
fig, ax = plt.subplots()
ax.plot(x, y, label='Zig-Zag', c="red")
ax.legend(loc="upper right", handlelength=0, handletextpad=0, fancybox=True)
plt.show()

输出

如何在Matplotlib中仅显示图例中的文本标签?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程