Matplotlib文本未显示在p中

2024-03-29 10:09:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过为坐标轴提供相对范围为0-1的坐标来为pandas绘图添加文本注释,但是当前文本没有显示出来。下面是一些示例代码和结果图:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt


df = sns.load_dataset('iris')
df['sepal_length'].plot()
plt.text(0.7, 0.7, 'test')
plt.show()

pandas plot

我使用的版本: 熊猫:0.24.2 matplotlib:3.0.3版

在Ubuntu 18上。你知道吗

annotate()函数似乎也不起作用。你知道吗

有没有线索说明为什么文本没有出现?你知道吗


Tags: 代码文本import绘图示例pandasdfmatplotlib
3条回答

正如其他人所提到的,您使用的测试点超出了绘制范围。测试范围内的一个点,它应该按预期工作。你知道吗

这是测试点(60,5)

enter image description here

由于绘图的边界超出了传递的值,因此应尝试传递不同的参数,例如:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt


df = sns.load_dataset('iris')
df['sepal_length'].plot()
plt.text(70,7, 'test')
plt.show()

好吧,感谢@ImportanceOfBeingErnest,我误解了打印文本(). 默认情况下,x和y坐标位于“数据”维度中。要改为使用轴的0-1范围,可以执行以下操作:

ax = df['sepal_length'].plot()
plt.text(0.1, 0.7, 'test', transform=ax.transAxes)

这在文档中不是很清楚(尤其是'transform'参数的选项),但是它在一个例子中:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.text.html#matplotlib.pyplot.text

相关问题 更多 >