python - 是否可以在 matplotlib 中添加一个字符串作为图例项

标签 python matplotlib pandas legend legend-properties

我正在 matplotlib 中制作一些图,并想为一些数据添加解释性文本。我想在我的图例中有一个字符串作为“0-10”项上方的单独图例项。有谁知道是否有可能的方法来做到这一点?

enter image description here

这是我传奇的代码:
ax.legend(['0-10','10-100','100-500','500+'],loc='best')

最佳答案

替代解决方案,有点脏但很快。

import pylab as plt

X = range(50)
Y = range(50)
plt.plot(X, Y, label="Very straight line")

# Create empty plot with blank marker containing the extra label
plt.plot([], [], ' ', label="Extra label on the legend")

plt.legend()
plt.show()

enter image description here

关于python - 是否可以在 matplotlib 中添加一个字符串作为图例项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826711/

相关文章:

python - 将类似字典的对象传递给 exec 而不是 dict 对象会更改封装的范围

python - 截断数据帧的字符串并将它们分组在一起

python - 迭代数据帧中选定列的行到每行 "clean"

python - matplotlib 将窗口显示为事件窗口

python - 如何有效地将一个 Pandas Dataframe 的每一列与另一个 Dataframe 的每一列相乘?

python - 从 Paramiko 命令捕获标准输出

python - 基本列表检查器

python - 类属性的继承(python)

python - 使用循环从列表创建子图。使数据绘制到两个子图上

python - Pycharm - 导入 Matplotlib