簡體   English   中英

Python:如何繪制條件累積頻率直方圖?

[英]Python: How to plot a conditional cumulative frequency histogram?

我有這個數據列表,我想為其繪制直方圖。 然而,對於 X 軸的大值來說,圖表的可讀性不是很好,保留它們並不是很重要。

這是我的數據的子樣本:

print(v)

1      1738   #the values ​​I want to plot on the histogram
2      2200
3      1338
4      1222
5       939
6       898 

我計算的累積頻率如下:

v = x.cumsum()
t = [round(100*v/x.sum(),2)]
t

輸出是:

 1        9.90
 2       22.44
 3       30.06
 4       37.02
 5       42.37

如何在直方圖上僅表示累積頻率小於或等於 40% 的數據?

我不知道如何在python中做,提前感謝您的幫助

簡短的回答是:切片 numpy 數組以過濾值 <= 40%。 例如,如果a是一維 numpy 數組:

a[a <= 40]

下面的示例提供了更長的答案,它顯示:

  • 一代正態分布的隨機數據(因為提供的數據集非常小)
  • 對 numpy 數組執行計算
  • 切片數組以返回 <= 40% 的值
  • 使用Plotly 庫繪制結果 - 僅限 API。

示例代碼:

import numpy as np
import plotly.io as pio

# Generate random dataset (for demo only).
np.random.seed(1)
X = np.random.normal(0, 1, 10000)

# Calculate the cumulative frequency.
X_ = np.cumsum(X)*100/X.sum()
data = X_[X_ <= 40]

# Plot the histogram.
pio.show({'data': {'x': data, 
                   'type': 'histogram', 
                   'marker': {'line': {'width': 0.5}}},
          'layout': {'title': 'Cumulative Frequency Demo'}})

輸出:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM