【问题标题】:Rounding off spearmanr result to 3 decimal places将 spearmanr 结果四舍五入到小数点后 3 位
【发布时间】:2017-05-12 01:35:50
【问题描述】:

如果我想将 spearmanr 结果作为注释放在我的绘图上,如何将其四舍五入到小数点后 3 位?

import matplotlib.pyplot as plt
import scipy.stats
import numpy as np

percent = np.array([34.72,47.22,40.97,43.06,57.64,50.00,69.44])
year = np.array([2009,2010,2011,2012,2013,2014,2015])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(year,percent, marker='o')
ax.set_ylim(20,80)
ax.set_xticks(year)

ax.annotate(scipy.stats.spearmanr(year,percent), xy=(2009,70))

plt.show()

Output 我希望注释保留在图表内。

【问题讨论】:

    标签: python-3.x matplotlib scipy


    【解决方案1】:

    重新格式化注释中的文本。将倒数第二行替换为:

    r = scipy.stats.spearmanr(year, percent)
    text = "correlation: {:.3f}, pvalue:{:.3f}".format(r.correlation, r.pvalue)
    ax.annotate(text, xy=(2009,70))
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多