【问题标题】:How to convert my stats calculated array to 2 decimals? [duplicate]如何将我的统计数据计算数组转换为 2 位小数? [复制]
【发布时间】:2017-10-13 11:32:18
【问题描述】:

以下代码行在 python 中使用 matplotlib 在我的图表上打印标准差和均值。我的问题是。如何将值转换为 2 位小数?

ax.text(60, 0.01, r'$\mu={},\ \sigma={}$'.format(np.mean(stat_input),np.std(stat_input)))

【问题讨论】:

  • 您能详细说明“2 位小数”是什么意思吗?你目前正在得到什么,你想得到什么?
  • 我得到 mu = 54.38493289325 和 Sigma = 48.48389489348 我想要 54.38 和 48.48

标签: python numpy matplotlib


【解决方案1】:
>>> '{:.2f}'.format(123.456789)
'123.46'

详情见:Format String Syntax

因此,在您的情况下,该行变为:

ax.text(60, 0.01, r'$\mu={:.2f},\ \sigma={:.2f}$'.format(
    np.mean(stat_input), np.std(stat_input)
))

【讨论】:

  • 我明白了。我不知道如何将它合并到我编写的代码行中。我不是 python 专业人士,我是初学者。
  • @ShibbyBoss 已更新。您可以在 {} 中使用此语法
  • 啊,我明白了。我尝试了类似的东西,但缺少一个字符。谢谢
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2013-01-26
  • 2013-03-18
相关资源
最近更新 更多