gpt4 book ai didi

python - 如何在图中表示非常大和非常小的值

转载 作者:行者123 更新时间:2023-12-05 09:12:37 25 4
gpt4 key购买 nike

我需要在直方图中绘制 3 个值。其中之一与其他值相比非常大。当我尝试绘制它们时,由于另外两个值较大,因此没有显示在图表中。除了 Python 中的直方图之外,是否有任何方法可以在图表中说明它们?有什么缩放技巧可以解决这个问题吗?

下面给出的代码是我试过的。我使用 python 库 numpy 和 matplotlib 来绘制图形。

import numpy as np
import matplotlib.pyplot as plt

height = [0.422602, 0.000011, 0.000453]
bars = ('2X2', '4X4', '8X8')
y_pos = np.arange(len(bars))

plt.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
plt.xlabel('Matrix Dimensions')
plt.ylabel('Fidelity for Matrices with Sparsity 1')
plt.xticks(y_pos, bars)

plt.show()

enter image description here

输出是上面包含的图片。此图片未描述其他两列的值。我该如何解决这个问题?

最佳答案

导入和数据

import matplotlib.pyplot as plt
import numpy as np

height = [0.422602, 0.000011, 0.000453]
bars = ('2X2', '4X4', '8X8')
y_pos = np.arange(len(bars))

例子1

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))

ax1.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
ax1.set(xlabel='Matrix Dimensions', ylabel='Fidelity for Matrices with Sparsity 1', title='y without log scale')
ax1.set_xticks(y_pos)
ax1.set_xticklabels(bars)

ax2.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))

# set yscale; can also use plt.yscale('log') or plt.yscale('symlog')
ax2.set(yscale='log', xlabel='Matrix Dimensions', ylabel='Fidelity for Matrices with Sparsity 1', title='y with log scale')
ax2.set_xticks(y_pos)
ax2.set_xticklabels(bars)

fig.tight_layout()
plt.show()

enter image description here

例子2

plt.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
plt.yscale('log')
plt.xlabel('Matrix Dimensions')
plt.ylabel('Fidelity for Matrices with Sparsity 1')
plt.xticks(y_pos, bars)

plt.show()

enter image description here

关于python - 如何在图中表示非常大和非常小的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57722200/

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