簡體   English   中英

R 中每個值一列的直方圖

[英]histogram with one column per each value in R

我正在嘗試 R 中的 plot 簡單直方圖。 我有一個 integer 向量,我想繪制一個直方圖,每個值有一列。

test_data = c(1,1,1,2,2,3,3,4)
hist(test_data)

但我明白了

在此處輸入圖像描述

請告訴我是否可以獲得與 Python 相同的結果?

import matplotlib.pyplot as plt
test_data = [1,1,1,2,2,3,3,4]
plt.hist(test_data)
plt.show()

在此處輸入圖像描述

你可以使用 barplot 和 table 函數

barplot(table(test_data))

您可以使用nclassbreaks參數來調整 bin 的數量。

test_data = c(1,1,1,2,2,3,3,4)
hist(test_data,breaks=5)
hist(test_data,nclass=5)

在此處輸入圖像描述

事實上,python 也是一樣的。 參數是bins 默認值為10(根據this page

所以如果你修改它,我們會得到一個不同的 plot

import matplotlib.pyplot as plt
test_data = [1,1,1,2,2,3,3,4]
plt.hist(test_data,bins=4)
plt.show()

你得到

在此處輸入圖像描述

暫無
暫無

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

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