python - 如何使用 ggplot2 在 ipython notebook 中制作直方图(适用于 python)

标签 python ggplot2 ipython ipython-notebook python-ggplot

我正在尝试使用 ipython notebook 在 python 中制作一个简单数字列表的直方图和 ggplot for python .使用 pylab,这很容易,但我无法让 ggplot 工作。

我正在使用此代码(基于菱形直方图示例,它对我有用):

from ggplot import *
a = [1, 1, 2, 1, 1, 4, 5, 6]
p = ggplot(aes(x='carat'), data=a)
p + geom_hist() + ggtitle("Histogram of Diamond Carats") + labs("Carats", "Freq")

使用 ipython 和 pylab,我可以只用 hist(a) 制作直方图并显示出来。如何使用 ggplot 绘制直方图?

最佳答案

如果您只想制作向量“a”中数字的直方图,则有几个问题。

首先,ggplot 接受 pandas Dataframe 形式的数据,因此您需要先构建它。

import pandas as pd
a = [1, 1, 2, 1, 1, 4, 5, 6]
df = pd.DataFrame(a, columns=['a'])

其次,geom 是 geom_histogram() 而不是 geom_hist()。最后,看起来您正在从钻石数据的示例图之一中输入代码。你不需要那个,所以我把它去掉了。

from ggplot import *
p = ggplot(aes(x='a'), data=df)
p + geom_histogram(binwidth=1)

enter image description here

关于python - 如何使用 ggplot2 在 ipython notebook 中制作直方图(适用于 python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19377371/

相关文章:

python - 连接几个文件删除标题行

r - 在 ggplot 代码中使用 ifelse() 条件时出错

r - ggplot2:yearmon 比例和 geom_bar

ipython Qtconsole - 键绑定(bind)冲突

python - KeyError : 'text/plain' mean? 是什么意思

python - 如何使用 Python 在 Linux 下查看 nfs 挂载目录中新创建或更改的文件?

python - Python中基于FFT的二维卷积和相关性

r - 如何在 R 中使用 plotly 在时间序列中绘制多个系列/线?

python - IPython 中输入编码的奇怪问题

python - 强制 Sphinx 在 Python 文档字符串中解释 Markdown 而不是 reStructuredText