如何制作只有文字的图例

Syd*_*Syd 5 python plot text matplotlib legend

我有两个要为其制作传奇的情节,现在它们看起来像这样: 在此处输入图片说明

我只希望“C3H8”和“CO2”出现在图例中,不包括蓝色框。现在我正在使用该matplotlib.patches模块。还有什么我可以用的吗?

The*_*tor 5

正如@PaulH 所提到的,您只需要使用plt.text或 即可plt.annotate

例子:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,100)
y = np.random.random(100)
y2 = np.random.random(100)

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.scatter(x,y)
ax1.annotate("$C_{3}H_{8}$", xy=(0.9,0.9),xycoords='axes fraction',
             fontsize=14)

ax2 = fig.add_subplot(212)
ax2.scatter(x,y2)
ax2.annotate("$CO_{2}$", xy=(0.9,0.9),xycoords='axes fraction',
             fontsize=14)

fig.show()
Run Code Online (Sandbox Code Playgroud)

这里xy注释中的参数是指x and y文本的坐标。您可以相应地更改它们。

产生:

在此处输入图片说明


tsv*_*iko 5

您可以在 pyplot 注释中使用bbox文本属性,如下所示:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-5, 3))

ax.annotate("$C_{3}H_{8}$",
            xy=(0.86,0.9), xycoords='axes fraction',
            textcoords='offset points',
            size=14,
            bbox=dict(boxstyle="round", fc=(1.0, 0.7, 0.7), ec="none"))

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述