【问题标题】:Matplotlib - cumulative density plot with Y-axis as cumulative fractionMatplotlib - 以 Y 轴为累积分数的累积密度图
【发布时间】:2019-12-29 04:02:47
【问题描述】:

我可以制作累积密度图 (cumulative distribution plots python):

import numpy as np
import matplotlib.pyplot as plt

# Some fake data:
data = np.random.randn(1000)

sorted_data = np.sort(data)  # Or data.sort(), if data can be modified

# Cumulative counts:
plt.step(np.concatenate([sorted_data, sorted_data[[-1]]]),
         np.arange(sorted_data.size+1))

plt.show()

但是,我希望将 Y 轴表示为累积分数。介于 0 和 1 之间的某个值。如何缩放 Y 轴来做到这一点?

解决方案

请参阅下面欧内斯特的回答。如果使用 Python 2:

plt.step(np.concatenate([sorted_data, sorted_data[[-1]]]), np.arange(sorted_data.size+1)/float(sorted_data.size))

【问题讨论】:

  • 除以你的数据大小?

标签: python matplotlib


【解决方案1】:

不要让事情过于复杂,只需除以您拥有的数据数量即可

import numpy as np
import matplotlib.pyplot as plt

# Some fake data:
data = np.random.randn(1000)

sorted_data = np.sort(data)  # Or data.sort(), if data can be modified

# Cumulative counts:
plt.step(np.concatenate([sorted_data, sorted_data[[-1]]]),
         np.arange(sorted_data.size+1)/sorted_data.size)

plt.show()

【讨论】:

  • 在您询问累积分数的问题中。所以我回答了这个问题。
  • plt.step(np.concatenate([sorted_data, sorted_data[[-1]]]), np.arange(sorted_data.size+1)/float(sorted_data.size)) 固定跨度>
  • 我可以使用你喜欢的任何版本,但我强烈怀疑这与 matplotlib 的版本有关。所以你显然在使用 python 2;既然这已经慢慢变老了,最好在寻求帮助时提及这一事实,以便人们了解它
【解决方案2】:

通常,您可以通过从最小值中减去您的值并除以最大值和最小值之间的差值来使用最小值-最大值缩放。

y = np.arange(sorted_data.size+1)
# Using min-max scaling
y = (y - np.min(y)) / (np.max(y) - np.min(y))

由于在这种情况下y 的最小值为 0,这与除以 y 值的最大值相同。

plt.step(np.concatenate([sorted_data, sorted_data[[-1]]]),
     y / np.max(y))

【讨论】:

  • max(np.arange(sorted_data.size+1)) == sorted_data.size
  • @ImportanceOfBeingErnest 哈哈,完全超越了那个。不错的收获。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2010-10-14
  • 2018-05-13
  • 2019-04-19
  • 1970-01-01
  • 2015-11-17
相关资源
最近更新 更多