gpt4 book ai didi

python - 如何在Python中对2个 float 组进行制表而不进行四舍五入

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:29 25 4
gpt4 key购买 nike

我正在使用凹凸和matplotlib。我有 2 个 ndarray,我希望生成一个包含 2 列的表格来并排比较它们

如何在不舍入值的情况下进行比较?我已经尝试过the table function ,但是当我传入类型转换的 float 时,它会将每个数字存储在一个单元格中

    #my code
the_table = plt.table(cellText= str(w), #w is a float
rowLabels= None,
colLabels="columns",
loc='bottom')
plt.show()

我的 table 看起来像这样 plot

最佳答案

table 需要数字序列,每个数字都放入一个表格单元格中。您仅向其提供单个数字的字符串表示形式,因此将此字符串中的每个字符解释为单个单元格的内容。

示例:

import numpy as np
import matplotlib.pyplot as plt
a = np.random.randn(20) # data for first column
b = np.random.randn(20) # data for second column
fig, ax = plt.subplots()
ax.axis("off")
ax.table(cellText=np.column_stack([a,b]),loc="center")
plt.show()

给出

enter image description here

请注意,仍然存在一定程度的舍入。为了避免这种情况,您可能必须自己处理浮点到字符串的转换(例如,使用 repr)。

表格必须是 matplotlib 图吗?使用类似

的东西会容易得多
for x, y in zip(a,b):
print "{}\t{}".format(repr(x),repr(y))

关于python - 如何在Python中对2个 float 组进行制表而不进行四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46475153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com