【问题标题】:How to represent very large and a very small values in a plot如何在图中表示非常大和非常小的值
【发布时间】:2020-01-03 10:33:48
【问题描述】:

我需要在直方图中绘制 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()

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

【问题讨论】:

    标签: python pandas matplotlib seaborn bar-chart


    【解决方案1】:

    这不是栏本身的问题,因为数字的差异太大而无法显示。

    在这种情况下,通常使用对数轴。这意味着您没有线性轴,而是对数轴。请参阅此处的文档:https://matplotlib.org/3.1.0/gallery/scales/log_bar.html

    【讨论】:

      【解决方案2】:

      进口和数据

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

      示例 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        相关资源
        最近更新 更多