如何在不舍入的情况下在python中列出2个浮点数组

2024-06-15 20:45:10 发布

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

我在和坎比和马特普洛特利布一起工作。我有2个数组,我希望生成一个包含2列的表来并排比较它们

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

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

我的桌子看起来像这样plot


Tags: theismytable情况codefunctionplt
1条回答
网友
1楼 · 发布于 2024-06-15 20:45:10

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))

相关问题 更多 >