如何在 python 中更改 matplotlib ax.text 中的小数位? [复制]

Posted

技术标签:

【中文标题】如何在 python 中更改 matplotlib ax.text 中的小数位? [复制]【英文标题】:how to change decimal places in matplotlib ax.text in python? [duplicate] 【发布时间】:2021-02-03 06:24:23 【问题描述】:

我有数据集调用“df”并尝试使用其中一个列数据作为图轴。

数据在图表上显示为 11.58,所以我想将其更改为小数点后一位,即 11.5

这是我得到的代码。

if df['gear'] is no None:
    ax2.text[10,5, df['gear'][0], size=5, weight='bold', color='black', rotation=0., ha="center", va="center")

要将其更改为小数点后一位,我已经尝试如下,但它不起作用

if df['gear'] is no None:
        ax2.text[10,5, df['gear'][0], ":.1f", size=5, weight='bold', color='black', rotation=0., ha="center", va="center")

谁能告诉我如何更改 ax.text 中的小数点。 谢谢

【问题讨论】:

ax2.text[10, 5, f"df['gear'][0]:.1f", ...) 应该可以解决问题。 【参考方案1】:

使用round函数将值四舍五入到小数点后一位

round(number, number of digits)
ax2.text[10,5, round(df['gear'][0], 1), size=5, weight='bold', color='black', rotation=0., ha="center", va="center")

【讨论】:

以上是关于如何在 python 中更改 matplotlib ax.text 中的小数位? [复制]的主要内容,如果未能解决你的问题,请参考以下文章